Search in sources :

Example 81 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRObjectCommands method restoreToRevision.

/**
 * This method restores a MyCoRe Object to the selected revision. Please note that children and derivates are not
 * deleted or reverted!
 *
 * @param id
 *            id of MyCoRe Object
 * @param revision
 *            revision to restore
 */
@MCRCommand(syntax = "restore {0} to revision {1}", help = "Restores the selected MCRObject to the selected revision.", order = 270)
public static void restoreToRevision(String id, long revision) {
    LOGGER.info("Try to restore object {} with revision {}", id, revision);
    MCRObjectID mcrId = MCRObjectID.getInstance(id);
    try {
        MCRObjectUtils.restore(mcrId, revision);
        LOGGER.info("Object {} successfully restored!", id);
    } catch (Exception exc) {
        LOGGER.error("While retrieving object {} with revision {}", id, revision, exc);
    }
}
Also used : MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) 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 82 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRObjectCommands method mergeDerivatesOfObject.

@MCRCommand(syntax = "merge derivates of object {0}", help = "Retrieves the MCRObject with the MCRObjectID {0} and if it has more then one MCRDerivate, then all" + " Files will be copied to the first Derivate and all other will be deleted.", order = 190)
public static void mergeDerivatesOfObject(String id) {
    MCRObjectID objectID = MCRObjectID.getInstance(id);
    if (!MCRMetadataManager.exists(objectID)) {
        LOGGER.error("The object with the id {} does not exist!", id);
        return;
    }
    MCRObject object = MCRMetadataManager.retrieveMCRObject(objectID);
    List<MCRMetaLinkID> derivateLinkIDs = object.getStructure().getDerivates();
    List<MCRObjectID> derivateIDs = derivateLinkIDs.stream().map(MCRMetaLinkID::getXLinkHrefID).collect(Collectors.toList());
    if (derivateIDs.size() <= 1) {
        LOGGER.error("The object with the id {} has no Derivates to merge!", id);
        return;
    }
    String mainID = derivateIDs.get(0).toString();
    MCRPath mainDerivateRootPath = MCRPath.getPath(mainID, "/");
    derivateIDs.stream().skip(1).forEach(derivateID -> {
        LOGGER.info("Merge {} into {}...", derivateID, mainID);
        MCRPath copyRootPath = MCRPath.getPath(derivateID.toString(), "/");
        try {
            MCRTreeCopier treeCopier = new MCRTreeCopier(copyRootPath, mainDerivateRootPath);
            Files.walkFileTree(copyRootPath, treeCopier);
            Files.walkFileTree(copyRootPath, MCRRecursiveDeleter.instance());
            MCRMetadataManager.deleteMCRDerivate(derivateID);
        } catch (IOException | MCRAccessException e) {
            throw new MCRException(e);
        }
    });
}
Also used : MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRException(org.mycore.common.MCRException) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRAccessException(org.mycore.access.MCRAccessException) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 83 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRObjectCommands method deleteFromTo.

/**
 * Delete MCRObject's form ID to ID from the datastore.
 *
 * @param IDfrom
 *            the start ID for deleting the MCRObjects
 * @param IDto
 *            the stop ID for deleting the MCRObjects
 * @return list of delete commands
 */
@MCRCommand(syntax = "delete object from {0} to {1}", help = "Removes MCRObjects in the number range between the MCRObjectID {0} and {1}.", order = 30)
public static List<String> deleteFromTo(String IDfrom, String IDto) {
    MCRObjectID from = MCRObjectID.getInstance(IDfrom);
    MCRObjectID to = MCRObjectID.getInstance(IDto);
    int from_i = from.getNumberAsInteger();
    int to_i = to.getNumberAsInteger();
    if (from_i > to_i) {
        throw new MCRException("The from-to-interval is false.");
    }
    List<String> cmds = new ArrayList<>(to_i - from_i);
    for (int i = from_i; i < to_i + 1; i++) {
        String id = MCRObjectID.formatID(from.getProjectId(), from.getTypeId(), i);
        if (MCRMetadataManager.exists(MCRObjectID.getInstance(id))) {
            cmds.add("delete object " + id);
        }
    }
    return cmds;
}
Also used : MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 84 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRObjectCommands method replaceParent.

