Search in sources :

Example 6 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRURNGranularRESTRegistrationService method registerURN.

private MCRDNBURN registerURN(MCRDerivate deriv, String filePath) {
    MCRObjectID derivID = deriv.getId();
    Function<String, Integer> countCreatedPI = s -> MCRPersistentIdentifierManager.getInstance().getCreatedIdentifiers(derivID, getType(), getRegistrationServiceID()).size();
    int seed = Optional.of(filePath).filter(p -> !"".equals(p)).map(countCreatedPI).map(count -> count + 1).orElse(1);
    MCRDNBURN derivURN = Optional.ofNullable(deriv.getDerivate()).map(MCRObjectDerivate::getURN).flatMap(new MCRDNBURNParser()::parse).orElseGet(() -> createNewURN(deriv));
    String setID = derivID.getNumberAsString();
    GranularURNGenerator granularURNGen = new GranularURNGenerator(seed, derivURN, setID);
    Function<MCRPath, Supplier<String>> generateURN = p -> granularURNGen.getURNSupplier();
    derivateFileStream.apply(deriv).filter(notInIgnoreList().and(matchFile(filePath))).sorted().collect(Collectors.toMap(generateURN, p -> p, (m1, m2) -> m1, LinkedHashMap::new)).forEach(createFileMetadata(deriv).andThen(persistURN(deriv)));
    try {
        MCRMetadataManager.update(deriv);
    } catch (MCRPersistenceException | MCRAccessException e) {
        LOGGER.error("Error while updating derivate {}", derivID, e);
    }
    EntityTransaction transaction = MCREntityManagerProvider.getCurrentEntityManager().getTransaction();
    if (!transaction.isActive()) {
        transaction.begin();
    }
    transaction.commit();
    return derivURN;
}
Also used : MCRBase(org.mycore.datamodel.metadata.MCRBase) Arrays(java.util.Arrays) Date(java.util.Date) Function(java.util.function.Function) Supplier(java.util.function.Supplier) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) ArrayList(java.util.ArrayList) MCRDNBURN(org.mycore.pi.urn.MCRDNBURN) LinkedHashMap(java.util.LinkedHashMap) MCRPI(org.mycore.pi.backend.MCRPI) Locale(java.util.Locale) BiConsumer(java.util.function.BiConsumer) MCRAccessException(org.mycore.access.MCRAccessException) Path(java.nio.file.Path) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRPersistenceException(org.mycore.common.MCRPersistenceException) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) MCRPIRegistrationService(org.mycore.pi.MCRPIRegistrationService) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) EntityTransaction(javax.persistence.EntityTransaction) Optional(java.util.Optional) MCRPersistentIdentifierManager(org.mycore.pi.MCRPersistentIdentifierManager) Pattern(java.util.regex.Pattern) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRDNBURNParser(org.mycore.pi.urn.MCRDNBURNParser) LogManager(org.apache.logging.log4j.LogManager) EntityTransaction(javax.persistence.EntityTransaction) MCRDNBURN(org.mycore.pi.urn.MCRDNBURN) MCRAccessException(org.mycore.access.MCRAccessException) MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) MCRDNBURNParser(org.mycore.pi.urn.MCRDNBURNParser) LinkedHashMap(java.util.LinkedHashMap) Supplier(java.util.function.Supplier) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 7 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRPersistentIdentifierManagerTest method testRegistrationService.

