Search in sources :

Example 96 with MCRObjectID

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

the class MCRDerivateCommands method export.

/**
 * Export any MCRDerivate's to files named <em>MCRObjectID</em> .xml in a
 * directory and the objects under them named <em>MCRObjectID</em>. 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 derivate from {0} to {1} to directory {2} with {3}", help = "The command store all derivates with MCRObjectID's between {0} and {1} to the directory {2} with the stylesheet {3}-object.xsl. For {3} save is the default.", order = 80)
public static void export(String fromID, String toID, String dirname, String style) {
    // check fromID and toID
    MCRObjectID fid = null;
    MCRObjectID tid = null;
    try {
        fid = MCRObjectID.getInstance(fromID);
    } catch (Exception ex) {
        LOGGER.error("FromID : {}", ex.getMessage());
        return;
    }
    try {
        tid = MCRObjectID.getInstance(toID);
    } catch (Exception ex) {
        LOGGER.error("ToID : {}", ex.getMessage());
        return;
    }
    // check dirname
    File dir = new File(dirname);
    if (dir.isFile()) {
        LOGGER.error("{} is not a dirctory.", dirname);
        return;
    }
    Transformer trans = getTransformer(style);
    int k = 0;
    try {
        for (int i = fid.getNumberAsInteger(); i < tid.getNumberAsInteger() + 1; i++) {
            exportDerivate(dir, trans, MCRObjectID.formatID(fid.getProjectId(), fid.getTypeId(), i));
            k++;
        }
    } catch (Exception ex) {
        LOGGER.error(ex.getMessage());
        LOGGER.error("Exception while store file or objects to {}", dir.getAbsolutePath(), ex);
        return;
    }
    LOGGER.info("{} Object's stored under {}.", k, dir.getAbsolutePath());
}
Also used : Transformer(javax.xml.transform.Transformer) MCRXSLTransformer(org.mycore.common.content.transformer.MCRXSLTransformer) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) File(java.io.File) 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 97 with MCRObjectID

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

the class MCRDerivateCommands method setMainFile.

@MCRCommand(syntax = "set main file of {0} to {1}", help = "Sets the main file of the derivate with the id {0} to " + "the file with the path {1}")
public static void setMainFile(final String derivateIDString, final String filePath) {
    if (!MCRObjectID.isValid(derivateIDString)) {
        LOGGER.error("{} is not valid. ", derivateIDString);
        return;
    }
    // check for derivate exist
    final MCRObjectID derivateID = MCRObjectID.getInstance(derivateIDString);
    if (!MCRMetadataManager.exists(derivateID)) {
        LOGGER.error("{} does not exist!", derivateIDString);
        return;
    }
    // remove leading slash
    String cleanPath = filePath;
    if (filePath.startsWith(String.valueOf(MCRAbstractFileSystem.SEPARATOR))) {
        cleanPath = filePath.substring(1, filePath.length());
    }
    // check for file exist
    final MCRPath path = MCRPath.getPath(derivateID.toString(), cleanPath);
    if (!Files.exists(path)) {
        LOGGER.error("File {} does not exist!", cleanPath);
        return;
    }
    final MCRDerivate derivate = MCRMetadataManager.retrieveMCRDerivate(derivateID);
    derivate.getDerivate().getInternals().setMainDoc(cleanPath);
    MCRMetadataManager.updateMCRDerivateXML(derivate);
    LOGGER.info("The main file of {} is now '{}'!", derivateIDString, cleanPath);
}
Also used : MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 98 with MCRObjectID

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

the class MCRDerivateCommands method delete.

/**
 * Delete an MCRDerivate from the datastore.
 *
 * @param ID
 *            the ID of the MCRDerivate that should be deleted
 * @throws MCRActiveLinkException
 * @throws MCRAccessException see {@link MCRMetadataManager#delete(MCRDerivate)}
 */
@MCRCommand(syntax = "delete derivate {0}", help = "The command remove a derivate with the MCRObjectID {0}", order = 30)
public static void delete(String ID) throws MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
    MCRObjectID objectID = MCRObjectID.getInstance(ID);
    MCRMetadataManager.deleteMCRDerivate(objectID);
    LOGGER.info("{} deleted.", objectID);
}
Also used : MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 99 with MCRObjectID

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

the class MCRObjectCommands method repairMCRLinkHrefTable.

@MCRCommand(syntax = "repair mcrlinkhref table", help = "Runs through the whole table and checks for already deleted mcr objects and deletes them.", order = 185)
public static void repairMCRLinkHrefTable() {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    MCRStreamQuery<String> fromQuery = MCRStreamQuery.getInstance(em, "SELECT DISTINCT m.key.mcrfrom FROM MCRLINKHREF m", String.class);
    MCRStreamQuery<String> toQuery = MCRStreamQuery.getInstance(em, "SELECT DISTINCT m.key.mcrto FROM MCRLINKHREF m", String.class);
    String query = "DELETE FROM MCRLINKHREF m WHERE m.key.mcrfrom IN (:invalidIds) or m.key.mcrto IN (:invalidIds)";
    // open streams
    try (Stream<String> fromStream = fromQuery.getResultStream()) {
        try (Stream<String> toStream = toQuery.getResultStream()) {
            List<String> invalidIds = Stream.concat(fromStream, toStream).distinct().filter(MCRObjectID::isValid).map(MCRObjectID::getInstance).filter(MCRStreamUtils.not(MCRMetadataManager::exists)).map(MCRObjectID::toString).collect(Collectors.toList());
            // delete
            em.createQuery(query).setParameter("invalidIds", invalidIds).executeUpdate();
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 100 with MCRObjectID

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

the class MCRObjectCommands method listRevisions.

/**
 * List revisions of an MyCoRe Object.
 *
 * @param id
 *            id of MyCoRe Object
 */
@MCRCommand(syntax = "list revisions of {0}", help = "List revisions of MCRObject.", order = 260)
public static void listRevisions(String id) {
    MCRObjectID mcrId = MCRObjectID.getInstance(id);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
    try {
        StringBuilder log = new StringBuilder("Revisions:\n");
        List<MCRMetadataVersion> revisions = MCRXMLMetadataManager.instance().listRevisions(mcrId);
        for (MCRMetadataVersion revision : revisions) {
            log.append(revision.getRevision()).append(" ");
            log.append(revision.getType()).append(" ");
            log.append(sdf.format(revision.getDate())).append(" ");
            log.append(revision.getUser());
            log.append("\n");
        }
        LOGGER.info(log.toString());
    } catch (Exception exc) {
        LOGGER.error("While print revisions.", exc);
    }
}
Also used : MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) SimpleDateFormat(java.text.SimpleDateFormat) 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)

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