use of org.olat.repository.RepositoryService in project OpenOLAT by OpenOLAT.
the class CourseWebService method getRepoFileById.
/**
* Export the course
* @response.representation.200.mediaType application/zip
* @response.representation.200.doc The course as a ZIP file
* @response.representation.401.doc Not authorized to export the course
* @response.representation.404.doc The course not found
* @return It returns the <code>CourseVO</code> object representing the course.
*/
@GET
@Path("file")
@Produces({ "application/zip", MediaType.APPLICATION_OCTET_STREAM })
public Response getRepoFileById(@Context HttpServletRequest request) {
RepositoryService rs = CoreSpringFactory.getImpl(RepositoryService.class);
RepositoryEntry re = course.getCourseEnvironment().getCourseGroupManager().getCourseEntry();
if (re == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
RepositoryHandler typeToDownload = RepositoryHandlerFactory.getInstance().getRepositoryHandler(re);
if (typeToDownload == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
Identity identity = getIdentity(request);
boolean canDownload = re.getCanDownload() && typeToDownload.supportsDownload();
if (isAdmin(request) || RepositoryManager.getInstance().isOwnerOfRepositoryEntry(identity, re)) {
canDownload = true;
} else if (!isAuthor(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
if (!canDownload) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
OLATResource ores = OLATResourceManager.getInstance().findResourceable(re.getOlatResource());
if (ores == null) {
return Response.serverError().status(Status.NOT_FOUND).build();
}
boolean isAlreadyLocked = typeToDownload.isLocked(ores);
LockResult lockResult = null;
try {
lockResult = typeToDownload.acquireLock(ores, identity);
if (lockResult == null || (lockResult != null && lockResult.isSuccess() && !isAlreadyLocked)) {
MediaResource mr = typeToDownload.getAsMediaResource(ores, false);
if (mr != null) {
rs.incrementDownloadCounter(re);
// success
return Response.ok(mr.getInputStream()).cacheControl(cc).build();
} else {
return Response.serverError().status(Status.NO_CONTENT).build();
}
} else {
return Response.serverError().status(Status.CONFLICT).build();
}
} finally {
if ((lockResult != null && lockResult.isSuccess() && !isAlreadyLocked)) {
typeToDownload.releaseLock(lockResult);
}
}
}
use of org.olat.repository.RepositoryService in project OpenOLAT by OpenOLAT.
the class CourseWebService method deleteCoursePermanently.
/**
* Change the status of a course by id. The possible status are:
* <ul>
* <li>closed</li>
* <li>unclosed</li>
* <li>unpublished</li>
* <li>deleted</li>
* <li>restored</li>
* </ul>
*
* @response.representation.200.doc The metadatas of the deleted course
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The course not found
* @param request The HTTP request
* @return It returns the XML representation of the <code>Structure</code>
* object representing the course.
*/
@POST
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Path("status")
public Response deleteCoursePermanently(@FormParam("newStatus") String newStatus, @Context HttpServletRequest request) {
if (!isAuthor(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
} else if (!isAuthorEditor(course, request) && !isInstitutionalResourceManager(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
RepositoryService rs = CoreSpringFactory.getImpl(RepositoryService.class);
RepositoryEntry re = course.getCourseEnvironment().getCourseGroupManager().getCourseEntry();
if ("closed".equals(newStatus)) {
rs.closeRepositoryEntry(re);
log.audit("REST closing course: " + re.getDisplayname() + " [" + re.getKey() + "]");
ThreadLocalUserActivityLogger.log(LearningResourceLoggingAction.LEARNING_RESOURCE_CLOSE, getClass(), LoggingResourceable.wrap(re, OlatResourceableType.genRepoEntry));
} else if ("unclosed".equals(newStatus)) {
rs.uncloseRepositoryEntry(re);
log.audit("REST unclosing course: " + re.getDisplayname() + " [" + re.getKey() + "]");
ThreadLocalUserActivityLogger.log(LearningResourceLoggingAction.LEARNING_RESOURCE_UPDATE, getClass(), LoggingResourceable.wrap(re, OlatResourceableType.genRepoEntry));
} else if ("unpublished".equals(newStatus)) {
rs.unpublishRepositoryEntry(re);
log.audit("REST unpublishing course: " + re.getDisplayname() + " [" + re.getKey() + "]");
ThreadLocalUserActivityLogger.log(LearningResourceLoggingAction.LEARNING_RESOURCE_DEACTIVATE, getClass(), LoggingResourceable.wrap(re, OlatResourceableType.genRepoEntry));
} else if ("deleted".equals(newStatus)) {
Identity identity = getIdentity(request);
rs.deleteSoftly(re, identity, true);
log.audit("REST deleting (soft) course: " + re.getDisplayname() + " [" + re.getKey() + "]");
ThreadLocalUserActivityLogger.log(LearningResourceLoggingAction.LEARNING_RESOURCE_TRASH, getClass(), LoggingResourceable.wrap(re, OlatResourceableType.genRepoEntry));
} else if ("restored".equals(newStatus)) {
rs.restoreRepositoryEntry(re);
log.audit("REST restoring course: " + re.getDisplayname() + " [" + re.getKey() + "]");
ThreadLocalUserActivityLogger.log(LearningResourceLoggingAction.LEARNING_RESOURCE_RESTORE, getClass(), LoggingResourceable.wrap(re, OlatResourceableType.genRepoEntry));
}
return Response.ok().build();
}
use of org.olat.repository.RepositoryService in project OpenOLAT by OpenOLAT.
the class CourseWebService method deleteCourse.
/**
* Delete a course by id.
*
* @response.representation.200.doc The metadatas of the deleted course
* @response.representation.401.doc The roles of the authenticated user are not sufficient
* @response.representation.404.doc The course not found
* @param request The HTTP request
* @return It returns the XML representation of the <code>Structure</code>
* object representing the course.
*/
@DELETE
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response deleteCourse(@Context HttpServletRequest request) {
if (!isAuthor(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
} else if (!isAuthorEditor(course, request) && !isInstitutionalResourceManager(request)) {
return Response.serverError().status(Status.UNAUTHORIZED).build();
}
UserRequest ureq = getUserRequest(request);
RepositoryService rs = CoreSpringFactory.getImpl(RepositoryService.class);
RepositoryEntry re = course.getCourseEnvironment().getCourseGroupManager().getCourseEntry();
ErrorList errors = rs.deletePermanently(re, ureq.getIdentity(), ureq.getUserSession().getRoles(), ureq.getLocale());
if (errors.hasErrors()) {
return Response.serverError().status(500).build();
}
ThreadLocalUserActivityLogger.log(LearningResourceLoggingAction.LEARNING_RESOURCE_DELETE, getClass(), LoggingResourceable.wrap(re, OlatResourceableType.genRepoEntry));
return Response.ok().build();
}
use of org.olat.repository.RepositoryService in project OpenOLAT by OpenOLAT.
the class CoursesWebService method createEmptyCourse.
/**
* Create an empty course with some settings
* @param initialAuthor
* @param shortTitle
* @param longTitle
* @param softKey
* @param externalId
* @param externalRef
* @param managedFlags
* @param courseConfigVO
* @return
*/
public static ICourse createEmptyCourse(Identity initialAuthor, String shortTitle, String longTitle, String reDisplayName, String description, String softKey, int access, boolean membersOnly, String authors, String location, String externalId, String externalRef, String managedFlags, CourseConfigVO courseConfigVO) {
if (!StringHelper.containsNonWhitespace(reDisplayName)) {
reDisplayName = shortTitle;
}
try {
// create a repository entry
RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class);
OLATResource resource = OLATResourceManager.getInstance().createOLATResourceInstance(CourseModule.class);
RepositoryEntry addedEntry = repositoryService.create(initialAuthor, null, "-", reDisplayName, null, resource, 0);
if (StringHelper.containsNonWhitespace(softKey) && softKey.length() <= 30) {
addedEntry.setSoftkey(softKey);
}
addedEntry.setLocation(location);
addedEntry.setAuthors(authors);
addedEntry.setExternalId(externalId);
addedEntry.setExternalRef(externalRef);
addedEntry.setManagedFlagsString(managedFlags);
addedEntry.setDescription(description);
if (RepositoryEntryManagedFlag.isManaged(addedEntry, RepositoryEntryManagedFlag.membersmanagement)) {
addedEntry.setAllowToLeaveOption(RepositoryEntryAllowToLeaveOptions.never);
} else {
// default
addedEntry.setAllowToLeaveOption(RepositoryEntryAllowToLeaveOptions.atAnyTime);
}
if (membersOnly) {
addedEntry.setMembersOnly(true);
addedEntry.setAccess(RepositoryEntry.ACC_OWNERS);
} else {
addedEntry.setAccess(access);
}
addedEntry = repositoryService.update(addedEntry);
// create an empty course
CourseFactory.createCourse(addedEntry, shortTitle, longTitle, "");
return prepareCourse(addedEntry, shortTitle, longTitle, courseConfigVO);
} catch (Exception e) {
throw new WebApplicationException(e);
}
}
use of org.olat.repository.RepositoryService in project OpenOLAT by OpenOLAT.
the class RepositoryEntriesResource method importFileResource.
private RepositoryEntry importFileResource(Identity identity, File fResource, String resourcename, String displayname, String softkey, int access) {
RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class);
RepositoryHandlerFactory handlerFactory = CoreSpringFactory.getImpl(RepositoryHandlerFactory.class);
try {
RepositoryHandler handler = null;
for (String type : handlerFactory.getSupportedTypes()) {
RepositoryHandler h = handlerFactory.getRepositoryHandler(type);
ResourceEvaluation eval = h.acceptImport(fResource, fResource.getName());
if (eval != null && eval.isValid()) {
handler = h;
break;
}
}
RepositoryEntry addedEntry = null;
if (handler != null) {
Locale locale = I18nModule.getDefaultLocale();
addedEntry = handler.importResource(identity, null, displayname, "", true, locale, fResource, fResource.getName());
if (StringHelper.containsNonWhitespace(resourcename)) {
addedEntry.setResourcename(resourcename);
}
if (StringHelper.containsNonWhitespace(softkey)) {
addedEntry.setSoftkey(softkey);
}
if (access < RepositoryEntry.ACC_OWNERS || access > RepositoryEntry.ACC_USERS_GUESTS) {
addedEntry.setAccess(RepositoryEntry.ACC_OWNERS);
} else {
addedEntry.setAccess(access);
}
addedEntry = repositoryService.update(addedEntry);
}
return addedEntry;
} catch (Exception e) {
log.error("Fail to import a resource", e);
throw new WebApplicationException(e);
}
}
Aggregations