Search in sources :

Example 21 with MCRDerivate

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

the class MCRMETSDefaultGenerator method generate.

@Override
public Mets generate() throws MCRException {
    try {
        Mets mets = createMets();
        MCRDerivate owner = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(getOwner()));
        mets.getLogicalStructMap().getDivContainer().setLabel(MCRTranslation.exists("MCR.Mets.LogicalStructMap.Default.Label") ? MCRTranslation.translate("MCR.Mets.LogicalStructMap.Default.Label") : owner.getId().toString());
        Map<String, String> urnFileMap = owner.getUrnMap();
        if (urnFileMap.size() > 0) {
            try {
                MCRMetsSave.updateURNsInMetsDocument(mets, urnFileMap);
            } catch (Exception e) {
                LOGGER.error("error while adding urnĀ“s to new Mets file", e);
            }
        }
        return mets;
    } catch (Exception ioExc) {
        throw new MCRException("Unable to create mets.xml of " + getOwner(), ioExc);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) URISyntaxException(java.net.URISyntaxException) MCRException(org.mycore.common.MCRException) IOException(java.io.IOException)

Example 22 with MCRDerivate

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

the class MCRMETSServlet method doGetPost.

@Override
protected void doGetPost(MCRServletJob job) throws Exception {
    HttpServletRequest request = job.getRequest();
    HttpServletResponse response = job.getResponse();
    LOGGER.info(request.getPathInfo());
    String derivate = getOwnerID(request.getPathInfo());
    MCRPath rootPath = MCRPath.getPath(derivate, "/");
    if (!Files.isDirectory(rootPath)) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, MessageFormat.format("Derivate {0} does not exist.", derivate));
        return;
    }
    request.setAttribute("XSL.derivateID", derivate);
    Collection<String> linkList = MCRLinkTableManager.instance().getSourceOf(derivate);
    if (linkList.isEmpty()) {
        MCRDerivate derivate2 = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivate));
        MCRObjectID ownerID = derivate2.getOwnerID();
        if (ownerID != null && ownerID.toString().length() != 0) {
            linkList.add(ownerID.toString());
        } else {
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, MessageFormat.format("Derivate {0} is not linked with a MCRObject. Please contact an administrator.", derivate));
            return;
        }
    }
    request.setAttribute("XSL.objectID", linkList.iterator().next());
    response.setContentType("text/xml");
    long lastModified = Files.getLastModifiedTime(rootPath).toMillis();
    MCRFrontendUtil.writeCacheHeaders(response, (long) CACHE_TIME, lastModified, useExpire);
    long start = System.currentTimeMillis();
    MCRContent metsContent = getMetsSource(job, useExistingMets(request), derivate);
    MCRLayoutService.instance().doLayout(request, response, metsContent);
    LOGGER.info("Generation of code by {} took {} ms", this.getClass().getSimpleName(), System.currentTimeMillis() - start);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRContent(org.mycore.common.content.MCRContent)

Example 23 with MCRDerivate

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

the class MCRUpdateMetsOnDerivateChangeEventHandler method checkUpdateMets.

/**
 * Checks if the mets.xml should be updated.
 *
 * @param evt the mcr event
 * @param file the file which was changed
 * @param attrs the file attributes
 * @return true if the mets shoud be updated, otherwise false
 */
protected boolean checkUpdateMets(MCREvent evt, Path file, BasicFileAttributes attrs) {
    // don't update if no MCRPath
    if (!(file instanceof MCRPath)) {
        return false;
    }
    // don't update if mets.xml is deleted
    Path fileName = file.getFileName();
    if (fileName != null && fileName.toString().equals(mets)) {
        return false;
    }
    MCRPath mcrPath = MCRPath.toMCRPath(file);
    String derivateId = mcrPath.getOwner();
    // don't update if mets.xml does not exist
    if (Files.notExists(MCRPath.getPath(derivateId, '/' + mets))) {
        return false;
    }
    // don't update if derivate or mycore object is marked for deletion
    MCRObjectID mcrDerivateId = MCRObjectID.getInstance(derivateId);
    MCRDerivate mcrDerivate = MCRMetadataManager.retrieveMCRDerivate(mcrDerivateId);
    return !MCRMarkManager.instance().isMarkedForDeletion(mcrDerivate);
}
Also used : MCRPath(org.mycore.datamodel.niofs.MCRPath) Path(java.nio.file.Path) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath)

Example 24 with MCRDerivate

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

the class MCRMetadataHistoryCommands method buildDerivateHistory.

private static Stream<MCRMetaHistoryItem> buildDerivateHistory(MCRObjectID derId, List<MCRMetadataVersion> versions) throws IOException {
    boolean exist = false;
    LogManager.getLogger().debug("Complete history rebuild of {} should be possible", derId);
    ArrayList<MCRMetaHistoryItem> items = new ArrayList<>(100);
    for (MCRMetadataVersion version : versions) {
        String user = version.getUser();
        Instant revDate = version.getDate().toInstant();
        if (version.getType() == MCRMetadataVersion.DELETED) {
            if (exist) {
                items.add(delete(derId, user, revDate));
                exist = false;
            }
        } else {
            // created or updated
            int timeOffset = 0;
            if (version.getType() == MCRMetadataVersion.CREATED && !exist) {
                items.add(create(derId, user, revDate));
                timeOffset = 1;
                exist = true;
            }
            try {
                MCRDerivate derivate = new MCRDerivate(version.retrieve().asXML());
                boolean derivateIsHidden = !derivate.getDerivate().isDisplayEnabled();
                if (derivateIsHidden && exist) {
                    items.add(delete(derId, user, revDate.plusMillis(timeOffset)));
                    exist = false;
                } else if (!derivateIsHidden && !exist) {
                    items.add(create(derId, user, revDate.plusMillis(timeOffset)));
                    exist = true;
                }
            } catch (JDOMException | SAXException e) {
                LogManager.getLogger().error("Error while reading revision {} of {}", version.getRevision(), derId, e);
            }
        }
    }
    return items.stream();
}
Also used : MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) Instant(java.time.Instant) ArrayList(java.util.ArrayList) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) JDOMException(org.jdom2.JDOMException) SAXException(org.xml.sax.SAXException)

Example 25 with MCRDerivate

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

the class MCRMetadataHistoryCommands method buildSimpleDerivateHistory.

private static Stream<MCRMetaHistoryItem> buildSimpleDerivateHistory(MCRObjectID derId) throws IOException {
    boolean exist = false;
    LogManager.getLogger().debug("Store of {} has no old revisions. History rebuild is limited", derId);
    if (MCRMetadataManager.exists(derId)) {
        MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(derId);
        Instant lastModified = Instant.ofEpochMilli(MCRXMLMetadataManager.instance().getLastModified(derId));
        String creator;
        try {
            creator = MCRCreatorCache.getCreator(der.getId());
        } catch (ExecutionException e) {
            LogManager.getLogger().warn("Error while getting creator of {}", derId, e);
            creator = null;
        }
        String user = Optional.ofNullable(creator).orElseGet(() -> MCRSystemUserInformation.getSystemUserInstance().getUserID());
        MCRMetaHistoryItem create = create(derId, user, lastModified);
        exist = true;
        boolean objectIsHidden = !der.getDerivate().isDisplayEnabled();
        if (objectIsHidden && exist) {
            return Stream.of(create, delete(derId, user, lastModified.plusMillis(1)));
        }
        return Stream.of(create);
    } else {
        return Stream.of(delete(derId, null, Instant.now()));
    }
}
Also used : Instant(java.time.Instant) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) ExecutionException(java.util.concurrent.ExecutionException)

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