Search in sources :

Example 1 with LockResult

use of org.olat.core.util.coordinate.LockResult in project OpenOLAT by OpenOLAT.

the class IQConfigurationController method doIQReference.

private void doIQReference(UserRequest urequest, RepositoryEntry re, boolean manualCorrection) {
    // repository search controller done
    if (re != null) {
        if (CoordinatorManager.getInstance().getCoordinator().getLocker().isLocked(re.getOlatResource(), null)) {
            LockResult lockResult = CoordinatorManager.getInstance().getCoordinator().getLocker().acquireLock(re.getOlatResource(), urequest.getIdentity(), null);
            String fullName = CoreSpringFactory.getImpl(UserManager.class).getUserDisplayName(lockResult.getOwner());
            showError("error.entry.locked", fullName);
            if (lockResult.isSuccess()) {
                // improbable concurrency security
                CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLock(lockResult);
            }
        } else {
            if (editTestButton != null) {
                myContent.remove(editTestButton);
            }
            IQEditController.setIQReference(re, moduleConfiguration);
            String displayName = StringHelper.escapeHtml(re.getDisplayname());
            previewLink = LinkFactory.createCustomLink("command.preview.link", "command.preview", displayName, Link.NONTRANSLATED, myContent, this);
            previewLink.setIconLeftCSS("o_icon o_icon-fw o_icon_preview");
            previewLink.setCustomEnabledLinkCSS("o_preview");
            previewLink.setTitle(getTranslator().translate("command.preview"));
            previewButton = LinkFactory.createButtonSmall("command.preview", myContent, this);
            previewButton.setIconLeftCSS("o_icon o_icon-fw o_icon_preview");
            myContent.contextPut("dontRenderRepositoryButton", new Boolean(true));
            // If of type test, get min, max, cut - put in module config and push
            // to velocity
            boolean isOnyx = OnyxModule.isOnyxTest(re.getOlatResource());
            myContent.contextPut("isOnyx", Boolean.valueOf(isOnyx));
            if (isOnyx) {
                myContent.contextPut("onyxDisplayName", displayName);
                moduleConfiguration.set(IQEditController.CONFIG_KEY_TYPE_QTI, IQEditController.CONFIG_VALUE_QTI2);
            } else if (ImsQTI21Resource.TYPE_NAME.equals(re.getOlatResource().getResourceableTypeName())) {
                moduleConfiguration.set(IQEditController.CONFIG_KEY_TYPE_QTI, IQEditController.CONFIG_VALUE_QTI21);
                if (isEditable(urequest.getIdentity(), urequest.getUserSession().getRoles(), re)) {
                    editTestButton = LinkFactory.createButtonSmall("command.editRepFile", myContent, this);
                }
            } else {
                moduleConfiguration.set(IQEditController.CONFIG_KEY_TYPE_QTI, IQEditController.CONFIG_VALUE_QTI1);
                if (isEditable(urequest.getIdentity(), urequest.getUserSession().getRoles(), re)) {
                    editTestButton = LinkFactory.createButtonSmall("command.editRepFile", myContent, this);
                }
            }
            if (manualCorrection) {
                myContent.contextPut(IQEditController.CONFIG_CORRECTION_MODE, "manual");
            } else {
                myContent.contextPut(IQEditController.CONFIG_CORRECTION_MODE, "auto");
            }
            fireEvent(urequest, NodeEditController.NODECONFIG_CHANGED_EVENT);
        }
    }
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) UserManager(org.olat.user.UserManager)

Example 2 with LockResult

use of org.olat.core.util.coordinate.LockResult in project OpenOLAT by OpenOLAT.

the class CoursesWebService method copyCourse.

