Search in sources :

Example 1 with StorageLocation

use of edu.unc.lib.boxc.persist.api.storage.StorageLocation in project box-c by UNC-Libraries.

the class AddContainerService method addContainer.

/**
 * Creates a new container as a child of the given parent using the agent principals provided.
 *
 * @param addRequest request object containing the details of the container to create
 */
public void addContainer(AddContainerRequest addRequest) {
    notNull(addRequest.getParentPid(), "A parent pid must be provided");
    notNull(addRequest.getContainerType(), "A type must be provided for the next container");
    PID parentPid = addRequest.getParentPid();
    AgentPrincipals agent = addRequest.getAgent();
    ContentContainerObject child = null;
    FedoraTransaction tx = txManager.startTransaction();
    try (Timer.Context context = timer.time()) {
        PID containerPid = PIDs.get(UUID.randomUUID().toString());
        Model containerModel = createDefaultModel();
        Resource containerResc = containerModel.createResource(containerPid.getRepositoryPath());
        containerResc.addLiteral(DcElements.title, addRequest.getLabel());
        StorageLocation storageLoc = storageLocationManager.getStorageLocation(parentPid);
        containerResc.addLiteral(Cdr.storageLocation, storageLoc.getId());
        log.debug("Adding new container to storage location {}", storageLoc.getId());
        ResourceType containerType = addRequest.getContainerType();
        // Create the appropriate container
        if (ResourceType.AdminUnit.equals(containerType)) {
            aclService.assertHasAccess("User does not have permissions to create admin units", parentPid, agent.getPrincipals(), Permission.createAdminUnit);
            child = repoObjFactory.createAdminUnit(containerPid, containerModel);
        } else if (ResourceType.Collection.equals(containerType)) {
            aclService.assertHasAccess("User does not have permissions to create collections", parentPid, agent.getPrincipals(), Permission.createCollection);
            child = repoObjFactory.createCollectionObject(containerPid, containerModel);
        } else if (ResourceType.Folder.equals(containerType)) {
            aclService.assertHasAccess("User does not have permissions to create folders", parentPid, agent.getPrincipals(), Permission.ingest);
            child = repoObjFactory.createFolderObject(containerPid, containerModel);
        } else if (ResourceType.Work.equals(containerType)) {
            aclService.assertHasAccess("User does not have permissions to create works", parentPid, agent.getPrincipals(), Permission.ingest);
            child = repoObjFactory.createWorkObject(containerPid, containerModel);
        } else {
            throw new AccessRestrictionException("User cannot add a container to object of type " + containerType);
        }
        ContentContainerObject parent = (ContentContainerObject) repoObjLoader.getRepositoryObject(parentPid);
        parent.addMember(child);
        if (addRequest.isStaffOnly() && !ResourceType.AdminUnit.equals(containerType)) {
            PatronAccessDetails accessDetails = new PatronAccessDetails();
            accessDetails.setRoles(asList(new RoleAssignment(PUBLIC_PRINC, none), new RoleAssignment(AUTHENTICATED_PRINC, none)));
            patronService.updatePatronAccess(new PatronAccessAssignmentRequest(agent, containerPid, accessDetails).withFolderCreation(true));
        } else if (ResourceType.Collection.equals(containerType)) {
            PatronAccessDetails accessDetails = new PatronAccessDetails();
            accessDetails.setRoles(asList(new RoleAssignment(PUBLIC_PRINC, canViewOriginals), new RoleAssignment(AUTHENTICATED_PRINC, canViewOriginals)));
            patronService.updatePatronAccess(new PatronAccessAssignmentRequest(agent, containerPid, accessDetails));
        }
        storeDescription(containerPid, addRequest);
        premisLoggerFactory.createPremisLogger(child).buildEvent(Premis.Creation).addImplementorAgent(AgentPids.forPerson(agent)).addEventDetail("Container added at destination " + parentPid).writeAndClose();
    } catch (Exception e) {
        tx.cancel(e);
    } finally {
        tx.close();
    }
    // Send message that the action completed
    operationsMessageSender.sendAddOperation(agent.getUsername(), Arrays.asList(parentPid), Arrays.asList(child.getPid()), null, null);
}
Also used : RoleAssignment(edu.unc.lib.boxc.auth.api.models.RoleAssignment) Resource(org.apache.jena.rdf.model.Resource) AccessRestrictionException(edu.unc.lib.boxc.auth.api.exceptions.AccessRestrictionException) PID(edu.unc.lib.boxc.model.api.ids.PID) ResourceType(edu.unc.lib.boxc.model.api.ResourceType) PatronAccessDetails(edu.unc.lib.boxc.operations.impl.acl.PatronAccessDetails) PatronAccessAssignmentRequest(edu.unc.lib.boxc.operations.impl.acl.PatronAccessAssignmentService.PatronAccessAssignmentRequest) ContentContainerObject(edu.unc.lib.boxc.model.api.objects.ContentContainerObject) AccessRestrictionException(edu.unc.lib.boxc.auth.api.exceptions.AccessRestrictionException) IOException(java.io.IOException) AgentPrincipals(edu.unc.lib.boxc.auth.api.models.AgentPrincipals) Timer(io.dropwizard.metrics5.Timer) FedoraTransaction(edu.unc.lib.boxc.fcrepo.utils.FedoraTransaction) Model(org.apache.jena.rdf.model.Model) ModelFactory.createDefaultModel(org.apache.jena.rdf.model.ModelFactory.createDefaultModel) StorageLocation(edu.unc.lib.boxc.persist.api.storage.StorageLocation)