@Test
public void testRegistrationService() throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
    MockMetadataManager mockMetadataManager = new MockMetadataManager();
    MCRPIRegistrationService<MCRMockIdentifier> registrationService = MCRPIRegistrationServiceManager.getInstance().getRegistrationService(MOCK_SERVICE);
    MCRMockIdentifierRegistrationService casted = (MCRMockIdentifierRegistrationService) registrationService;
    Assert.assertFalse("Delete should not have been called!", casted.isDeleteCalled());
    Assert.assertFalse("Register should not have been called!", casted.isRegisterCalled());
    Assert.assertFalse("Update should not have been called!", casted.isUpdatedCalled());
    MCRObject mcrObject = buildMockObject();
    mockMetadataManager.put(mcrObject.getId(), mcrObject);
    MCRMockIdentifier identifier = registrationService.register(mcrObject, "", true);
    Assert.assertFalse("Delete should not have been called!", casted.isDeleteCalled());
    Assert.assertTrue("The identifier " + identifier.asString() + " should be registered now!", registrationService.isCreated(mcrObject.getId(), ""));
    registrationService.onUpdate(identifier, mcrObject, "");
    Assert.assertFalse("Delete should not have been called!", casted.isDeleteCalled());
    Assert.assertTrue("The identifier " + identifier.asString() + " should have been updated!", casted.isUpdatedCalled());
    registrationService.onDelete(identifier, mcrObject, "");
    Assert.assertFalse("The identifier " + identifier.asString() + " should not be registered now!", registrationService.isCreated(mcrObject.getId(), ""));
    Assert.assertTrue("There should be one resolver", MCRPersistentIdentifierManager.getInstance().getResolvers().stream().filter(r -> r.getName().equals(MCRMockResolver.NAME)).count() > 0);
}
Also used : MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) Test(org.junit.Test) Field(java.lang.reflect.Field) MCRPI(org.mycore.pi.backend.MCRPI) Stream(java.util.stream.Stream) Rule(org.junit.Rule) MCRAccessBaseImpl(org.mycore.access.MCRAccessBaseImpl) MCRStoreTestCase(org.mycore.common.MCRStoreTestCase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) Map(java.util.Map) After(org.junit.After) MCRObject(org.mycore.datamodel.metadata.MCRObject) Optional(java.util.Optional) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) Assert(org.junit.Assert) MCRAccessException(org.mycore.access.MCRAccessException) TemporaryFolder(org.junit.rules.TemporaryFolder) Before(org.junit.Before) MCRObject(org.mycore.datamodel.metadata.MCRObject) Test(org.junit.Test)

Example 8 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRIIIFImageResource method getInfo.

