Search in sources :

Example 36 with MCRObjectID

use of org.mycore.datamodel.metadata.MCRObjectID 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 37 with MCRObjectID

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

the class MCRObjectCommands method export.

/**
 * Save any MCRObject's to files named <em>MCRObjectID</em> .xml in a directory. The saving starts with fromID and
 * runs to toID. ID's they was not found will skiped. The method use the converter stylesheet mcr_<em>style</em>
 * _object.xsl.
 *
 * @param fromID
 *            the ID of the MCRObject from be save.
 * @param toID
 *            the ID of the MCRObject to be save.
 * @param dirname
 *            the filename to store the object
 * @param style
 *            the type of the stylesheet
 */
@MCRCommand(syntax = "export object from {0} to {1} to directory {2} with {3}", help = "Stores all MCRObjects with MCRObjectID's between {0} and {1} to the directory {2} with the stylesheet {3}-object.xsl. For {3} save is the default.", order = 100)
public static void export(String fromID, String toID, String dirname, String style) {
    MCRObjectID fid, tid;
    // check fromID and toID
    try {
        fid = MCRObjectID.getInstance(fromID);
        tid = MCRObjectID.getInstance(toID);
    } catch (Exception ex) {
        LOGGER.error("FromID : {}", ex.getMessage());
        return;
    }
    // check dirname
    File dir = new File(dirname);
    if (!dir.isDirectory()) {
        LOGGER.error("{} is not a dirctory.", dirname);
        return;
    }
    int k = 0;
    try {
        Transformer trans = getTransformer(style);
        for (int i = fid.getNumberAsInteger(); i < tid.getNumberAsInteger() + 1; i++) {
            String id = MCRObjectID.formatID(fid.getProjectId(), fid.getTypeId(), i);
            if (!MCRMetadataManager.exists(MCRObjectID.getInstance(id))) {
                continue;
            }
            if (!exportMCRObject(dir, trans, id)) {
                continue;
            }
            k++;
        }
    } catch (Exception ex) {
        LOGGER.error(ex.getMessage());
        LOGGER.error("Exception while store file to {}", dir.getAbsolutePath());
        return;
    }
    LOGGER.info("{} Object's stored under {}.", k, dir.getAbsolutePath());
}
Also used : Transformer(javax.xml.transform.Transformer) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) File(java.io.File) URISyntaxException(java.net.URISyntaxException) JDOMException(org.jdom2.JDOMException) MCRPersistenceException(org.mycore.common.MCRPersistenceException) 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) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 38 with MCRObjectID

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

the class MCRObjectCommands method checkDerivatesInObjects.

/**
 * Check the derivate links in objects 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_document
 */
@MCRCommand(syntax = "check derivate entries in objects for base {0}", help = "check in all objects with MCR base ID {0} for existing linked derivates", order = 400)
public static void checkDerivatesInObjects(String base_id) throws IOException {
    if (base_id == null || base_id.length() == 0) {
        LOGGER.error("Base ID missed for check derivate entries in objects for base {0}");
        return;
    }
    MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
    List<String> id_list = mgr.listIDsForBase(base_id);
    int counter = 0;
    int maxresults = id_list.size();
    for (String objid : id_list) {
        counter++;
        LOGGER.info("Processing dataset {} from {} with ID: {}", counter, maxresults, objid);
        // get from data
        MCRObjectID mcrobjid = MCRObjectID.getInstance(objid);
        MCRObject obj = MCRMetadataManager.retrieveMCRObject(mcrobjid);
        List<MCRMetaLinkID> derivate_entries = obj.getStructure().getDerivates();
        for (MCRMetaLinkID derivate_entry : derivate_entries) {
            String derid = derivate_entry.getXLinkHref();
            if (!mgr.exists(MCRObjectID.getInstance(derid))) {
                LOGGER.error("   !!! Missing derivate {} in database for base ID {}", derid, base_id);
            }
        }
    }
    LOGGER.info("Check done for {} entries", Integer.toString(counter));
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 39 with MCRObjectID

use of org.mycore.datamodel.metadata.MCRObjectID 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 40 with MCRObjectID

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

the class MCRObjectCommands method delete.

/**
 * Delete a MCRObject from the datastore.
 *
 * @param ID
 *            the ID of the MCRObject that should be deleted
 * @throws MCRPersistenceException  if a persistence problem is occurred
 * @throws MCRAccessException see {@link MCRMetadataManager#deleteMCRObject(MCRObjectID)}
 * @throws MCRActiveLinkException if object is referenced by other objects
 */
@MCRCommand(syntax = "delete object {0}", help = "Removes a MCRObject with the MCRObjectID {0}", order = 40)
public static void delete(String ID) throws MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
    MCRObjectID mcrId = MCRObjectID.getInstance(ID);
    MCRMetadataManager.deleteMCRObject(mcrId);
    LOGGER.info("{} deleted.", mcrId);
}
Also used : MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)144 IOException (java.io.IOException)37 MCRObject (org.mycore.datamodel.metadata.MCRObject)32 MCRException (org.mycore.common.MCRException)30 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)30 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)29 MCRPath (org.mycore.datamodel.niofs.MCRPath)25 MCRAccessException (org.mycore.access.MCRAccessException)22 Document (org.jdom2.Document)20 MCRPersistenceException (org.mycore.common.MCRPersistenceException)18 MCRMetaLinkID (org.mycore.datamodel.metadata.MCRMetaLinkID)16 JDOMException (org.jdom2.JDOMException)15 MCRBase (org.mycore.datamodel.metadata.MCRBase)15 SAXException (org.xml.sax.SAXException)15 Date (java.util.Date)14 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)13 MCRSession (org.mycore.common.MCRSession)11 MCRContent (org.mycore.common.content.MCRContent)11 MCRPersistentIdentifierException (org.mycore.pi.exceptions.MCRPersistentIdentifierException)11 URISyntaxException (java.net.URISyntaxException)10