Search in sources :

Example 51 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRRestAPIObjectsHelper method showMCRObject.

public static Response showMCRObject(String pathParamId, String queryParamStyle, UriInfo info, HttpServletRequest request) throws MCRRestAPIException {
    MCRObject mcrObj = retrieveMCRObject(pathParamId);
    Document doc = mcrObj.createXML();
    Element eStructure = doc.getRootElement().getChild("structure");
    if (queryParamStyle != null && !MCRRestAPIObjects.STYLE_DERIVATEDETAILS.equals(queryParamStyle)) {
        throw new MCRRestAPIException(Response.Status.BAD_REQUEST, new MCRRestAPIError(MCRRestAPIError.CODE_WRONG_PARAMETER, "The value of parameter {style} is not allowed.", "Allowed values for {style} parameter are: " + MCRRestAPIObjects.STYLE_DERIVATEDETAILS));
    }
    if (MCRRestAPIObjects.STYLE_DERIVATEDETAILS.equals(queryParamStyle) && eStructure != null) {
        Element eDerObjects = eStructure.getChild("derobjects");
        if (eDerObjects != null) {
            for (Element eDer : eDerObjects.getChildren("derobject")) {
                String derID = eDer.getAttributeValue("href", MCRConstants.XLINK_NAMESPACE);
                try {
                    MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derID));
                    eDer.addContent(der.createXML().getRootElement().detach());
                    // <mycorederivate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink" xsi:noNamespaceSchemaLocation="datamodel-derivate.xsd" ID="cpr_derivate_00003760" label="display_image" version="1.3">
                    // <derivate display="true">
                    eDer = eDer.getChild("mycorederivate").getChild("derivate");
                    Document docContents = listDerivateContentAsXML(MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derID)), "/", -1, info);
                    if (docContents.hasRootElement()) {
                        eDer.addContent(docContents.getRootElement().detach());
                    }
                } catch (MCRException e) {
                    eDer.addContent(new Comment("Error: Derivate not found."));
                } catch (IOException e) {
                    eDer.addContent(new Comment("Error: Derivate content could not be listed: " + e.getMessage()));
                }
            }
        }
    }
    StringWriter sw = new StringWriter();
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
    try {
        String filter_id = MCRConfiguration.instance().getString("MCR.RestAPI.v1.Filter.XML", "");
        if (filter_id.length() > 0) {
            MCRContentTransformer trans = MCRContentTransformerFactory.getTransformer(filter_id);
            Document filtered_doc = trans.transform(new MCRJDOMContent(doc)).asXML();
            outputter.output(filtered_doc, sw);
        } else {
            outputter.output(doc, sw);
        }
    } catch (SAXException | JDOMException e) {
        throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, "Unable to transform MCRContent to XML document", e.getMessage()));
    } catch (IOException e) {
        throw new MCRRestAPIException(Response.Status.INTERNAL_SERVER_ERROR, new MCRRestAPIError(MCRRestAPIError.CODE_INTERNAL_ERROR, "Unable to retrieve/transform MyCoRe object", e.getMessage()));
    }
    String authHeader = MCRJSONWebTokenUtil.createJWTAuthorizationHeader(MCRJSONWebTokenUtil.retrieveAuthenticationToken(request));
    return Response.ok(sw.toString()).type("application/xml").header(HEADER_NAME_AUTHORIZATION, authHeader).build();
}
Also used : Comment(org.jdom2.Comment) XMLOutputter(org.jdom2.output.XMLOutputter) MCRRestAPIException(org.mycore.restapi.v1.errors.MCRRestAPIException) MCRException(org.mycore.common.MCRException) Element(org.jdom2.Element) MCRRestAPIError(org.mycore.restapi.v1.errors.MCRRestAPIError) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IOException(java.io.IOException) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) SAXException(org.xml.sax.SAXException) MCRObject(org.mycore.datamodel.metadata.MCRObject) StringWriter(java.io.StringWriter) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer)

Example 52 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRURNGranularRESTRegistrationServiceTest method fullRegister.

