Search in sources :

Example 31 with CourseVO

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

the class CoursesTest method testCreateEmpty_withInitialAuthor.

@Test
public void testCreateEmpty_withInitialAuthor() throws IOException, URISyntaxException {
    Identity adhocAuthor = JunitTestHelper.createAndPersistIdentityAsRndUser("adhoc-author");
    dbInstance.commit();
    assertTrue(conn.login("administrator", "openolat"));
    URI uri = UriBuilder.fromUri(getContextURI()).path("repo").path("courses").queryParam("shortTitle", "Course without author").queryParam("title", "Course without author").queryParam("initialAuthor", adhocAuthor.getKey().toString()).build();
    HttpPut method = conn.createPut(uri, MediaType.APPLICATION_JSON, true);
    HttpResponse response = conn.execute(method);
    Assert.assertEquals(200, response.getStatusLine().getStatusCode());
    CourseVO courseVo = conn.parse(response, CourseVO.class);
    Assert.assertNotNull(courseVo);
    Assert.assertEquals("Course without author", courseVo.getTitle());
    // load repository entry
    RepositoryEntry re = repositoryManager.lookupRepositoryEntry(courseVo.getRepoEntryKey());
    Assert.assertNotNull(re);
    Assert.assertNotNull(re.getOlatResource());
    Assert.assertEquals("Course without author", re.getDisplayname());
    // load the course
    ICourse course = CourseFactory.loadCourse(re.getOlatResource().getResourceableId());
    Assert.assertNotNull(course);
    Assert.assertEquals("Course without author", course.getCourseTitle());
    Assert.assertEquals(re, course.getCourseEnvironment().getCourseGroupManager().getCourseEntry());
    // check the list of owners
    List<Identity> owners = repositoryEntryRelationDao.getMembers(re, RepositoryEntryRelationType.both, GroupRoles.owner.name());
    Assert.assertNotNull(owners);
    Assert.assertEquals(1, owners.size());
    Assert.assertEquals(adhocAuthor, owners.get(0));
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) HttpResponse(org.apache.http.HttpResponse) ICourse(org.olat.course.ICourse) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) URI(java.net.URI) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 32 with CourseVO

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

the class CoursesWebService method createEmptyCourse.

/**
 * Creates an empty course, or a copy from a course if the parameter copyFrom is set.
 * @response.representation.200.qname {http://www.example.com}courseVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The metadatas of the created course
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSEVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @param shortTitle The short title
 * @param title The title
 * @param sharedFolderSoftKey The repository entry key of a shared folder (optional)
 * @param copyFrom The course primary key key to make a copy from (optional)
 * @param initialAuthor The primary key of the initial author (optional)
 * @param noAuthor True to create a course without the author
 * @param request The HTTP request
 * @return It returns the id of the newly created Course
 */
