From 8f20ec23d9d92e09c8115fe66ec1a5984f6e6066 Mon Sep 17 00:00:00 2001
From: gnaegi <none@none>
Date: Fri, 10 Nov 2017 18:25:17 +0100
Subject: [PATCH] OO-3133 GUI demo for REST API and JS inclusion

---
 .../olat/gui/demo/_spring/guiDemoContext.xml  |  14 +++
 .../demo/guidemo/GuiDemoRESTController.java   |  75 +++++++++++
 .../demo/guidemo/_content/guidemo-rest.html   | 117 ++++++++++++++++++
 .../guidemo/_i18n/LocalStrings_de.properties  |   2 +
 .../guidemo/_i18n/LocalStrings_en.properties  |   2 +
 .../org/olat/user/restapi/UserWebService.java |   2 +-
 6 files changed, 211 insertions(+), 1 deletion(-)
 create mode 100644 src/main/java/org/olat/gui/demo/guidemo/GuiDemoRESTController.java
 create mode 100644 src/main/java/org/olat/gui/demo/guidemo/_content/guidemo-rest.html

diff --git a/src/main/java/org/olat/gui/demo/_spring/guiDemoContext.xml b/src/main/java/org/olat/gui/demo/_spring/guiDemoContext.xml
index 4cdd968be1f..cf95e2749be 100644
--- a/src/main/java/org/olat/gui/demo/_spring/guiDemoContext.xml
+++ b/src/main/java/org/olat/gui/demo/_spring/guiDemoContext.xml
@@ -262,4 +262,18 @@
 			</property>
 			<property name="order" value="33" />
 		</bean>
+		
+		<bean class="org.olat.core.extensions.action.GenericActionExtension" init-method="initExtensionPoints">
+			<property name="actionController">	
+				<bean class="org.olat.core.gui.control.creator.AutoCreator" scope="prototype">
+					<property name="className" value="org.olat.gui.demo.guidemo.GuiDemoRESTController"/>
+				</bean>
+			</property>
+			<property name="extensionPoints">
+				<list>	
+					<value>org.olat.gui.demo.GUIDemoMainController</value>		
+				</list>
+			</property>
+			<property name="order" value="35" />
+		</bean>
 </beans>