Example 2 with StorageLocation

use of edu.unc.lib.boxc.persist.api.storage.StorageLocation in project box-c by UNC-Libraries.

the class AbstractDepositJob method getTransferSession.

protected BinaryTransferSession getTransferSession(Model depositModel) {
    Bag depositBag = depositModel.getBag(getDepositPID().getRepositoryPath());
    String destLocationId = depositBag.getProperty(Cdr.storageLocation).getString();
    StorageLocation destLocation = locationManager.getStorageLocationById(destLocationId);
    return transferService.getSession(destLocation);
}
Also used : Bag(org.apache.jena.rdf.model.Bag) StorageLocation(edu.unc.lib.boxc.persist.api.storage.StorageLocation)

Example 3 with StorageLocation

use of edu.unc.lib.boxc.persist.api.storage.StorageLocation in project box-c by UNC-Libraries.

the class BinaryTransferServiceImplIT method singleDestinationTransfer.

@Test
public void singleDestinationTransfer() throws Exception {
    PID binPid = pidMinter.mintContentPid();
    StorageLocation destination = storageManager.getStorageLocationById("loc1");
    Path sourceFile = createSourceFile(sourcePath1, "myfile.txt", "some content");
    try (BinaryTransferSession session = transferService.getSession(destination)) {
        BinaryTransferOutcome outcome = session.transfer(binPid, sourceFile.toUri());
        assertTrue(new File(outcome.getDestinationUri()).exists());
    }
}
Also used : Path(java.nio.file.Path) BinaryTransferSession(edu.unc.lib.boxc.persist.api.transfer.BinaryTransferSession) PID(edu.unc.lib.boxc.model.api.ids.PID) StorageLocation(edu.unc.lib.boxc.persist.api.storage.StorageLocation) HashedFilesystemStorageLocation(edu.unc.lib.boxc.persist.impl.storage.HashedFilesystemStorageLocation) BinaryTransferOutcome(edu.unc.lib.boxc.persist.api.transfer.BinaryTransferOutcome) File(java.io.File) Test(org.junit.Test)

Example 4 with StorageLocation

use of edu.unc.lib.boxc.persist.api.storage.StorageLocation in project box-c by UNC-Libraries.

