From c316aee7a15202270c60ce5559b4883aea026e21 Mon Sep 17 00:00:00 2001
From: srosse <none@none>
Date: Wed, 8 May 2013 11:35:25 +0200
Subject: [PATCH] OMA-130: add REST url repo/courses/infos/{courseId}

---
 .../course/CoursesInfosWebService.java        | 98 +++++++++++++------
 .../org/olat/restapi/support/vo/Examples.java | 13 ++-
 .../org/olat/restapi/CoursesInfosTest.java    | 94 ++++++++++++++++++
 .../java/org/olat/restapi/CoursesTest.java    | 15 ---
 .../java/org/olat/test/AllTestsJunit4.java    |  1 +
 5 files changed, 174 insertions(+), 47 deletions(-)
 create mode 100644 src/test/java/org/olat/restapi/CoursesInfosTest.java

diff --git a/src/main/java/org/olat/restapi/repository/course/CoursesInfosWebService.java b/src/main/java/org/olat/restapi/repository/course/CoursesInfosWebService.java
index 29e26a13e04..4693d9f1e7b 100644
--- a/src/main/java/org/olat/restapi/repository/course/CoursesInfosWebService.java
+++ b/src/main/java/org/olat/restapi/repository/course/CoursesInfosWebService.java
@@ -33,6 +33,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.ws.rs.DefaultValue;
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
 import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.Context;