private static ICourse copyCourse(Long copyFrom, UserRequest ureq, Identity initialAuthor, String shortTitle, String longTitle, String displayName, String description, String softKey, int access, boolean membersOnly, String authors, String location, String externalId, String externalRef, String managedFlags, CourseConfigVO courseConfigVO) {
    // String learningObjectives = name + " (Example of creating a new course)";
    OLATResourceable originalOresTrans = OresHelper.createOLATResourceableInstance(CourseModule.class, copyFrom);
    RepositoryEntry src = RepositoryManager.getInstance().lookupRepositoryEntry(originalOresTrans, false);
    if (src == null) {
        src = RepositoryManager.getInstance().lookupRepositoryEntry(copyFrom, false);
    }
    if (src == null) {
        log.warn("Cannot find course to copy from: " + copyFrom);
        return null;
    }
    OLATResource originalOres = OLATResourceManager.getInstance().findResourceable(src.getOlatResource());
    boolean isAlreadyLocked = RepositoryHandlerFactory.getInstance().getRepositoryHandler(src).isLocked(originalOres);
    LockResult lockResult = RepositoryHandlerFactory.getInstance().getRepositoryHandler(src).acquireLock(originalOres, ureq.getIdentity());
    // check range of access
    if (access < 1 || access > RepositoryEntry.ACC_USERS_GUESTS) {
        access = RepositoryEntry.ACC_OWNERS;
    }
    if (lockResult == null || (lockResult != null && lockResult.isSuccess()) && !isAlreadyLocked) {
        RepositoryService repositoryService = CoreSpringFactory.getImpl(RepositoryService.class);
        // create new repo entry
        String name;
        if (description == null || description.trim().length() == 0) {
            description = src.getDescription();
        }
        if (courseConfigVO != null && StringHelper.containsNonWhitespace(displayName)) {
            name = displayName;
        } else {
            name = "Copy of " + src.getDisplayname();
        }
        String resName = src.getResourcename();
        if (resName == null) {
            resName = "";
        }
        OLATResource sourceResource = src.getOlatResource();
        OLATResource copyResource = OLATResourceManager.getInstance().createOLATResourceInstance(sourceResource.getResourceableTypeName());
        RepositoryEntry preparedEntry = repositoryService.create(initialAuthor, null, resName, name, description, copyResource, RepositoryEntry.ACC_OWNERS);
        RepositoryHandler handler = RepositoryHandlerFactory.getInstance().getRepositoryHandler(src);
        preparedEntry = handler.copy(initialAuthor, src, preparedEntry);
        preparedEntry.setCanDownload(src.getCanDownload());
        if (StringHelper.containsNonWhitespace(softKey)) {
            preparedEntry.setSoftkey(softKey);
        }
        if (StringHelper.containsNonWhitespace(externalId)) {
            preparedEntry.setExternalId(externalId);
        }
        if (StringHelper.containsNonWhitespace(externalRef)) {
            preparedEntry.setExternalRef(externalRef);
        }
        if (StringHelper.containsNonWhitespace(authors)) {
            preparedEntry.setAuthors(authors);
        }
        if (StringHelper.containsNonWhitespace(location)) {
            preparedEntry.setLocation(location);
        }
        if (StringHelper.containsNonWhitespace(managedFlags)) {
            preparedEntry.setManagedFlagsString(managedFlags);
        }
        if (membersOnly) {
            preparedEntry.setMembersOnly(true);
            preparedEntry.setAccess(RepositoryEntry.ACC_OWNERS);
        } else {
            preparedEntry.setAccess(access);
        }
        preparedEntry.setAllowToLeaveOption(src.getAllowToLeaveOption());
        repositoryService.update(preparedEntry);
        // copy image if available
        RepositoryManager.getInstance().copyImage(src, preparedEntry);
        ICourse course = prepareCourse(preparedEntry, shortTitle, longTitle, courseConfigVO);
        RepositoryHandlerFactory.getInstance().getRepositoryHandler(src).releaseLock(lockResult);
        return course;
    }
    return null;
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) OLATResourceable(org.olat.core.id.OLATResourceable) OLATResource(org.olat.resource.OLATResource) ICourse(org.olat.course.ICourse) RepositoryEntry(org.olat.repository.RepositoryEntry) RepositoryHandler(org.olat.repository.handlers.RepositoryHandler) RepositoryService(org.olat.repository.RepositoryService)

Example 3 with LockResult

use of org.olat.core.util.coordinate.LockResult in project OpenOLAT by OpenOLAT.

the class CatalogWebService method addCatalogEntry.

/**
 * Adds a catalog entry under the path specified in the URL.
 * @response.representation.qname {http://www.example.com}catalogEntryVO
 * @response.representation.mediaType application/xml, application/json
 * @response.representation.doc The catalog entry
 * @response.representation.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_CATALOGENTRYVO}
 * @response.representation.200.qname {http://www.example.com}catalogEntryVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The list of catalog entry
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_CATALOGENTRYVO}
 * @response.representation.401.doc Not authorized
 * @response.representation.404.doc The path could not be resolved to a valid catalog entry
 * @param path The path
 * @param entryVo The catalog entry
 * @param httpRquest The HTTP request
 * @param uriInfo The URI informations
 * @return The response
 */