\ No newline at end of file
diff --git a/src/main/java/org/olat/gui/demo/guidemo/GuiDemoRESTController.java b/src/main/java/org/olat/gui/demo/guidemo/GuiDemoRESTController.java
new file mode 100644
index 00000000000..b699f6794e3
--- /dev/null
+++ b/src/main/java/org/olat/gui/demo/guidemo/GuiDemoRESTController.java
@@ -0,0 +1,75 @@
+/**
+ * <a href="http://www.openolat.org">
+ * OpenOLAT - Online Learning and Training</a><br>
+ * <p>
+ * Licensed under the Apache License, Version 2.0 (the "License"); <br>
+ * you may not use this file except in compliance with the License.<br>
+ * You may obtain a copy of the License at the
+ * <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache homepage</a>
+ * <p>
+ * Unless required by applicable law or agreed to in writing,<br>
+ * software distributed under the License is distributed on an "AS IS" BASIS, <br>
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
+ * See the License for the specific language governing permissions and <br>
+ * limitations under the License.
+ * <p>
+ * Initial code contributed and copyrighted by<br>
+ * frentix GmbH, http://www.frentix.com
+ * <p>
+ */
+package org.olat.gui.demo.guidemo;
+
+import org.olat.core.gui.UserRequest;
+import org.olat.core.gui.components.Component;
+import org.olat.core.gui.components.htmlheader.jscss.JSAndCSSComponent;
+import org.olat.core.gui.components.velocity.VelocityContainer;
+import org.olat.core.gui.control.Event;
+import org.olat.core.gui.control.WindowControl;
+import org.olat.core.gui.control.controller.BasicController;
+import org.olat.restapi.RestModule;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * The purpose of this GUI demo is to show how you can add a custom JS library and do some REST queries via JavaScript
+ * 
+ * Initial date: 10.11.2017<br>
+ * @author gnaegi, gnaegi@frentix.com, http://www.frentix.com
+ *
+ */
+public class GuiDemoRESTController extends BasicController {
+	@Autowired
+	private RestModule restModule;
+	
+	private VelocityContainer vcMain;
+
+	public GuiDemoRESTController(UserRequest ureq, WindowControl wControl) {
+		super(ureq, wControl);
+		vcMain = createVelocityContainer("guidemo-rest");		
+		putInitialPanel(vcMain);
+		
+		// Push module state to velocty
+		vcMain.contextPut("restEnabled", restModule.isEnabled());
+
+		// Add the http://listjs.com jQuery plugin from a CDN.
+		//
+		// The JS and CSS component makes sure that the JS and CSS required by
+		// this view is always in the DOM when the view is visible. The JS code
+		// is loaded on-demand on first use.
+		// If you want to load it from within the project, simply use a relative path notation like in this example: 
+		// JSAndCSSComponent js = new JSAndCSSComponent("js", new String[] { "js/jquery/list.js/1.5.0/list.min.js" }, null);
+		JSAndCSSComponent js = new JSAndCSSComponent("js", new String[] { "https://cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js" }, null);
+		vcMain.put("jsAdder", js);
+	}
+
+	@Override
+	protected void event(UserRequest ureq, Component source, Event event) {
+		// nothing to catch
+	}
+
+	@Override
+	protected void doDispose() {
+		// nothing to dispose
+
+	}
+
+}
diff --git a/src/main/java/org/olat/gui/demo/guidemo/_content/guidemo-rest.html b/src/main/java/org/olat/gui/demo/guidemo/_content/guidemo-rest.html
new file mode 100644
index 00000000000..42bf309b444
--- /dev/null
+++ b/src/main/java/org/olat/gui/demo/guidemo/_content/guidemo-rest.html
@@ -0,0 +1,117 @@
+<h2>REST API demo</h2>
+<p>
+	The purpose of this GUI demo is to show how you can add a custom JS library and do some REST queries via JavaScript. 
+	More info about the REST API can be found <a href="$r.relLink("auth/AdminSite/0/restapi/0")">in the REST admin section</a>
+</p>
+<p>
+	The demo also shows how to integrate a JS library to implement custom UI widgets. In this case a simple jQuery plugin
+	has been integrated: http://listjs.com. We use a public CDN for loading of the library. See the <code>GuiDemoRESTController</code> 
+	class to see how simple it is.
+</p>
+
+#if($restEnabled)
+	<p class="o_success">
+		REST API status: <i class="o_icon o_icon_ok o_icon-fw">&nbsp;</i> activated
+	</p>
+
+	## DOM container for example 1)	
+	<h3>OpenOLAT License fetched via REST API</h3>
+	<p>
+		Path: <code>$r.relLink("restapi/api/copyright")</code>
+	<p>
+	<pre style="height: 200px; overflow: scroll;" class="o_small" id="o_guidemo_version">loading...</pre>
+
+
+	## DOM container for example 2)
+	<h3>My group list via REST API</h3>
+	<p>
+		Path: <code>$r.relLink("restapi/groups/")</code>
+	<p>
+	<div id="o_guidemo_groups">loading...</div>
+
+
+	## DOM container for example 3)
+	<h3>My course list via REST API</h3>
+	<p>
+		Path: <code>$r.relLink("restapi/courses/")</code>
+	<p>
+	<div id="o_guidemo_courses">loading...</div>
+	
+	
+<script type="text/javascript">
+/* <![CDATA[ */ 
+	
+	/* OpenOLAT REST API URL for this server */
+	var restAPI ='$r.relLink("restapi")';
+	
+	/* Helper function to read from REST API with simple error handling */
+	function readFromREST(queryPath, callback, dataType) {
+		jQuery.ajax({			
+		    url: restAPI + queryPath,
+		    type: 'GET',
+		    processData: false,
+		    contentType:'application/json',
+		    dataType:dataType,
+		    cache: false,
+			error: function(result) {
+				console.error('Error while reading from OpenOLAT REST API with URL::' + restAPI + ' and key::' + queryPath);
+				console.log(result);
+			}
+		}).done(callback);
+	}
+	
+	/* Example 1: get the copyright */
+	readFromREST('/api/copyright', function(data) {
+		jQuery('#o_guidemo_version').html(data);	
+	}, 'html');
+	
+	
+	/* Example 2: get the list of my groups, convert to HTML list */
+	readFromREST('/groups', function(data) {
+		var groups = "Search groups: <input type='text' class='o_small fuzzy-search' /><ul class='list'>";
+		jQuery(data).each(function(i,val){
+			groups += '<li><span class="name">' + val.name + '</span> <small class="text-muted">ID:' + val.key + '</small></li>';
+		});
+		groups += '</ul>'
+		jQuery('#o_guidemo_groups').html(groups);
+	},'json');
+	/* Init the list plugin for list search. execute defered, timing issues.  More info: http://listjs.com */
+	setTimeout(function() {
+		var groupsList = new List('o_guidemo_groups', { 
+		  valueNames: ['name']
+		});	
+	},400);
+	
+		
+	/* Example 3: get the list of my courses, convert to HTML list */
+	readFromREST('/repo/courses', function(data) {
+		var courses = "Search courses: <input type='text' class='o_small fuzzy-search' /><ul class='list'>";
+		jQuery(data).each(function(i,val){
+			courses += '<li><span class="title">' + val.title + '</span> <small class="text-muted">ID:' + val.key + '</small></li>';
+		});
+		courses += '</ul>'
+		jQuery('#o_guidemo_courses').html(courses);
+	},'json');
+	/* Init the list plugin for list search. execute defered, timing issues. More info: http://listjs.com */
+	setTimeout(function() {
+		var groupsList = new List('o_guidemo_courses', { 
+		  valueNames: ['title']
+		});	
+	},400);
+
+/* ]]> */
+</script>
+
+
+#else 
+	<div class="o_error">
+		<h4>Sorry</h4>
+		<p>
+			This demo only works when the REST module is enabled!
+		</p>
+		<p>
+			The configuration change changed <a href="$r.relLink("auth/AdminSite/0/restapi/0")">in the REST admin section</a>
+		</p>
+		
+	</div>
+#end
\ No newline at end of file
diff --git a/src/main/java/org/olat/gui/demo/guidemo/_i18n/LocalStrings_de.properties b/src/main/java/org/olat/gui/demo/guidemo/_i18n/LocalStrings_de.properties
index 5d59f7f77e7..2a0cd8eb5b1 100644
--- a/src/main/java/org/olat/gui/demo/guidemo/_i18n/LocalStrings_de.properties
+++ b/src/main/java/org/olat/gui/demo/guidemo/_i18n/LocalStrings_de.properties
@@ -21,6 +21,8 @@ GuiDemoLinksController.menu.title=Links & Buttons
 GuiDemoLinksController.menu.title.alt=all available links
 GuiDemoPanesController.menu.title=Panes
 GuiDemoPanesController.menu.title.alt=Panes management
