Search in sources :

Example 11 with MCRDerivate

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

the class MCRDerivateCommands method exportDerivate.

/**
 * @param dir
 * @param trans
 * @param nid
 * @throws FileNotFoundException
 * @throws TransformerException
 * @throws IOException
 */
private static void exportDerivate(File dir, Transformer trans, String nid) throws TransformerException, IOException {
    // store the XML file
    Document xml = null;
    MCRDerivate obj;
    MCRObjectID derivateID = MCRObjectID.getInstance(nid);
    try {
        obj = MCRMetadataManager.retrieveMCRDerivate(derivateID);
        String path = obj.getDerivate().getInternals().getSourcePath();
        // reset from the absolute to relative path, for later reload
        LOGGER.info("Old Internal Path ====>{}", path);
        obj.getDerivate().getInternals().setSourcePath(nid);
        LOGGER.info("New Internal Path ====>{}", nid);
        // add ACL's
        Collection<String> l = ACCESS_IMPL.getPermissionsForID(nid);
        for (String permission : l) {
            Element rule = ACCESS_IMPL.getRule(nid, permission);
            obj.getService().addRule(permission, rule);
        }
        // build JDOM
        xml = obj.createXML();
    } catch (MCRException ex) {
        LOGGER.warn("Could not read {}, continue with next ID", nid);
        return;
    }
    File xmlOutput = new File(dir, derivateID + ".xml");
    FileOutputStream out = new FileOutputStream(xmlOutput);
    dir = new File(dir, derivateID.toString());
    if (trans != null) {
        trans.setParameter("dirname", dir.getPath());
        StreamResult sr = new StreamResult(out);
        trans.transform(new org.jdom2.transform.JDOMSource(xml), sr);
    } else {
        new org.jdom2.output.XMLOutputter().output(xml, out);
        out.flush();
        out.close();
    }
    LOGGER.info("Object {} stored under {}.", nid, xmlOutput);
    // store the derivate file under dirname
    if (!dir.isDirectory()) {
        dir.mkdir();
    }
    MCRPath rootPath = MCRPath.getPath(derivateID.toString(), "/");
    Files.walkFileTree(rootPath, new MCRTreeCopier(rootPath, dir.toPath()));
    LOGGER.info("Derivate {} saved under {} and {}.", nid, dir, xmlOutput);
}
Also used : MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRException(org.mycore.common.MCRException) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.jdom2.Element) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) Document(org.jdom2.Document) FileOutputStream(java.io.FileOutputStream) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) File(java.io.File)

Example 12 with MCRDerivate

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

the class MCRDerivateCommands method linkDerivateToObject.

/**
 * Links the given derivate to the given object.
 */
