Search in sources :

Example 61 with MCRContent

use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.

the class MCRDFGLinkServlet method getMetsSource.

/**
 * Returns the mets document wrapped in a {@link MCRContent} object.
 *
 * @param job
 * @param useExistingMets
 * @return
 * @throws Exception
 */
private static MCRContent getMetsSource(MCRServletJob job, boolean useExistingMets, String derivate) throws Exception {
    MCRPath metsFile = MCRPath.getPath(derivate, "/mets.xml");
    try {
        job.getRequest().setAttribute("XSL.derivateID", derivate);
        job.getRequest().setAttribute("XSL.objectID", MCRLinkTableManager.instance().getSourceOf(derivate).iterator().next());
    } catch (Exception x) {
        LOGGER.warn("Unable to set \"XSL.objectID\" attribute to current request", x);
    }
    boolean metsExists = Files.exists(metsFile);
    if (metsExists && useExistingMets) {
        MCRContent content = new MCRPathContent(metsFile);
        content.setDocType("mets");
        return content;
    } else {
        Document mets = MCRMETSGeneratorFactory.create(metsFile.getParent()).generate().asDocument();
        return new MCRJDOMContent(mets);
    }
}
Also used : MCRPathContent(org.mycore.common.content.MCRPathContent) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) MCRPersistenceException(org.mycore.common.MCRPersistenceException)

Example 62 with MCRContent

use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.

the class MCRMETSServlet method getMetsSource.

/**
 * Returns the mets document wrapped in a {@link MCRContent} object.
 */
static MCRContent getMetsSource(MCRServletJob job, boolean useExistingMets, String derivate) throws Exception {
    MCRPath metsPath = MCRPath.getPath(derivate, "/mets.xml");
    try {
        job.getRequest().setAttribute("XSL.derivateID", derivate);
        String objectid = MCRLinkTableManager.instance().getSourceOf(derivate).iterator().next();
        if (objectid == null || objectid.length() == 0) {
            MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(derivate));
            MCRObjectID ownerID = derObj.getOwnerID();
            objectid = ownerID.toString();
        }
        job.getRequest().setAttribute("XSL.objectID", objectid);
    } catch (Exception x) {
        LOGGER.warn("Unable to set \"XSL.objectID\" attribute to current request", x);
    }
    boolean metsExists = Files.exists(metsPath);
    if (metsExists && useExistingMets) {
        MCRContent content = new MCRPathContent(metsPath);
        content.setDocType("mets");
        return content;
    } else {
        Document mets = MCRMETSGeneratorFactory.create(MCRPath.getPath(derivate, "/")).generate().asDocument();
        if (!metsExists && STORE_METS_ON_GENERATE) {
            MCRMetsSave.saveMets(mets, MCRObjectID.getInstance(derivate));
        }
        return new MCRJDOMContent(mets);
    }
}
Also used : MCRPathContent(org.mycore.common.content.MCRPathContent) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) Document(org.jdom2.Document) MCRContent(org.mycore.common.content.MCRContent) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 63 with MCRContent

use of org.mycore.common.content.MCRContent in project mycore by MyCoRe-Org.

the class MCRXMLMetadataEventHandler method handleStoreEvent.

private void handleStoreEvent(MCREvent evt, MCRBase obj) {
    String eventType = evt.getEventType();
    MCRObjectID id = obj.getId();
    try {
        switch(eventType) {
            case MCREvent.REPAIR_EVENT:
            case MCREvent.UPDATE_EVENT:
            case MCREvent.CREATE_EVENT:
                MCRBaseContent content = new MCRBaseContent(obj);
                Date modified = obj.getService().getDate(MCRObjectService.DATE_TYPE_MODIFYDATE);
                switch(eventType) {
                    case MCREvent.REPAIR_EVENT:
                        MCRContent retrieveContent = metaDataManager.retrieveContent(id);
                        if (isUptodate(retrieveContent, content)) {
                            return;
                        }
                    case MCREvent.UPDATE_EVENT:
                        metaDataManager.update(id, content, modified);
                        break;
                    case MCREvent.CREATE_EVENT:
                        metaDataManager.create(id, content, modified);
                        break;
                }
                evt.put("content", content);
                break;
            case MCREvent.DELETE_EVENT:
                metaDataManager.delete(id);
                break;
            default:
                throw new IllegalArgumentException("Invalid event type " + eventType + " for object " + id);
        }
    } catch (IOException e) {
        throw new MCRPersistenceException("Error while handling '" + eventType + "' event of '" + id + "'", e);
    }
}
Also used : MCRBaseContent(org.mycore.common.content.MCRBaseContent) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRContent(org.mycore.common.content.MCRContent) MCRPersistenceException(org.mycore.common.MCRPersistenceException) Date(java.util.Date)

Aggregations

MCRContent (org.mycore.common.content.MCRContent)63 Document (org.jdom2.Document)21 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)20 IOException (java.io.IOException)16 Element (org.jdom2.Element)13 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)11 MCRPath (org.mycore.datamodel.niofs.MCRPath)10 Test (org.junit.Test)8 MCRPathContent (org.mycore.common.content.MCRPathContent)7 MCRParameterCollector (org.mycore.common.xsl.MCRParameterCollector)6 File (java.io.File)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 JDOMException (org.jdom2.JDOMException)5 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 MCRException (org.mycore.common.MCRException)4 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)4 URL (java.net.URL)3 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)3