Search in sources :

Example 46 with RepositoryService

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);
        }
    }
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) OLATResource(org.olat.resource.OLATResource) MediaResource(org.olat.core.gui.media.MediaResource) RepositoryEntry(org.olat.repository.RepositoryEntry) RepositoryHandler(org.olat.repository.handlers.RepositoryHandler) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) RepositoryService(org.olat.repository.RepositoryService) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 47 with RepositoryService

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();
}
Also used : RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) RestSecurityHelper.getIdentity(org.olat.restapi.security.RestSecurityHelper.getIdentity) RepositoryService(org.olat.repository.RepositoryService) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 48 with RepositoryService

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();
}
Also used : ErrorList(org.olat.repository.ErrorList) RepositoryEntry(org.olat.repository.RepositoryEntry) RestSecurityHelper.getUserRequest(org.olat.restapi.security.RestSecurityHelper.getUserRequest) UserRequest(org.olat.core.gui.UserRequest) RepositoryService(org.olat.repository.RepositoryService) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

Example 49 with RepositoryService

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);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) OLATResource(org.olat.resource.OLATResource) RepositoryEntry(org.olat.repository.RepositoryEntry) WebApplicationException(javax.ws.rs.WebApplicationException) RepositoryService(org.olat.repository.RepositoryService)

Example 50 with RepositoryService

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);
    }
}
Also used : ResourceEvaluation(org.olat.fileresource.types.ResourceEvaluation) Locale(java.util.Locale) WebApplicationException(javax.ws.rs.WebApplicationException) RepositoryHandlerFactory(org.olat.repository.handlers.RepositoryHandlerFactory) RepositoryHandler(org.olat.repository.handlers.RepositoryHandler) RepositoryEntry(org.olat.repository.RepositoryEntry) WebApplicationException(javax.ws.rs.WebApplicationException) RepositoryService(org.olat.repository.RepositoryService)

Aggregations

RepositoryService (org.olat.repository.RepositoryService)76 RepositoryEntry (org.olat.repository.RepositoryEntry)72 OLATResource (org.olat.resource.OLATResource)32 Identity (org.olat.core.id.Identity)20 Produces (javax.ws.rs.Produces)18 Path (javax.ws.rs.Path)16 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)14 File (java.io.File)12 OLATResourceable (org.olat.core.id.OLATResourceable)12 GET (javax.ws.rs.GET)10 UserRequest (org.olat.core.gui.UserRequest)8 WindowControl (org.olat.core.gui.control.WindowControl)8 ICourse (org.olat.course.ICourse)8 RepositoryHandler (org.olat.repository.handlers.RepositoryHandler)8 UserVO (org.olat.user.restapi.UserVO)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 Before (org.junit.Before)6 VFSContainer (org.olat.core.util.vfs.VFSContainer)6 GlossaryResource (org.olat.fileresource.types.GlossaryResource)6