/**
 * Moves object to new parent.
 *
 * @param sourceId
 *            object that should be attached to new parent
 * @param newParentId
 *            the ID of the new parent
 * @throws MCRAccessException see {@link MCRMetadataManager#update(MCRObject)}
 */
@MCRCommand(syntax = "set parent of {0} to {1}", help = "replaces a parent of an object (first parameter) to the given new one (second parameter)", order = 300)
public static void replaceParent(String sourceId, String newParentId) throws MCRPersistenceException, MCRActiveLinkException, MCRAccessException {
    // child
    MCRObject sourceMCRObject = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(sourceId));
    // old parent
    MCRObjectID oldParentId = sourceMCRObject.getStructure().getParentID();
    MCRObjectID newParentObjectID = MCRObjectID.getInstance(newParentId);
    if (newParentObjectID.equals(oldParentId)) {
        LOGGER.info("Object {} is already child of {}", sourceId, newParentId);
        return;
    }
    MCRObject oldParentMCRObject = null;
    if (oldParentId != null) {
        try {
            oldParentMCRObject = MCRMetadataManager.retrieveMCRObject(oldParentId);
        } catch (Exception exc) {
            LOGGER.error("Unable to get old parent object {}, its probably deleted.", oldParentId, exc);
        }
    }
    // change href to new parent
    LOGGER.info("Setting link in \"{}\" to parent \"{}\"", sourceId, newParentObjectID);
    MCRMetaLinkID parentLinkId = new MCRMetaLinkID("parent", 0);
    parentLinkId.setReference(newParentObjectID, null, null);
    sourceMCRObject.getStructure().setParent(parentLinkId);
    if (oldParentMCRObject != null) {
        // remove Child in old parent
        LOGGER.info("Remove child \"{}\" in old parent \"{}\"", sourceId, oldParentId);
        oldParentMCRObject.getStructure().removeChild(sourceMCRObject.getId());
        LOGGER.info("Update old parent \"{}\n", oldParentId);
        MCRMetadataManager.update(oldParentMCRObject);
    }
    LOGGER.info("Update \"{}\" in datastore (saving new link)", sourceId);
    MCRMetadataManager.update(sourceMCRObject);
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Structure: {}", sourceMCRObject.getStructure().isValid());
        LOGGER.debug("Object: {}", sourceMCRObject.isValid());
    }
}
Also used : MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) 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 85 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.

the class MCRSolrCommands method createObjectType.

@MCRCommand(syntax = "create solr objecttype {0} at {1}", help = "indexes all objects of an object type (e.g. document) on specific solr server core", order = 125)
public static void createObjectType(String type, String url) throws Exception {
    MCRSolrCore core = new MCRSolrCore(url);
    SolrClient concurrentSolrClient = core.getConcurrentClient();
    MCRSolrIndexer.rebuildMetadataIndex(MCRXMLMetadataManager.instance().listIDsOfType(type), concurrentSolrClient);
    concurrentSolrClient.optimize();
}
Also used : SolrClient(org.apache.solr.client.solrj.SolrClient) ConcurrentUpdateSolrClient(org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient) MCRSolrCore(org.mycore.solr.MCRSolrCore) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)106 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)37 MCRException (org.mycore.common.MCRException)34 IOException (java.io.IOException)30 File (java.io.File)22 ArrayList (java.util.ArrayList)18 Document (org.jdom2.Document)17 JDOMException (org.jdom2.JDOMException)17 MCRObject (org.mycore.datamodel.metadata.MCRObject)17 Path (java.nio.file.Path)16 SAXException (org.xml.sax.SAXException)16 EntityManager (javax.persistence.EntityManager)15 MCRAccessException (org.mycore.access.MCRAccessException)15 MCRActiveLinkException (org.mycore.datamodel.common.MCRActiveLinkException)15 MCRPath (org.mycore.datamodel.niofs.MCRPath)15 FileNotFoundException (java.io.FileNotFoundException)13 SAXParseException (org.xml.sax.SAXParseException)12 List (java.util.List)11 Element (org.jdom2.Element)11 MCRPersistenceException (org.mycore.common.MCRPersistenceException)11