@GET
@Produces(MCRIIIFMediaType.APPLICATION_LD_JSON)
@Path("{" + IDENTIFIER_PARAM + "}/info.json")
public Response getInfo(@PathParam(IMPL_PARAM) String implString, @PathParam(IDENTIFIER_PARAM) String identifier) {
    try {
        MCRIIIFImageImpl impl = getImpl(implString);
        MCRIIIFImageInformation information = impl.getInformation(identifier);
        MCRIIIFImageProfile profile = getProfile(impl);
        information.profile.add(IIIF_IMAGE_API_2_LEVEL2);
        information.profile.add(profile);
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        return Response.ok().header("Access-Control-Allow-Origin", "*").header("Link", buildCanonicalURL(impl, identifier)).header("Profile", buildProfileURL()).entity(gson.toJson(information)).build();
    } catch (MCRIIIFImageNotFoundException e) {
        return Response.status(Response.Status.NOT_FOUND).entity(e.getMessage()).build();
    } catch (MCRIIIFImageProvidingException | UnsupportedEncodingException e) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
    } catch (MCRAccessException e) {
        return Response.status(Response.Status.FORBIDDEN).entity(e.getMessage()).build();
    }
}
Also used : MCRIIIFImageNotFoundException(org.mycore.iiif.image.impl.MCRIIIFImageNotFoundException) MCRIIIFImageProfile(org.mycore.iiif.image.model.MCRIIIFImageProfile) GsonBuilder(com.google.gson.GsonBuilder) MCRIIIFImageInformation(org.mycore.iiif.image.model.MCRIIIFImageInformation) MCRAccessException(org.mycore.access.MCRAccessException) Gson(com.google.gson.Gson) MCRIIIFImageProvidingException(org.mycore.iiif.image.impl.MCRIIIFImageProvidingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MCRIIIFImageImpl(org.mycore.iiif.image.impl.MCRIIIFImageImpl) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 9 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRPersistentIdentifierRegistrationResource method register.

@POST
@Path("service/{serviceName}/{mycoreId}")
@Produces(MediaType.APPLICATION_JSON)
public Response register(@PathParam("serviceName") String serviceName, @PathParam("mycoreId") String mycoreId, @DefaultValue("") @QueryParam("additional") String additional) {
    if (!MCRPIRegistrationServiceManager.getInstance().getServiceIDList().contains(serviceName)) {
        return Response.status(Response.Status.BAD_REQUEST).entity(buildErrorJSON("No Registration Service found for " + serviceName)).build();
    }
    MCRPIRegistrationService<MCRPersistentIdentifier> registrationService = MCRPIRegistrationServiceManager.getInstance().getRegistrationService(serviceName);
    MCRObjectID mycoreIDObject;
    try {
        mycoreIDObject = MCRObjectID.getInstance(mycoreId);
    } catch (MCRException e) {
        LOGGER.error(e);
        return Response.status(Response.Status.BAD_REQUEST).entity(buildErrorJSON("The provided id " + mycoreId + " seems to be invalid!", e)).build();
    }
    MCRPersistentIdentifier identifier;
    MCRBase object = MCRMetadataManager.retrieve(mycoreIDObject);
    try {
        identifier = registrationService.register(object, additional, true);
    } catch (MCRPersistentIdentifierException | MCRActiveLinkException e) {
        LOGGER.error(e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(buildErrorJSON("Error while register new identifier!", e)).build();
    } catch (MCRAccessException e) {
        LOGGER.error(e);
        return Response.status(Response.Status.FORBIDDEN).entity(buildErrorJSON("Error while register new identifier!", e)).build();
    }
    return Response.status(Response.Status.CREATED).entity(buildIdentifierObject(identifier)).build();
}
Also used : MCRException(org.mycore.common.MCRException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRAccessException(org.mycore.access.MCRAccessException) MCRBase(org.mycore.datamodel.metadata.MCRBase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistentIdentifier(org.mycore.pi.MCRPersistentIdentifier) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 10 with MCRAccessException

use of org.mycore.access.MCRAccessException in project mycore by MyCoRe-Org.

the class MCRUpdateDerivateServlet method updateDerivateXML.

/**
 * Updates derivate xml in the persistence backend
 * @param editorSubmission
 *  MyCoRe derivate as XML
 * @return
 *  MCRObjectID of the MyCoRe object
 * @throws SAXParseException
 * @throws MCRAccessException
 */
private MCRObjectID updateDerivateXML(Document editorSubmission) throws SAXParseException, IOException, MCRAccessException {
    MCRObjectID objectID;
    Element root = editorSubmission.getRootElement();
    root.setAttribute("noNamespaceSchemaLocation", "datamodel-derivate.xsd", XSI_NAMESPACE);
    root.addNamespaceDeclaration(XLINK_NAMESPACE);
    root.addNamespaceDeclaration(XSI_NAMESPACE);
    byte[] xml = new MCRJDOMContent(editorSubmission).asByteArray();
    MCRDerivate der = new MCRDerivate(xml, true);
    MCRObjectID derivateID = der.getId();
    // store entry of derivate xlink:title in object
    objectID = der.getDerivate().getMetaLink().getXLinkHrefID();
    MCRObject obj = MCRMetadataManager.retrieveMCRObject(objectID);
    MCRObjectStructure structure = obj.getStructure();
    MCRMetaLinkID linkID = structure.getDerivateLink(derivateID);
    linkID.setXLinkTitle(der.getLabel());
    try {
        MCRMetadataManager.update(obj);
        MCRMetadataManager.update(der);
    } catch (MCRPersistenceException | MCRAccessException e) {
        throw new MCRPersistenceException("Can't store label of derivate " + derivateID + " in derivate list of object " + objectID + ".", e);
    }
    return objectID;
}
Also used : MCRObjectStructure(org.mycore.datamodel.metadata.MCRObjectStructure) MCRObject(org.mycore.datamodel.metadata.MCRObject) Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRAccessException(org.mycore.access.MCRAccessException) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Aggregations

MCRAccessException (org.mycore.access.MCRAccessException)23 MCRPersistenceException (org.mycore.common.MCRPersistenceException)12 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)12 IOException (java.io.IOException)11 MCRException (org.mycore.common.MCRException)9 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)9 MCRPath (org.mycore.datamodel.niofs.MCRPath)7 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)6 MCRObject (org.mycore.datamodel.metadata.MCRObject)6 Path (java.nio.file.Path)4 JDOMException (org.jdom2.JDOMException)4 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)4 SAXException (org.xml.sax.SAXException)4 List (java.util.List)3 Stream (java.util.stream.Stream)3 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)3 SignedJWT (com.nimbusds.jwt.SignedJWT)2 File (java.io.File)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URISyntaxException (java.net.URISyntaxException)2