@PUT
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response createEmptyCourse(@QueryParam("shortTitle") String shortTitle, @QueryParam("title") String title, @QueryParam("displayName") String displayName, @QueryParam("description") String description, @QueryParam("softKey") String softKey, @QueryParam("access") Integer access, @QueryParam("membersOnly") Boolean membersOnly, @QueryParam("externalId") String externalId, @QueryParam("externalRef") String externalRef, @QueryParam("authors") String authors, @QueryParam("location") String location, @QueryParam("managedFlags") String managedFlags, @QueryParam("sharedFolderSoftKey") String sharedFolderSoftKey, @QueryParam("copyFrom") Long copyFrom, @QueryParam("initialAuthor") Long initialAuthor, @QueryParam("setAuthor") @DefaultValue("true") Boolean setAuthor, @Context HttpServletRequest request) {
    if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    CourseConfigVO configVO = new CourseConfigVO();
    configVO.setSharedFolderSoftKey(sharedFolderSoftKey);
    int accessInt = (access == null ? RepositoryEntry.ACC_OWNERS : access.intValue());
    boolean membersOnlyBool = (membersOnly == null ? false : membersOnly.booleanValue());
    if (!StringHelper.containsNonWhitespace(displayName)) {
        displayName = shortTitle;
    }
    ICourse course;
    UserRequest ureq = getUserRequest(request);
    Identity id = null;
    if (setAuthor != null && setAuthor.booleanValue()) {
        if (initialAuthor != null) {
            id = getIdentity(initialAuthor);
        }
        if (id == null) {
            id = ureq.getIdentity();
        }
    }
    if (copyFrom != null) {
        course = copyCourse(copyFrom, ureq, id, shortTitle, title, displayName, description, softKey, accessInt, membersOnlyBool, authors, location, externalId, externalRef, managedFlags, configVO);
    } else {
        course = createEmptyCourse(id, shortTitle, title, displayName, description, softKey, accessInt, membersOnlyBool, authors, location, externalId, externalRef, managedFlags, configVO);
    }
    if (course == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    CourseVO vo = ObjectFactory.get(course);
    return Response.ok(vo).build();
}
Also used : CourseConfigVO(org.olat.restapi.support.vo.CourseConfigVO) CourseVO(org.olat.restapi.support.vo.CourseVO) ICourse(org.olat.course.ICourse) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) RestSecurityHelper.getUserRequest(org.olat.restapi.security.RestSecurityHelper.getUserRequest) UserRequest(org.olat.core.gui.UserRequest) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 33 with CourseVO

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

the class CoursesWebService method importCourse.

/**
 * Imports a course from a course archive zip file
 * @response.representation.200.qname {http://www.example.com}courseVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The metadatas of the imported course
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSEVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @param ownerUsername set the owner of the imported course to the user of this username.
 * @param request The HTTP request
 * @return It returns the imported course
 */