@PUT
@Path("{path:.*}")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response addCatalogEntry(@PathParam("path") List<PathSegment> path, CatalogEntryVO entryVo, @Context HttpServletRequest httpRequest, @Context UriInfo uriInfo) {
    if (!isAuthor(httpRequest)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    Long parentKey = getCatalogEntryKeyFromPath(path);
    if (parentKey == null) {
        return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
    }
    CatalogEntry parent = catalogManager.loadCatalogEntry(parentKey);
    if (parent == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    int type = guessType(entryVo);
    if (type == CatalogEntry.TYPE_NODE && !canAdminSubTree(parent, httpRequest)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    RepositoryEntry re = null;
    if (entryVo.getRepositoryEntryKey() != null) {
        re = RepositoryManager.getInstance().lookupRepositoryEntry(entryVo.getRepositoryEntryKey());
        if (re == null) {
            return Response.serverError().status(Status.NOT_FOUND).build();
        }
    }
    Identity id = getUserRequest(httpRequest).getIdentity();
    LockResult lock = CoordinatorManager.getInstance().getCoordinator().getLocker().acquireLock(lockRes, id, LOCK_TOKEN);
    if (!lock.isSuccess()) {
        return getLockedResponse(lock, httpRequest);
    }
    CatalogEntry ce = null;
    try {
        ce = catalogManager.createCatalogEntry();
        ce.setType(guessType(entryVo));
        ce.setName(entryVo.getName());
        ce.setDescription(entryVo.getDescription());
        ce.setOwnerGroup(BaseSecurityManager.getInstance().createAndPersistSecurityGroup());
        if (re != null) {
            ce.setRepositoryEntry(re);
        }
        catalogManager.addCatalogEntry(parent, ce);
    } catch (Exception e) {
        throw new WebApplicationException(e);
    } finally {
        CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLock(lock);
    }
    CatalogEntryVO newEntryVo = link(get(ce), uriInfo);
    return Response.ok(newEntryVo).build();
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) WebApplicationException(javax.ws.rs.WebApplicationException) CatalogEntryVO(org.olat.restapi.support.vo.CatalogEntryVO) CatalogEntry(org.olat.repository.CatalogEntry) RepositoryEntry(org.olat.repository.RepositoryEntry) Identity(org.olat.core.id.Identity) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 4 with LockResult

use of org.olat.core.util.coordinate.LockResult in project OpenOLAT by OpenOLAT.

the class CatalogWebService method updateCatalogEntry.

/**
 * Updates the catalog entry with the path specified in the URL.
 * @response.representation.200.qname {http://www.example.com}catalogEntryVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The catalog entry
 * @response.representation.200.example {@link org.olat.restapi.support.vo.Examples#SAMPLE_CATALOGENTRYVO}
 * @response.representation.401.doc Not authorized
 * @response.representation.404.doc The path could not be resolved to a valid catalog entry
 * @param path The path
 * @param entryVo The catalog entry
 * @param newParentKey The parent key to move the entry (optional)
 * @param httpRquest The HTTP request
 * @param uriInfo The URI informations
 * @return The response
 */
@POST
@Path("{path:.*}")
@Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response updateCatalogEntry(@PathParam("path") List<PathSegment> path, CatalogEntryVO entryVo, @QueryParam("newParentKey") Long newParentKey, @Context HttpServletRequest httpRequest, @Context UriInfo uriInfo) {
    if (!isAuthor(httpRequest)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    Long key = getCatalogEntryKeyFromPath(path);
    if (key == null) {
        return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
    }
    CatalogEntry ce = catalogManager.loadCatalogEntry(key);
    if (ce.getType() == CatalogEntry.TYPE_NODE) {
        // check if can admin category
        if (!canAdminSubTree(ce, httpRequest)) {
            return Response.serverError().status(Status.UNAUTHORIZED).build();
        }
    }
    // fxdiff FXOLAT-122: course management
    CatalogEntry newParent = null;
    if (newParentKey != null) {
        newParent = catalogManager.loadCatalogEntry(newParentKey);
        if (newParent.getType() == CatalogEntry.TYPE_NODE) {
            // check if can admin category
            if (!canAdminSubTree(newParent, httpRequest)) {
                return Response.serverError().status(Status.UNAUTHORIZED).build();
            }
        }
    }
    Identity id = getUserRequest(httpRequest).getIdentity();
    LockResult lock = CoordinatorManager.getInstance().getCoordinator().getLocker().acquireLock(lockRes, id, LOCK_TOKEN);
    if (!lock.isSuccess()) {
        return getLockedResponse(lock, httpRequest);
    }
    try {
        ce = catalogManager.loadCatalogEntry(ce);
        if (ce == null) {
            return Response.serverError().status(Status.NOT_FOUND).build();
        }
        // only update if needed
        if (StringHelper.containsNonWhitespace(entryVo.getName())) {
            ce.setName(entryVo.getName());
        }
        if (StringHelper.containsNonWhitespace(entryVo.getDescription())) {
            ce.setDescription(entryVo.getDescription());
        }
        if (entryVo.getType() != null) {
            ce.setType(guessType(entryVo));
        }
        catalogManager.updateCatalogEntry(ce);
        if (newParent != null) {
            catalogManager.moveCatalogEntry(ce, newParent);
        }
    } catch (Exception e) {
        throw new WebApplicationException(e);
    } finally {
        CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLock(lock);
    }
    CatalogEntryVO newEntryVo = link(get(ce), uriInfo);
    return Response.ok(newEntryVo).build();
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) WebApplicationException(javax.ws.rs.WebApplicationException) CatalogEntryVO(org.olat.restapi.support.vo.CatalogEntryVO) CatalogEntry(org.olat.repository.CatalogEntry) Identity(org.olat.core.id.Identity) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 5 with LockResult

use of org.olat.core.util.coordinate.LockResult in project OpenOLAT by OpenOLAT.

the class CatalogWebService method addOwner.

/**
 * Add an owner of the local sub tree
 * @response.representation.200.qname {http://www.example.com}userVO
 * @response.representation.200.mediaType application/xml, application/json
 * @response.representation.200.doc The catalog entry
 * @response.representation.200.example {@link org.olat.user.restapi.Examples#SAMPLE_USERVOes}
 * @response.representation.401.doc Not authorized
 * @response.representation.404.doc The path could not be resolved to a valid catalog entry
 * @param path The path
 * @param identityKey The id of the user
 * @param httpRquest The HTTP request
 * @return The response
 */
@PUT
@Path("{path:.*}/owners/{identityKey}")
public Response addOwner(@PathParam("path") List<PathSegment> path, @PathParam("identityKey") Long identityKey, @Context HttpServletRequest httpRequest) {
    Long key = getCatalogEntryKeyFromPath(path);
    if (key == null) {
        return Response.serverError().status(Status.NOT_ACCEPTABLE).build();
    }
    CatalogEntry ce = catalogManager.loadCatalogEntry(key);
    if (ce == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    if (!isAuthor(httpRequest) && !canAdminSubTree(ce, httpRequest)) {
        return Response.serverError().status(Status.UNAUTHORIZED).build();
    }
    BaseSecurity securityManager = BaseSecurityManager.getInstance();
    Identity identity = securityManager.loadIdentityByKey(identityKey, false);
    if (identity == null) {
        return Response.serverError().status(Status.NOT_FOUND).build();
    }
    Identity id = getUserRequest(httpRequest).getIdentity();
    LockResult lock = CoordinatorManager.getInstance().getCoordinator().getLocker().acquireLock(lockRes, id, LOCK_TOKEN);
    if (!lock.isSuccess()) {
        return getLockedResponse(lock, httpRequest);
    }
    try {
        SecurityGroup sg = ce.getOwnerGroup();
        if (sg == null) {
            ce.setOwnerGroup(securityManager.createAndPersistSecurityGroup());
            DBFactory.getInstance().intermediateCommit();
        }
        securityManager.addIdentityToSecurityGroup(identity, ce.getOwnerGroup());
    } catch (Exception e) {
        throw new WebApplicationException(e);
    } finally {
        CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLock(lock);
    }
    return Response.ok().build();
}
Also used : LockResult(org.olat.core.util.coordinate.LockResult) WebApplicationException(javax.ws.rs.WebApplicationException) CatalogEntry(org.olat.repository.CatalogEntry) Identity(org.olat.core.id.Identity) SecurityGroup(org.olat.basesecurity.SecurityGroup) WebApplicationException(javax.ws.rs.WebApplicationException) BaseSecurity(org.olat.basesecurity.BaseSecurity) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Aggregations

LockResult (org.olat.core.util.coordinate.LockResult)28 Identity (org.olat.core.id.Identity)16 Path (javax.ws.rs.Path)14 Produces (javax.ws.rs.Produces)10 WebApplicationException (javax.ws.rs.WebApplicationException)10 CatalogEntry (org.olat.repository.CatalogEntry)10 RepositoryEntry (org.olat.repository.RepositoryEntry)10 OLATResourceable (org.olat.core.id.OLATResourceable)6 RepositoryHandler (org.olat.repository.handlers.RepositoryHandler)6 OLATResource (org.olat.resource.OLATResource)6 Consumes (javax.ws.rs.Consumes)4 DELETE (javax.ws.rs.DELETE)4 GET (javax.ws.rs.GET)4 PUT (javax.ws.rs.PUT)4 BaseSecurity (org.olat.basesecurity.BaseSecurity)4 SecurityGroup (org.olat.basesecurity.SecurityGroup)4 MediaResource (org.olat.core.gui.media.MediaResource)4 LockEntry (org.olat.core.util.coordinate.LockEntry)4 RepositoryService (org.olat.repository.RepositoryService)4 RestSecurityHelper.getIdentity (org.olat.restapi.security.RestSecurityHelper.getIdentity)4