@MCRCommand(syntax = "link derivate {0} to {1}", help = "links the given derivate {0} to the given mycore object {1}", order = 180)
public static void linkDerivateToObject(String derivateId, String objectId) throws Exception {
    if (derivateId == null || objectId == null) {
        LOGGER.error("Either derivate id or object id is null. Derivate={}, object={}", derivateId, objectId);
        return;
    }
    MCRObjectID derID = MCRObjectID.getInstance(derivateId);
    MCRObjectID objID = MCRObjectID.getInstance(objectId);
    if (!MCRMetadataManager.exists(objID)) {
        throw new Exception("The object with id " + objID + " does not exist");
    }
    if (!MCRMetadataManager.exists(derID)) {
        throw new Exception("The derivate with id " + derID + " does not exist");
    }
    MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(derID);
    MCRMetaLinkID oldDerivateToObjectLink = derObj.getDerivate().getMetaLink();
    MCRObjectID oldOwnerId = oldDerivateToObjectLink.getXLinkHrefID();
    /* set link to new parent in the derivate object */
    LOGGER.info("Setting {} as parent for derivate {}", objID, derID);
    derObj.getDerivate().getMetaLink().setReference(objID, oldDerivateToObjectLink.getXLinkLabel(), oldDerivateToObjectLink.getXLinkTitle());
    derObj.setLabel("data object from " + objectId + " (prev. owner was " + oldOwnerId);
    MCRMetadataManager.updateMCRDerivateXML(derObj);
    /* set link to derivate in the new parent */
    MCRObject oldOwner = MCRMetadataManager.retrieveMCRObject(oldOwnerId);
    List<MCRMetaLinkID> derivates = oldOwner.getStructure().getDerivates();
    MCRMetaLinkID oldObjectToDerivateLink = null;
    for (MCRMetaLinkID derivate : derivates) {
        if (derivate.getXLinkHrefID().equals(derID)) {
            oldObjectToDerivateLink = derivate;
        }
    }
    if (oldObjectToDerivateLink == null) {
        oldObjectToDerivateLink = new MCRMetaLinkID();
    }
    LOGGER.info("Linking derivate {} to {}", derID, objID);
    MCRMetaLinkID derivateLink = new MCRMetaLinkID();
    derivateLink.setReference(derID, oldObjectToDerivateLink.getXLinkLabel(), oldObjectToDerivateLink.getXLinkTitle());
    derivateLink.setSubTag("derobject");
    MCRMetadataManager.addOrUpdateDerivateToObject(objID, derivateLink);
    /* removing link from old parent */
    boolean flag = oldOwner.getStructure().removeDerivate(derID);
    LOGGER.info("Unlinking derivate {} from object {}. Success={}", derID, oldOwnerId, flag);
    MCRMetadataManager.fireUpdateEvent(oldOwner);
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) JDOMException(org.jdom2.JDOMException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) TransformerException(javax.xml.transform.TransformerException) MCRException(org.mycore.common.MCRException) MCRAccessException(org.mycore.access.MCRAccessException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 13 with MCRDerivate

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

the class MCRDerivateCommands method checkObjectsInDerivates.

/**
 * Check the object links in derivates of MCR base ID for existing. It looks to the XML store on the disk to get all object IDs.
 *
 * @param base_id
 *            the base part of a MCRObjectID e.g. DocPortal_derivate
 */
@MCRCommand(syntax = "check object entries in derivates for base {0}", help = "check in all derivates of MCR base ID {0} for existing linked objects", order = 400)
public static void checkObjectsInDerivates(String base_id) throws IOException {
    if (base_id == null || base_id.length() == 0) {
        LOGGER.error("Base ID missed for check object entries in derivates for base {0}");
        return;
    }
    int project_part_position = base_id.indexOf('_');
    if (project_part_position == -1) {
        LOGGER.error("The given base ID {} has not the syntax of project_type", base_id);
        return;
    }
    MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
    List<String> id_list = mgr.listIDsForBase(base_id.substring(0, project_part_position + 1) + "derivate");
    int counter = 0;
    int maxresults = id_list.size();
    for (String derid : id_list) {
        counter++;
        LOGGER.info("Processing dataset {} from {} with ID: {}", counter, maxresults, derid);
        // get from data
        MCRObjectID mcrderid = MCRObjectID.getInstance(derid);
        MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(mcrderid);
        MCRObjectID objid = der.getOwnerID();
        if (!mgr.exists(objid)) {
            LOGGER.error("   !!! Missing object {} in database for derivate ID {}", objid, mcrderid);
        }
    }
    LOGGER.info("Check done for {} entries", Integer.toString(counter));
}
Also used : MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 14 with MCRDerivate

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

the class MCRObjectCommands method xslt.

private static void xslt(String objectId, String xslFilePath, boolean force) throws IOException, JDOMException, SAXException, URISyntaxException, TransformerException, MCRPersistenceException, MCRAccessException, ParserConfigurationException {
    File xslFile = new File(xslFilePath);
    URL xslURL;
    if (!xslFile.exists()) {
        try {
            xslURL = new URL(xslFilePath);
        } catch (MalformedURLException e) {
            LOGGER.error("XSL parameter is not a file or URL: {}", xslFilePath);
            return;
        }
    } else {
        xslURL = xslFile.toURI().toURL();
    }
    MCRObjectID mcrId = MCRObjectID.getInstance(objectId);
    Document document = MCRXMLMetadataManager.instance().retrieveXML(mcrId);
    // do XSL transform
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setErrorListener(MCRErrorListener.getInstance());
    transformerFactory.setURIResolver(MCRURIResolver.instance());
    XMLReader xmlReader = MCRXMLParserFactory.getNonValidatingParser().getXMLReader();
    xmlReader.setEntityResolver(MCREntityResolver.instance());
    SAXSource styleSource = new SAXSource(xmlReader, new InputSource(xslURL.toURI().toString()));
    Transformer transformer = transformerFactory.newTransformer(styleSource);
    for (Entry<String, String> property : MCRConfiguration.instance().getPropertiesMap().entrySet()) {
        transformer.setParameter(property.getKey(), property.getValue());
    }
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    JDOMResult result = new JDOMResult();
    transformer.transform(new JDOMSource(document), result);
    Document resultDocument = Objects.requireNonNull(result.getDocument(), "Could not get transformation result");
    String originalName = document.getRootElement().getName();
    String resultName = resultDocument.getRootElement().getName();
    if (!force && !originalName.equals(resultName)) {
        LOGGER.error("{}: root name '{}' does not match result name '{}'.", objectId, originalName, resultName);
        return;
    }
    // update on diff
    if (MCRXMLHelper.deepEqual(document, resultDocument)) {
        return;
    }
    if (resultName.equals(MCRObject.ROOT_NAME)) {
        MCRMetadataManager.update(new MCRObject(resultDocument));
    } else if (resultName.equals(MCRDerivate.ROOT_NAME)) {
        MCRMetadataManager.update(new MCRDerivate(resultDocument));
    } else {
        LOGGER.error("Unable to transform '{}' because unknown result root name '{}'.", objectId, resultName);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) InputSource(org.xml.sax.InputSource) JDOMResult(org.jdom2.transform.JDOMResult) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) JDOMSource(org.jdom2.transform.JDOMSource) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) Document(org.jdom2.Document) URL(java.net.URL) SAXSource(javax.xml.transform.sax.SAXSource) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) File(java.io.File) XMLReader(org.xml.sax.XMLReader)