+GuiDemoRESTController.menu.title=REST JS integration
+GuiDemoRESTController.menu.title.alt=Integration example with REST service
 GuiDemoSoaController.menu.title=GUI SOA
 GuiDemoSoaController.menu.title.alt=shows the gui soa
 GuiDemoStepsRunner.menu.title=Steps
diff --git a/src/main/java/org/olat/gui/demo/guidemo/_i18n/LocalStrings_en.properties b/src/main/java/org/olat/gui/demo/guidemo/_i18n/LocalStrings_en.properties
index b6bcb0ae730..fae85251391 100644
--- a/src/main/java/org/olat/gui/demo/guidemo/_i18n/LocalStrings_en.properties
+++ b/src/main/java/org/olat/gui/demo/guidemo/_i18n/LocalStrings_en.properties
@@ -25,6 +25,8 @@ GuiDemoLinksController.menu.title=Links & buttons
 GuiDemoLinksController.menu.title.alt=all available links
 GuiDemoPanesController.menu.title=Panes
 GuiDemoPanesController.menu.title.alt=Panes management
+GuiDemoRESTController.menu.title=REST JS integration
+GuiDemoRESTController.menu.title.alt=Integration example with REST service
 GuiDemoSoaController.menu.title=GUI SOA
 GuiDemoSoaController.menu.title.alt=shows the gui soa
 GuiDemoStepsRunner.menu.title=Steps
diff --git a/src/main/java/org/olat/user/restapi/UserWebService.java b/src/main/java/org/olat/user/restapi/UserWebService.java
index 4c0274630b7..5ca79548c26 100644
--- a/src/main/java/org/olat/user/restapi/UserWebService.java
+++ b/src/main/java/org/olat/user/restapi/UserWebService.java
@@ -153,7 +153,7 @@ public class UserWebService {
 			@QueryParam("statusVisibleLimit") String statusVisibleLimit,
 			@Context UriInfo uriInfo, @Context HttpServletRequest httpRequest) {
 
-		// User lookup allowd for authors, usermanagers and admins. For
+		// User lookup allowed for authors, usermanagers and admins. For
 		// usernamanger and up are considered "administrative" when it comes to
 		// lookup of the user properties
 		boolean isAdministrativeUser = isUserManager(httpRequest);
-- 
GitLab