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;
}
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);
}
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();
}
}
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();
}
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;
}
Aggregations