Search in sources :

Example 6 with MCRCommand

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

the class MCRSolrCommands method selectObjectsWithSolrQuery.

@MCRCommand(syntax = "select objects with solr query {0}", help = "selects mcr objects with a solr query", order = 180)
public static void selectObjectsWithSolrQuery(String query) throws Exception {
    SolrClient solrClient = MCRSolrClientFactory.getSolrClient();
    List<String> ids = MCRSolrSearchUtils.listIDs(solrClient, query);
    MCRObjectCommands.setSelectedObjectIDs(ids);
}
Also used : SolrClient(org.apache.solr.client.solrj.SolrClient) ConcurrentUpdateSolrClient(org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrClient) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 7 with MCRCommand

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

the class MCRDerivateCommands method synchronizeDerivateForID.

/**
 * The method sychronize the xlink:label of the mycorederivate with the
 * xlink:label of the derivate reference of mycoreobject.
 *
 * @param id
 *            the MCRObjectID as String
 */
@MCRCommand(syntax = "synchronize derivate with ID {0}", help = "The command read a derivate with the MCRObjectID {0} and synchronize the xlink:label with the derivate entry of the mycoreobject.", order = 170)
public static void synchronizeDerivateForID(String id) {
    MCRObjectID mid = null;
    try {
        mid = MCRObjectID.getInstance(id);
    } catch (Exception e) {
        LOGGER.error("The String {} is not a MCRObjectID.", id);
        return;
    }
    // set mycoreobject
    MCRDerivate der = MCRMetadataManager.retrieveMCRDerivate(mid);
    String label = der.getLabel();
    String href = der.getDerivate().getMetaLink().getXLinkHref();
    MCRObject obj = MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(href));
    int size = obj.getStructure().getDerivates().size();
    boolean isset = false;
    for (int i = 0; i < size; i++) {
        MCRMetaLinkID link = obj.getStructure().getDerivates().get(i);
        if (link.getXLinkHref().equals(mid.toString())) {
            String oldlabel = link.getXLinkLabel();
            if (oldlabel != null && !oldlabel.trim().equals(label)) {
                obj.getStructure().getDerivates().get(i).setXLinkTitle(label);
                isset = true;
            }
            break;
        }
    }
    // update mycoreobject
    if (isset) {
        MCRMetadataManager.fireUpdateEvent(obj);
        LOGGER.info("Synchronized {}", mid);
    }
}
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 8 with MCRCommand

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

the class MCRDerivateCommands method exportAllDerivatesOfProject.

/**
 * The command look for all derivates starts with project name in the
 * application and build export commands.
 *
 * @param dirname
 *            the filename to store the object
 * @param style
 *            the type of the stylesheet
 * @return a list of export commands for derivates with project name
 */
@MCRCommand(syntax = "export all derivates of project {0} to directory {1} with {2}", help = "Stores all derivates of project {0} to the directory {1} with the stylesheet mcr_{2}-derivate.xsl. For {2} save is the default.", order = 110)
public static List<String> exportAllDerivatesOfProject(String project, String dirname, String style) {
    // check dirname
    File dir = new File(dirname);
    if (dir.isFile()) {
        throw new MCRException(dirname + " is not a dirctory.");
    }
    List<String> ids = MCRXMLMetadataManager.instance().listIDsOfType("derivate");
    List<String> cmds = new ArrayList<>(ids.size());
    for (String id : ids) {
        if (!id.startsWith(project))
            continue;
        cmds.add("export derivate " + id + " to directory " + dirname + " with " + style);
    }
    return cmds;
}
Also used : MCRException(org.mycore.common.MCRException) ArrayList(java.util.ArrayList) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 9 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand 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 10 with MCRCommand

use of org.mycore.frontend.cli.annotation.MCRCommand 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)

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