@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.MULTIPART_FORM_DATA })
public Response importCourse(@QueryParam("ownerUsername") String ownerUsername, @Context HttpServletRequest request) {
    if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    UserRequest ureq = RestSecurityHelper.getUserRequest(request);
    Identity identity = null;
    // Set the owner of the imported course to the user defined in the parameter
    if (ownerUsername != null && !ownerUsername.isEmpty() && isAuthor(request)) {
        identity = BaseSecurityManager.getInstance().findIdentityByName(ownerUsername);
        if (identity == null) {
            return Response.serverError().status(Status.BAD_REQUEST).build();
        }
    }
    if (identity == null) {
        identity = ureq.getIdentity();
    }
    MultipartReader partsReader = null;
    try {
        partsReader = new MultipartReader(request);
        File tmpFile = partsReader.getFile();
        long length = tmpFile.length();
        if (length > 0) {
            Long accessRaw = partsReader.getLongValue("access");
            int access = accessRaw != null ? accessRaw.intValue() : RepositoryEntry.ACC_OWNERS;
            String membersOnlyRaw = partsReader.getValue("membersOnly");
            boolean membersonly = "true".equals(membersOnlyRaw);
            String softKey = partsReader.getValue("softkey");
            String displayName = partsReader.getValue("displayname");
            ICourse course = importCourse(ureq, identity, tmpFile, displayName, softKey, access, membersonly);
            CourseVO vo = ObjectFactory.get(course);
            return Response.ok(vo).build();
        }
        return Response.serverError().status(Status.NO_CONTENT).build();
    } catch (Exception e) {
        log.error("Error while importing a file", e);
    } finally {
        MultipartReader.closeQuietly(partsReader);
    }
    CourseVO vo = null;
    return Response.ok(vo).build();
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) ICourse(org.olat.course.ICourse) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) File(java.io.File) RestSecurityHelper.getUserRequest(org.olat.restapi.security.RestSecurityHelper.getUserRequest) UserRequest(org.olat.core.gui.UserRequest) MultipartReader(org.olat.restapi.support.MultipartReader) WebApplicationException(javax.ws.rs.WebApplicationException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 34 with CourseVO

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

the class CoursesWebService method createEmptyCourse.

/**
 * Creates an empty course
 * @response.representation.200.qname {http://www.example.com}courseVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The metadatas of the created course
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSEVO}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @param courseVo The course
 * @param request The HTTP request
 * @return It returns the newly created course
 */
@PUT
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response createEmptyCourse(CourseVO courseVo, @Context HttpServletRequest request) {
    if (!isAuthor(request)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    UserRequest ureq = getUserRequest(request);
    CourseConfigVO configVO = new CourseConfigVO();
    ICourse course = createEmptyCourse(ureq.getIdentity(), courseVo.getTitle(), courseVo.getTitle(), courseVo.getTitle(), courseVo.getDescription(), courseVo.getSoftKey(), RepositoryEntry.ACC_OWNERS, false, courseVo.getAuthors(), courseVo.getLocation(), courseVo.getExternalId(), courseVo.getExternalRef(), courseVo.getManagedFlags(), configVO);
    CourseVO vo = ObjectFactory.get(course);
    return Response.ok(vo).build();
}
Also used : CourseConfigVO(org.olat.restapi.support.vo.CourseConfigVO) CourseVO(org.olat.restapi.support.vo.CourseVO) ICourse(org.olat.course.ICourse) RestSecurityHelper.getUserRequest(org.olat.restapi.security.RestSecurityHelper.getUserRequest) UserRequest(org.olat.core.gui.UserRequest) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Example 35 with CourseVO

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

the class UserCoursesWebService method getTeachedCourses.

/**
 * Retrieves the list of "My supervised courses" but limited to courses.
 * @response.representation.200.qname {http://www.example.com}courseVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The courses
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_COURSEVOes}
 * @response.representation.401.doc The roles of the authenticated user are not sufficient
 * @param start The first result
 * @param limit Max result
 * @param httpRequest The HTTP request
 * @param request The REST request
 * @return The list of my supervised entries
 */
@GET
@Path("teached")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response getTeachedCourses(@QueryParam("start") @DefaultValue("0") Integer start, @QueryParam("limit") @DefaultValue("25") Integer limit, @Context HttpServletRequest httpRequest, @Context Request request) {
    RepositoryManager rm = RepositoryManager.getInstance();
    if (MediaTypeVariants.isPaged(httpRequest, request)) {
        List<RepositoryEntry> repoEntries = rm.getLearningResourcesAsTeacher(identity, start, limit, RepositoryEntryOrder.nameAsc);
        int totalCount = rm.countLearningResourcesAsTeacher(identity);
        CourseVO[] vos = toCourseVo(repoEntries);
        CourseVOes voes = new CourseVOes();
        voes.setCourses(vos);
        voes.setTotalCount(totalCount);
        return Response.ok(voes).build();
    } else {
        List<RepositoryEntry> repoEntries = rm.getLearningResourcesAsTeacher(identity, 0, -1);
        CourseVO[] vos = toCourseVo(repoEntries);
        return Response.ok(vos).build();
    }
}
Also used : CourseVO(org.olat.restapi.support.vo.CourseVO) RepositoryManager(org.olat.repository.RepositoryManager) RepositoryEntry(org.olat.repository.RepositoryEntry) CourseVOes(org.olat.restapi.support.vo.CourseVOes) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

CourseVO (org.olat.restapi.support.vo.CourseVO)70 Test (org.junit.Test)48 URI (java.net.URI)46 HttpResponse (org.apache.http.HttpResponse)46 RepositoryEntry (org.olat.repository.RepositoryEntry)34 HttpGet (org.apache.http.client.methods.HttpGet)26 ICourse (org.olat.course.ICourse)26 Identity (org.olat.core.id.Identity)20 InputStream (java.io.InputStream)18 Produces (javax.ws.rs.Produces)16 HttpEntity (org.apache.http.HttpEntity)16 HttpPut (org.apache.http.client.methods.HttpPut)16 File (java.io.File)14 HttpPost (org.apache.http.client.methods.HttpPost)14 CourseVOes (org.olat.restapi.support.vo.CourseVOes)14 URL (java.net.URL)12 GET (javax.ws.rs.GET)8 Path (javax.ws.rs.Path)8 UserRequest (org.olat.core.gui.UserRequest)8 RepositoryManager (org.olat.repository.RepositoryManager)8