use of org.olat.core.util.coordinate.LockResult in project openolat by klemens.
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;
}
use of org.olat.core.util.coordinate.LockResult in project openolat by klemens.
the class LockTest method testCreateDeleteAcquire.
@Test
public void testCreateDeleteAcquire() {
// some setup
List<Identity> identities = new ArrayList<Identity>();
for (int i = 0; i < MAX_COUNT + MAX_USERS_MORE; i++) {
Identity i1 = JunitTestHelper.createAndPersistIdentityAsRndUser("lock-");
identities.add(i1);
}
dbInstance.closeSession();
Identity ident = identities.get(0);
Identity ident2 = identities.get(1);
OLATResourceable ores = OresHelper.createOLATResourceableInstanceWithoutCheck(LockTest.class.getName(), new Long(123456789));
// ------------------ test the clusterlockmanager ----------------------
// create a lock
String asset = OresHelper.createStringRepresenting(ores, "locktest");
LockImpl li = clusterLockManager.createLockImpl(asset, ident);
clusterLockManager.saveLock(li);
dbInstance.closeSession();
// find it
LockImpl l2 = clusterLockManager.findLock(asset);
assertNotNull(l2);
assertEquals(li.getKey(), l2.getKey());
// delete it
int deletedLock = clusterLockManager.deleteLock(asset, ident);
dbInstance.closeSession();
Assert.assertEquals(1, deletedLock);
// may not find it again
LockImpl l3 = clusterLockManager.findLock(asset);
assertNull(l3);
// ------------------ test the clusterlocker ----------------------
// access the cluster locker explicitely
Locker cl = clusterCoordinator.getLocker();
// acquire
LockResult res1 = cl.acquireLock(ores, ident, "abc");
assertTrue(res1.isSuccess());
dbInstance.closeSession();
// reacquire same identity (get from db)
LockResult res11 = cl.acquireLock(ores, ident, "abc");
long lock1Ac = res11.getLockAquiredTime();
assertTrue(res11.isSuccess());
assertTrue(lock1Ac > 0);
dbInstance.closeSession();
// acquire by another identity must fail
LockResult res2 = cl.acquireLock(ores, ident2, "abc");
assertFalse(res2.isSuccess());
dbInstance.closeSession();
// reacquire same identity
LockResult res3 = cl.acquireLock(ores, ident, "abc");
assertTrue(res3.isSuccess());
dbInstance.closeSession();
// make sure it is not locked anymore
boolean lo3 = cl.isLocked(ores, "abc");
assertTrue(lo3);
// test the admin
List<LockEntry> entries = cl.adminOnlyGetLockEntries();
assertEquals(1, entries.size());
LockEntry le = entries.get(0);
// must be original owner
assertEquals(le.getOwner().getName(), ident.getName());
// release lock
cl.releaseLock(res3);
dbInstance.closeSession();
// test the admin
entries = cl.adminOnlyGetLockEntries();
assertEquals(0, entries.size());
// make sure it is not locked anymore
boolean lo = cl.isLocked(ores, "abc");
assertFalse(lo);
}
use of org.olat.core.util.coordinate.LockResult in project openolat by klemens.
the class CatalogWebService method deleteCatalogEntry.
/**
* Deletes 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 httpRquest The HTTP request
* @return The response
*/
@DELETE
@Path("{path:.*}")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Response deleteCatalogEntry(@PathParam("path") List<PathSegment> path, @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 (!canAdminSubTree(ce, 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 {
catalogManager.deleteCatalogEntry(ce);
} catch (Exception e) {
throw new WebApplicationException(e);
} finally {
CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLock(lock);
}
return Response.ok().build();
}
use of org.olat.core.util.coordinate.LockResult in project openolat by klemens.
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();
}
use of org.olat.core.util.coordinate.LockResult in project openolat by klemens.
the class CatalogWebService method removeOwner.
/**
* Remove 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
*/
@DELETE
@Path("{path:.*}/owners/{identityKey}")
public Response removeOwner(@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.ok().build();
}
SecurityGroup sg = ce.getOwnerGroup();
if (sg == null) {
return Response.ok().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 {
securityManager.removeIdentityFromSecurityGroup(identity, ce.getOwnerGroup());
} catch (Exception e) {
throw new WebApplicationException(e);
} finally {
CoordinatorManager.getInstance().getCoordinator().getLocker().releaseLock(lock);
}
return Response.ok().build();
}
Aggregations