Example 15 with MCRDerivate

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

the class MCRBasketPersistence method createNewDerivate.

/**
 * Creates a new, empty derivate.
 *
 * @param ownerID the ID of the object owning the new derivate
 * @param derivateOID a free derivate ID to use for the newly created derivate
 * @return the empty derivate that was created.
 * @throws IOException
 * @throws MCRPersistenceException
 * @throws MCRAccessException see {@link MCRMetadataManager#create(MCRDerivate)}
 */
private static MCRDerivate createNewDerivate(MCRObjectID ownerID, MCRObjectID derivateOID) throws MCRPersistenceException, IOException, MCRAccessException {
    MCRDerivate derivate = new MCRDerivate();
    derivate.setId(derivateOID);
    derivate.setLabel("Saved basket data for " + ownerID);
    String schema = MCRConfiguration.instance().getString("MCR.Metadata.Config.derivate", "datamodel-derivate.xml");
    derivate.setSchema(schema.replaceAll(".xml", ".xsd"));
    MCRMetaIFS ifs = new MCRMetaIFS();
    ifs.setSubTag("internal");
    ifs.setSourcePath(null);
    derivate.getDerivate().setInternals(ifs);
    MCRMetaLinkID linkId = new MCRMetaLinkID();
    linkId.setSubTag("linkmeta");
    linkId.setReference(ownerID, null, null);
    derivate.getDerivate().setLinkMeta(linkId);
    MCRMetadataManager.create(derivate);
    return derivate;
}
Also used : MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRMetaIFS(org.mycore.datamodel.metadata.MCRMetaIFS)

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