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));
}
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();
}
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();
}
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();
}
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();
}
}
Aggregations