Search in sources :

Example 1 with CourseInfoVO

use of org.olat.restapi.support.vo.CourseInfoVO in project OpenOLAT by OpenOLAT.

the class CoursesInfosTest method testGetCourseInfos_byId.

@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();
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ICourse(org.olat.course.ICourse) Identity(org.olat.core.id.Identity) CourseInfoVO(org.olat.restapi.support.vo.CourseInfoVO) URI(java.net.URI) Test(org.junit.Test)

Example 2 with CourseInfoVO

use of org.olat.restapi.support.vo.CourseInfoVO in project OpenOLAT by OpenOLAT.

the class CoursesInfosWebService method getCourseInfo.

/**
 * 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
 * @return
 */
@GET
@Path("{courseId}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getCourseInfo(@PathParam("courseId") Long courseId, @Context HttpServletRequest httpRequest) {
    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();
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Roles(org.olat.core.id.Roles) RestSecurityHelper.getRoles(org.olat.restapi.security.RestSecurityHelper.getRoles) ICourse(org.olat.course.ICourse) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) CourseInfoVO(org.olat.restapi.support.vo.CourseInfoVO) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with CourseInfoVO

use of org.olat.restapi.support.vo.CourseInfoVO in project openolat by klemens.

the class CoursesInfosTest method testGetCourseInfos_byId.

@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();
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ICourse(org.olat.course.ICourse) Identity(org.olat.core.id.Identity) CourseInfoVO(org.olat.restapi.support.vo.CourseInfoVO) URI(java.net.URI) Test(org.junit.Test)

Example 4 with CourseInfoVO

use of org.olat.restapi.support.vo.CourseInfoVO in project openolat by klemens.

the class CoursesInfosWebService method collect.

private CourseInfoVO collect(final Identity identity, final Roles roles, final RepositoryEntry entry, final Set<Long> forumNotified, final Map<Long, Set<String>> courseNotified) {
    CourseInfoVO info = new CourseInfoVO();
    info.setRepoEntryKey(entry.getKey());
    info.setSoftKey(entry.getSoftkey());
    info.setDisplayName(entry.getDisplayname());
    ACService acManager = CoreSpringFactory.getImpl(ACService.class);
    AccessResult result = acManager.isAccessible(entry, identity, false);
    if (result.isAccessible()) {
        try {
            final ICourse course = CourseFactory.loadCourse(entry);
            final List<FolderVO> folders = new ArrayList<FolderVO>();
            final List<ForumVO> forums = new ArrayList<ForumVO>();
            final IdentityEnvironment ienv = new IdentityEnvironment(identity, roles);
            new CourseTreeVisitor(course, ienv).visit(new Visitor() {

                @Override
                public void visit(INode node) {
                    if (node instanceof BCCourseNode) {
                        BCCourseNode bcNode = (BCCourseNode) node;
                        folders.add(BCWebService.createFolderVO(ienv, course, bcNode, courseNotified.get(course.getResourceableId())));
                    } else if (node instanceof FOCourseNode) {
                        FOCourseNode forumNode = (FOCourseNode) node;
                        forums.add(ForumCourseNodeWebService.createForumVO(course, forumNode, forumNotified));
                    }
                }
            }, new VisibleTreeFilter());
            info.setKey(course.getResourceableId());
            info.setTitle(course.getCourseTitle());
            info.setFolders(folders.toArray(new FolderVO[folders.size()]));
            info.setForums(forums.toArray(new ForumVO[forums.size()]));
        } catch (Exception e) {
            log.error("", e);
        }
    }
    return info;
}
Also used : ForumVO(org.olat.modules.fo.restapi.ForumVO) INode(org.olat.core.util.nodes.INode) Visitor(org.olat.core.util.tree.Visitor) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) FolderVO(org.olat.restapi.support.vo.FolderVO) VisibleTreeFilter(org.olat.course.run.userview.VisibleTreeFilter) CourseTreeVisitor(org.olat.course.run.userview.CourseTreeVisitor) ArrayList(java.util.ArrayList) ICourse(org.olat.course.ICourse) FOCourseNode(org.olat.course.nodes.FOCourseNode) BCCourseNode(org.olat.course.nodes.BCCourseNode) ACService(org.olat.resource.accesscontrol.ACService) AccessResult(org.olat.resource.accesscontrol.AccessResult) CourseInfoVO(org.olat.restapi.support.vo.CourseInfoVO) IdentityEnvironment(org.olat.core.id.IdentityEnvironment)

Example 5 with CourseInfoVO

use of org.olat.restapi.support.vo.CourseInfoVO in project openolat by klemens.

the class CoursesInfosWebService method getCourseInfo.

/**
 * 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
 * @return
 */
@GET
@Path("{courseId}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getCourseInfo(@PathParam("courseId") Long courseId, @Context HttpServletRequest httpRequest) {
    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();
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) Roles(org.olat.core.id.Roles) RestSecurityHelper.getRoles(org.olat.restapi.security.RestSecurityHelper.getRoles) ICourse(org.olat.course.ICourse) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) CourseInfoVO(org.olat.restapi.support.vo.CourseInfoVO) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

CourseInfoVO (org.olat.restapi.support.vo.CourseInfoVO)8 Identity (org.olat.core.id.Identity)6 ICourse (org.olat.course.ICourse)6 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Set (java.util.Set)4 GET (javax.ws.rs.GET)4 Produces (javax.ws.rs.Produces)4 Roles (org.olat.core.id.Roles)4 RepositoryEntry (org.olat.repository.RepositoryEntry)4 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)4 RestSecurityHelper.getRoles (org.olat.restapi.security.RestSecurityHelper.getRoles)4 URI (java.net.URI)2 Path (javax.ws.rs.Path)2 HttpResponse (org.apache.http.HttpResponse)2 HttpGet (org.apache.http.client.methods.HttpGet)2 Test (org.junit.Test)2 IdentityEnvironment (org.olat.core.id.IdentityEnvironment)2 INode (org.olat.core.util.nodes.INode)2