@@ -82,14 +83,14 @@ import org.olat.restapi.support.vo.FolderVO;
 @Path("repo/courses/infos")
 public class CoursesInfosWebService {
 	
-	private OLog log = Tracing.createLoggerFor(CoursesInfosWebService.class);
+	private static final OLog log = Tracing.createLoggerFor(CoursesInfosWebService.class);
 	
 	/**
 	 * Get courses informations viewable by the authenticated user
 	 * @response.representation.200.qname {http://www.example.com}courseVO
 	 * @response.representation.200.mediaType application/xml, application/json, application/json;pagingspec=1.0
 	 * @response.representation.200.doc List of visible courses
-	 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSEVOes}
+	 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSEINFOVOes}
 	 * @param start
 	 * @param limit
 	 * @param httpRequest The HTTP request
@@ -111,35 +112,10 @@ public class CoursesInfosWebService {
 			int totalCount = rm.countGenericANDQueryWithRolesRestriction(params, true);
 			List<RepositoryEntry> repoEntries = rm.genericANDQueryWithRolesRestriction(params, start, limit, true);
 			List<CourseInfoVO> infos = new ArrayList<CourseInfoVO>();
-			
-			
+
 			final Set<Long> forumNotified = new HashSet<Long>();
 			final Map<Long,Set<String>> courseNotified = new HashMap<Long,Set<String>>();
-			NotificationsManager man = NotificationsManager.getInstance();
-			{//collect subscriptions
-				List<String> notiTypes = new ArrayList<String>();
-				notiTypes.add("FolderModule");
-				notiTypes.add("Forum");
-				List<Subscriber> subs = man.getSubscribers(identity, notiTypes);
-				for(Subscriber sub:subs) {
-					String publisherType = sub.getPublisher().getType();
-					String resName = sub.getPublisher().getResName();
-					
-					if("CourseModule".equals(resName)) {
-						if("FolderModule".equals(publisherType)) {
-							Long courseKey = sub.getPublisher().getResId();
-							if(!courseNotified.containsKey(courseKey)) {
-								courseNotified.put(courseKey,new HashSet<String>());
-							}
-							courseNotified.get(courseKey).add(sub.getPublisher().getSubidentifier());
-						} else if ("Forum".equals(publisherType)) {
-							Long forumKey = Long.parseLong(sub.getPublisher().getData());
-							forumNotified.add(forumKey);
-						}
-					}
-				}
-			}
-
+			collectSubscriptions(identity, forumNotified, courseNotified);
 
 			for(RepositoryEntry entry:repoEntries) {
 				CourseInfoVO info = collect(identity, roles, entry, forumNotified, courseNotified);
@@ -158,6 +134,67 @@ public class CoursesInfosWebService {
 		}
 	}
 	
+	/**
+	 * Get course informations viewable by the authenticated user
+	 * @response.representation.200.qname {http://www.example.com}courseVO
+	 * @response.representation.200.mediaType application/xml, application/json
+	 * @response.representation.200.doc Course informations
+	 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSEINFOVO}
+	 * @param courseId The course id
+	 * @param httpRequest The HTTP request
+	 * @param request The REST request
+	 * @return
+	 */
+	@GET
+	@Path("{courseId}")
+	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
+	public Response getCourseInfo(@PathParam("courseId") Long courseId,
+			@Context HttpServletRequest httpRequest, @Context Request request) {
+		
+		Roles roles = getRoles(httpRequest);
+		Identity identity = getIdentity(httpRequest);
+		if(identity != null && roles != null) {
+			Set<Long> forumNotified = new HashSet<Long>();
+			Map<Long,Set<String>> courseNotified = new HashMap<Long,Set<String>>();
+			collectSubscriptions(identity, forumNotified, courseNotified);
+			
+			ICourse course = CourseFactory.loadCourse(courseId);
+			RepositoryEntry entry = RepositoryManager.getInstance().lookupRepositoryEntry(course, true);
+			
+			CourseInfoVO info = collect(identity, roles, entry, forumNotified, courseNotified);
+			return Response.ok(info).build();
+		} else {
+			return Response.serverError().status(Status.FORBIDDEN).build();
+		}
+	}
+	
+	private void collectSubscriptions(Identity identity, Set<Long> forumNotified, Map<Long,Set<String>> courseNotified) {
+		NotificationsManager man = NotificationsManager.getInstance();
+		{//collect subscriptions
+			List<String> notiTypes = new ArrayList<String>();
+			notiTypes.add("FolderModule");
+			notiTypes.add("Forum");
+			List<Subscriber> subs = man.getSubscribers(identity, notiTypes);
+			for(Subscriber sub:subs) {
+				String publisherType = sub.getPublisher().getType();
+				String resName = sub.getPublisher().getResName();
+				
+				if("CourseModule".equals(resName)) {
+					if("FolderModule".equals(publisherType)) {
+						Long courseKey = sub.getPublisher().getResId();
+						if(!courseNotified.containsKey(courseKey)) {
+							courseNotified.put(courseKey,new HashSet<String>());
+						}
+						courseNotified.get(courseKey).add(sub.getPublisher().getSubidentifier());
+					} else if ("Forum".equals(publisherType)) {
+						Long forumKey = Long.parseLong(sub.getPublisher().getData());
+						forumNotified.add(forumKey);
+					}
+				}
+			}
+		}
+	}
+	
 	private CourseInfoVO collect(final Identity identity, final Roles roles, final RepositoryEntry entry,
 			final Set<Long> forumNotified, final Map<Long,Set<String>> courseNotified) {
 		
@@ -198,5 +235,4 @@ public class CoursesInfosWebService {
 		}
 		return info;
 	}
-
-}
+}
\ No newline at end of file
diff --git a/src/main/java/org/olat/restapi/support/vo/Examples.java b/src/main/java/org/olat/restapi/support/vo/Examples.java
index 12ecc30ee75..2e35c4e2651 100644
--- a/src/main/java/org/olat/restapi/support/vo/Examples.java
+++ b/src/main/java/org/olat/restapi/support/vo/Examples.java
@@ -46,6 +46,9 @@ public class Examples {
 	public static final CourseVO SAMPLE_COURSEVO = new CourseVO();
 	public static final CourseVOes SAMPLE_COURSEVOes = new CourseVOes();
 	
+	public static final CourseInfoVO SAMPLE_COURSEINFOVO = new CourseInfoVO();
+	public static final CourseInfoVOes SAMPLE_COURSEINFOVOes = new CourseInfoVOes();
+	
 	public static final CourseNodeVO SAMPLE_COURSENODEVO = new CourseNodeVO();
 	public static final CourseNodeVOes SAMPLE_COURSENODEVOes = new CourseNodeVOes();
 
@@ -109,7 +112,15 @@ public class Examples {
   	
   	SAMPLE_COURSENODEVO.setId("id");
   	SAMPLE_COURSENODEVOes.getNodes().add(SAMPLE_COURSENODEVO);
-  	
+
+  	SAMPLE_COURSEINFOVO.setKey(777l);
+  	SAMPLE_COURSEINFOVO.setDisplayName("Demo course");
+  	SAMPLE_COURSEINFOVO.setTitle("Demo course");
+  	SAMPLE_COURSEINFOVO.setRepoEntryKey(456l);
+  	SAMPLE_COURSEINFOVO.setSoftKey("oo_98237498");
+  	SAMPLE_COURSEINFOVOes.setTotalCount(1);
+  	SAMPLE_COURSEINFOVOes.setInfos(new CourseInfoVO[]{SAMPLE_COURSEINFOVO});
+
   	SAMPLE_COURSECONFIGVO.setSharedFolderSoftKey("head_1_olat_43985684395");
   	
   	SAMPLE_FILE.setHref("href");
diff --git a/src/test/java/org/olat/restapi/CoursesInfosTest.java b/src/test/java/org/olat/restapi/CoursesInfosTest.java
new file mode 100644
index 00000000000..c0f1da87fa2
--- /dev/null
+++ b/src/test/java/org/olat/restapi/CoursesInfosTest.java
@@ -0,0 +1,94 @@
+/**
+* OLAT - Online Learning and Training<br>
+* http://www.olat.org
+* <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
+* <p>
+* http://www.apache.org/licenses/LICENSE-2.0
+* <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>
+* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
+* University of Zurich, Switzerland.
+* <hr>
+* <a href="http://www.openolat.org">
+* OpenOLAT - Online Learning and Training</a><br>
+* This file has been modified by the OpenOLAT community. Changes are licensed
+* under the Apache 2.0 license as the original file.  
+* <p>
+*/
+package org.olat.restapi;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import javax.ws.rs.core.MediaType;
+
+import junit.framework.Assert;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.junit.Test;
+import org.olat.basesecurity.BaseSecurityManager;
+import org.olat.core.id.Identity;
+import org.olat.course.ICourse;
+import org.olat.restapi.repository.course.CoursesWebService;
+import org.olat.restapi.support.vo.CourseInfoVO;
+import org.olat.restapi.support.vo.CourseInfoVOes;
+import org.olat.test.OlatJerseyTestCase;
+
+/**
+ * 
+ * Initial date: 08.05.2013<br>
+ * @author srosse, stephane.rosse@frentix.com, http://www.frentix.com
+ *
+ */
+public class CoursesInfosTest extends OlatJerseyTestCase {
+	
+	@Test
+	public void testGetCourseInfos() throws IOException, URISyntaxException {
+		RestConnection conn = new RestConnection();
+		assertTrue(conn.login("administrator", "openolat"));
+
+		URI uri = conn.getContextURI().path("repo").path("courses").path("infos").build();
+
+		HttpGet get = conn.createGet(uri, MediaType.APPLICATION_JSON + ";pagingspec=1.0", true);
+		HttpResponse response = conn.execute(get);
+		Assert.assertEquals(200, response.getStatusLine().getStatusCode());
+		CourseInfoVOes infos = conn.parse(response, CourseInfoVOes.class);
+		Assert.assertNotNull(infos);
+		
+		conn.shutdown();
+	}
+	
+	@Test
+	public void testGetCourseInfos_byId() throws IOException, URISyntaxException {
+		Identity admin = BaseSecurityManager.getInstance().findIdentityByName("administrator");
+		ICourse course = CoursesWebService.createEmptyCourse(admin, "course-info 1", "course long name", null);
+
+		RestConnection conn = new RestConnection();
+		assertTrue(conn.login("administrator", "openolat"));
+		
+		URI uri = conn.getContextURI().path("repo").path("courses").path("infos").path(course.getResourceableId().toString()).build();
+
+		HttpGet get = conn.createGet(uri, MediaType.APPLICATION_JSON, true);
+		HttpResponse response = conn.execute(get);
+		assertEquals(200, response.getStatusLine().getStatusCode());
+		CourseInfoVO infos = conn.parse(response, CourseInfoVO.class);
+		Assert.assertNotNull(infos);
+		Assert.assertEquals("course-info 1", infos.getTitle());
+		Assert.assertEquals("course-info 1", infos.getDisplayName());
+
+		conn.shutdown();
+	}
+}
\ No newline at end of file
diff --git a/src/test/java/org/olat/restapi/CoursesTest.java b/src/test/java/org/olat/restapi/CoursesTest.java
index 187ccf4e47e..316cdd6800b 100644
--- a/src/test/java/org/olat/restapi/CoursesTest.java
+++ b/src/test/java/org/olat/restapi/CoursesTest.java
@@ -65,7 +65,6 @@ import org.olat.course.ICourse;
 import org.olat.repository.RepositoryEntry;
 import org.olat.repository.RepositoryManager;
 import org.olat.restapi.repository.course.CoursesWebService;
-import org.olat.restapi.support.vo.CourseInfoVOes;
 import org.olat.restapi.support.vo.CourseVO;
 import org.olat.restapi.support.vo.CourseVOes;
 import org.olat.test.OlatJerseyTestCase;
@@ -209,20 +208,6 @@ public class CoursesTest extends OlatJerseyTestCase {
 		assertEquals(softKey, re.getSoftkey());
 	}
 	
-	@Test
-	public void testGetCourseInfos() throws IOException, URISyntaxException {
-		boolean loggedIN = conn.login("administrator", "openolat");
-		assertTrue(loggedIN);
-
-		URI uri = conn.getContextURI().path("repo").path("courses").path("infos").build();
-
-		HttpGet get = conn.createGet(uri, MediaType.APPLICATION_JSON + ";pagingspec=1.0", true);
-		HttpResponse response = conn.execute(get);
-		assertEquals(200, response.getStatusLine().getStatusCode());
-		CourseInfoVOes infos = conn.parse(response, CourseInfoVOes.class);
-		assertNotNull(infos);
-	}
-	
 	protected List<CourseVO> parseCourseArray(InputStream body) {
 		try {
 			ObjectMapper mapper = new ObjectMapper(jsonFactory); 
diff --git a/src/test/java/org/olat/test/AllTestsJunit4.java b/src/test/java/org/olat/test/AllTestsJunit4.java
index 009c683c1d5..a253e5d28dc 100644
--- a/src/test/java/org/olat/test/AllTestsJunit4.java
+++ b/src/test/java/org/olat/test/AllTestsJunit4.java
@@ -155,6 +155,7 @@ import org.junit.runners.Suite;
 	org.olat.restapi.CoursesForumsTest.class,
 	org.olat.restapi.CoursesResourcesFoldersTest.class,
 	org.olat.restapi.CoursesTest.class,
+	org.olat.restapi.CoursesInfosTest.class,
 	org.olat.restapi.CourseTest.class,
 	org.olat.restapi.FolderTest.class,
 	org.olat.restapi.ForumTest.class,
-- 
GitLab