@Test
public void fullRegister() throws Exception {
    new MockContentTypes();
    new MockFrontendUtil();
    new MockFiles();
    new MockAccessManager();
    new MockObjectDerivate();
    new MockDerivate();
    MockMetadataManager mockMetadataManager = new MockMetadataManager();
    MCRDerivate derivate = new MCRDerivate();
    MCRObjectID mcrObjectID = MCRPIUtils.getNextFreeID();
    derivate.setId(mcrObjectID);
    mockMetadataManager.put(mcrObjectID, derivate);
    Function<MCRDerivate, Stream<MCRPath>> foo = deriv -> IntStream.iterate(0, i -> i + 1).mapToObj(i -> "/foo/" + UUID.randomUUID() + "_" + String.format(Locale.getDefault(), "%02d", i)).map(f -> MCRPath.getPath(derivate.getId().toString(), f)).limit(numOfDerivFiles);
    String serviceID = "TestService";
    MCRURNGranularRESTRegistrationService testService = new MCRURNGranularRESTRegistrationService(serviceID, foo);
    testService.register(derivate, "", true);
    timerTask();
    List<MCRPIRegistrationInfo> registeredURNs = MCREntityManagerProvider.getEntityManagerFactory().createEntityManager().createNamedQuery("Get.PI.Created", MCRPIRegistrationInfo.class).setParameter("mcrId", mcrObjectID.toString()).setParameter("type", MCRDNBURN.TYPE).setParameter("service", serviceID).getResultList();
    Assert.assertEquals("Wrong number of registered URNs: ", numOfDerivFiles + 1, registeredURNs.size());
}
Also used : IntStream(java.util.stream.IntStream) Date(java.util.Date) MCRUUIDURNGenerator(org.mycore.pi.urn.MCRUUIDURNGenerator) Function(java.util.function.Function) MCRException(org.mycore.common.MCRException) TreeSet(java.util.TreeSet) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRAccessManager(org.mycore.access.MCRAccessManager) MCRDNBURN(org.mycore.pi.urn.MCRDNBURN) LinkOption(java.nio.file.LinkOption) Locale(java.util.Locale) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS) Map(java.util.Map) After(org.junit.After) Mock(mockit.Mock) MCRFileMetadata(org.mycore.datamodel.metadata.MCRFileMetadata) Path(java.nio.file.Path) MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) MockUp(mockit.MockUp) MCRContentTypes(org.mycore.datamodel.niofs.MCRContentTypes) Files(java.nio.file.Files) MCRPath(org.mycore.datamodel.niofs.MCRPath) IOException(java.io.IOException) Test(org.junit.Test) MCRPIRegistrationInfo(org.mycore.pi.MCRPIRegistrationInfo) UUID(java.util.UUID) MCRFrontendUtil(org.mycore.frontend.MCRFrontendUtil) TimeUnit(java.util.concurrent.TimeUnit) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) List(java.util.List) Stream(java.util.stream.Stream) MCRStoreTestCase(org.mycore.common.MCRStoreTestCase) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MockMetadataManager(org.mycore.pi.MockMetadataManager) MCRPIUtils(org.mycore.pi.MCRPIUtils) Assert(org.junit.Assert) MCRPIRegistrationInfo(org.mycore.pi.MCRPIRegistrationInfo) MockMetadataManager(org.mycore.pi.MockMetadataManager) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) IntStream(java.util.stream.IntStream) Stream(java.util.stream.Stream) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) Test(org.junit.Test)

Example 53 with MCRDerivate

use of org.mycore.datamodel.metadata.MCRDerivate in project mycore by MyCoRe-Org.

the class MCRURNGranularOAIRegistrationService method register.

@Override
public MCRDNBURN register(MCRBase obj, String additional, boolean updateObject) throws MCRAccessException, MCRActiveLinkException, MCRPersistentIdentifierException {
    this.validateRegistration(obj, additional);
    MCRObjectDerivate derivate = ((MCRDerivate) obj).getDerivate();
    MCRDNBURN newURN;
    if (additional.equals("")) {
        /* Multiple URN for entire Derivate...  */
        newURN = registerURNsDerivate(obj, additional, derivate);
    } else {
        /* Single URN to one File... */
        newURN = registerSingleURN(obj, additional, derivate);
    }
    try {
        MCRMetadataManager.update(obj);
    } catch (Exception e) {
        throw new MCRPersistentIdentifierException("Error while updating derivate " + obj.getId(), e);
    }
    return newURN;
}
Also used : MCRObjectDerivate(org.mycore.datamodel.metadata.MCRObjectDerivate) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) MCRAccessException(org.mycore.access.MCRAccessException) MCRPersistentIdentifierException(org.mycore.pi.exceptions.MCRPersistentIdentifierException) IOException(java.io.IOException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException)

Aggregations

MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)53 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)27 IOException (java.io.IOException)22 MCRPath (org.mycore.datamodel.niofs.MCRPath)19 MCRObject (org.mycore.datamodel.metadata.MCRObject)13 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)10 MCRAccessException (org.mycore.access.MCRAccessException)9 Path (java.nio.file.Path)8 MCRException (org.mycore.common.MCRException)8 MCRRestAPIException (org.mycore.restapi.v1.errors.MCRRestAPIException)8 Document (org.jdom2.Document)7 MCRPersistenceException (org.mycore.common.MCRPersistenceException)7 Date (java.util.Date)6 JDOMException (org.jdom2.JDOMException)6 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)6 ArrayList (java.util.ArrayList)5 MCRMetaIFS (org.mycore.datamodel.metadata.MCRMetaIFS)5 MCRObjectDerivate (org.mycore.datamodel.metadata.MCRObjectDerivate)5 SAXException (org.xml.sax.SAXException)5 StringWriter (java.io.StringWriter)4