Search in sources :

Example 11 with MCRCommand

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

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

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

Example 14 with MCRCommand

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

the class MCRBasicCommands method checkXMLFile.

/**
 * The method parse and check an XML file.
 *
 * @param fileName
 *            the location of the xml file
 */
@MCRCommand(syntax = "check file {0}", help = "Checks the data file {0} against the XML Schema.", order = 160)
public static boolean checkXMLFile(String fileName) throws MCRException, SAXParseException, IOException {
    if (!fileName.endsWith(".xml")) {
        LOGGER.warn("{} ignored, does not end with *.xml", fileName);
        return false;
    }
    File file = new File(fileName);
    if (!file.isFile()) {
        LOGGER.warn("{} ignored, is not a file.", fileName);
        return false;
    }
    LOGGER.info("Reading file {} ...", file);
    MCRContent content = new MCRFileContent(file);
    MCRXMLParserFactory.getParser().parseXML(content);
    LOGGER.info("The file has no XML errors.");
    return true;
}
Also used : MCRFileContent(org.mycore.common.content.MCRFileContent) File(java.io.File) MCRContent(org.mycore.common.content.MCRContent) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 15 with MCRCommand

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

the class MCRClassification2Commands method checkClassification.

@MCRCommand(syntax = "check classification {0}", help = "checks if all redundant information are stored without conflicts", order = 150)
public static void checkClassification(String id) {
    LOGGER.info("Checking classifcation {}", id);
    ArrayList<String> log = new ArrayList<>();
    LOGGER.info("{}: checking for missing parentID", id);
    checkMissingParent(id, log);
    LOGGER.info("{}: checking for empty labels", id);
    checkEmptyLabels(id, log);
    if (log.isEmpty()) {
        MCRCategoryImpl category = (MCRCategoryImpl) MCRCategoryDAOFactory.getInstance().getCategory(MCRCategoryID.rootID(id), -1);
        LOGGER.info("{}: checking left, right and level values and for non-null children", id);
        checkLeftRightAndLevel(category, 0, 0, log);
    }
    if (log.size() > 0) {
        LOGGER.error("Some errors occured on last test, report will follow");
        StringBuilder sb = new StringBuilder();
        for (String msg : log) {
            sb.append(msg).append('\n');
        }
        LOGGER.error("Error report for classification {}\n{}", id, sb);
    } else {
        LOGGER.info("Classifcation {} has no errors.", id);
    }
}
Also used : ArrayList(java.util.ArrayList) MCRCategoryImpl(org.mycore.datamodel.classifications2.impl.MCRCategoryImpl) 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