the class StorageLocationManagerImplTest method getDefaultStorageLocationFromRepoDefault.

@Test
public void getDefaultStorageLocationFromRepoDefault() throws Exception {
    PID unitPid = makePid();
    mockAncestors(unitPid, getContentRootPid());
    helper.addStorageLocation(LOC1_ID, LOC1_NAME, LOC1_BASE);
    helper.addMapping(getContentRootPid().getId(), LOC1_ID);
    initializeManager();
    StorageLocation loc = locManager.getDefaultStorageLocation(unitPid);
    assertIsLocation1(loc);
}
Also used : PID(edu.unc.lib.boxc.model.api.ids.PID) StorageLocation(edu.unc.lib.boxc.persist.api.storage.StorageLocation) Test(org.junit.Test)

Example 5 with StorageLocation

use of edu.unc.lib.boxc.persist.api.storage.StorageLocation in project box-c by UNC-Libraries.

the class StorageLocationManagerImplTest method getDefaultStorageLocationFromCollection.

@Test
public void getDefaultStorageLocationFromCollection() throws Exception {
    PID unitPid = makePid();
    PID collPid = makePid();
    PID folderPid = makePid();
    PID workPid = makePid();
    mockAncestors(collPid, getContentRootPid(), unitPid);
    mockAncestors(workPid, getContentRootPid(), unitPid, collPid, folderPid);
    helper.addStorageLocation(LOC1_ID, LOC1_NAME, LOC1_BASE);
    helper.addStorageLocation(LOC2_ID, LOC2_NAME, LOC2_BASE);
    helper.addMapping(unitPid.getId(), LOC1_ID);
    helper.addMapping(folderPid.getId(), LOC2_ID);
    initializeManager();
    StorageLocation collLoc = locManager.getDefaultStorageLocation(collPid);
    assertIsLocation1(collLoc);
    StorageLocation workLoc = locManager.getDefaultStorageLocation(workPid);
    // Assignment past the collection should be ignored
    assertIsLocation1(workLoc);
}
Also used : PID(edu.unc.lib.boxc.model.api.ids.PID) StorageLocation(edu.unc.lib.boxc.persist.api.storage.StorageLocation) Test(org.junit.Test)

Aggregations

StorageLocation (edu.unc.lib.boxc.persist.api.storage.StorageLocation)35 PID (edu.unc.lib.boxc.model.api.ids.PID)27 Test (org.junit.Test)24 File (java.io.File)8 URI (java.net.URI)7 Resource (org.apache.jena.rdf.model.Resource)7 BinaryTransferSession (edu.unc.lib.boxc.persist.api.transfer.BinaryTransferSession)6 HashedFilesystemStorageLocation (edu.unc.lib.boxc.persist.impl.storage.HashedFilesystemStorageLocation)6 Path (java.nio.file.Path)6 Model (org.apache.jena.rdf.model.Model)6 FedoraTransaction (edu.unc.lib.boxc.fcrepo.utils.FedoraTransaction)4 CollectionObject (edu.unc.lib.boxc.model.api.objects.CollectionObject)4 Before (org.junit.Before)4 BinaryObject (edu.unc.lib.boxc.model.api.objects.BinaryObject)3 BinaryTransferOutcome (edu.unc.lib.boxc.persist.api.transfer.BinaryTransferOutcome)3 AccessRestrictionException (edu.unc.lib.boxc.auth.api.exceptions.AccessRestrictionException)2 AdminUnit (edu.unc.lib.boxc.model.api.objects.AdminUnit)2 FolderObject (edu.unc.lib.boxc.model.api.objects.FolderObject)2 StorageLocationManager (edu.unc.lib.boxc.persist.api.storage.StorageLocationManager)2 UnknownStorageLocationException (edu.unc.lib.boxc.persist.api.storage.UnknownStorageLocationException)2