Search in sources :

Example 16 with MCRCommand

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

the class MCRClassification2Commands method repairEmptyLabels.

@MCRCommand(syntax = "repair category with empty labels", help = "fixes all categories with no labels (adds a label with categid as @text for default lang)", order = 110)
public static void repairEmptyLabels() {
    Session session = MCRHIBConnection.instance().getSession();
    String deleteEmptyLabels = "delete from {h-schema}MCRCategoryLabels where text is null or trim(text) = ''";
    int affected = session.createNativeQuery(deleteEmptyLabels).executeUpdate();
    LOGGER.info("Deleted {} labels.", affected);
    String sqlQuery = "select cat.classid,cat.categid from {h-schema}MCRCategory cat left outer join {h-schema}MCRCategoryLabels label on cat.internalid = label.category where label.text is null";
    @SuppressWarnings("unchecked") List<Object[]> list = session.createNativeQuery(sqlQuery).getResultList();
    for (Object resultList : list) {
        Object[] arrayOfResults = (Object[]) resultList;
        String classIDString = (String) arrayOfResults[0];
        String categIDString = (String) arrayOfResults[1];
        MCRCategoryID mcrCategID = new MCRCategoryID(classIDString, categIDString);
        MCRLabel mcrCategLabel = new MCRLabel(MCRConstants.DEFAULT_LANG, categIDString, null);
        MCRCategoryDAOFactory.getInstance().setLabel(mcrCategID, mcrCategLabel);
        LOGGER.info("fixing category with class ID \"{}\" and category ID \"{}\"", classIDString, categIDString);
    }
    LOGGER.info("Fixing category labels completed!");
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRLabel(org.mycore.datamodel.classifications2.MCRLabel) Session(org.hibernate.Session) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 17 with MCRCommand

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

the class MCRClassification2Commands method export.

/**
 * Save a MCRClassification.
 *
 * @param ID
 *            the ID of the MCRClassification to be save.
 * @param dirname
 *            the directory to export the classification to
 * @param style
 *            the name part of the stylesheet like <em>style</em>
 *            -classification.xsl
 * @return false if an error was occured, else true
 */
@MCRCommand(syntax = "export classification {0} to directory {1} with {2}", help = "The command exports the classification with MCRObjectID {0} as xml file to directory named {1} using the stylesheet {2}-object.xsl. For {2} save is the default.", order = 60)
public static boolean export(String ID, String dirname, String style) throws Exception {
    String dname = "";
    if (dirname.length() != 0) {
        try {
            File dir = new File(dirname);
            if (!dir.isDirectory()) {
                dir.mkdir();
            }
            if (!dir.isDirectory()) {
                LOGGER.error("Can't find or create directory {}", dir.getAbsolutePath());
                return false;
            } else {
                dname = dirname;
            }
        } catch (Exception e) {
            LOGGER.error("Can't find or create directory {}", dirname, e);
            return false;
        }
    }
    MCRCategory cl = DAO.getCategory(MCRCategoryID.rootID(ID), -1);
    Document classDoc = MCRCategoryTransformer.getMetaDataDocument(cl, false);
    Transformer trans = getTransformer(style);
    File xmlOutput = new File(dname, ID + ".xml");
    FileOutputStream out = new FileOutputStream(xmlOutput);
    if (trans != null) {
        StreamResult sr = new StreamResult(out);
        trans.transform(new org.jdom2.transform.JDOMSource(classDoc), sr);
    } else {
        XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat());
        xout.output(classDoc, out);
        out.flush();
    }
    LOGGER.info("Classifcation {} saved to {}.", ID, xmlOutput.getCanonicalPath());
    return true;
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) XMLOutputter(org.jdom2.output.XMLOutputter) Transformer(javax.xml.transform.Transformer) MCRCategoryTransformer(org.mycore.datamodel.classifications2.utils.MCRCategoryTransformer) MCRXMLTransformer(org.mycore.datamodel.classifications2.utils.MCRXMLTransformer) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream) Document(org.jdom2.Document) File(java.io.File) URISyntaxException(java.net.URISyntaxException) MCRException(org.mycore.common.MCRException) MalformedURLException(java.net.MalformedURLException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 18 with MCRCommand

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

the class MCRClassification2Commands method listAllClassifications.

/**
 * List all IDs of all classifications stored in the database
 */
@MCRCommand(syntax = "list all classifications", help = "The command list all classification stored in the database.", order = 100)
public static void listAllClassifications() {
    List<MCRCategoryID> allClassIds = DAO.getRootCategoryIDs();
    for (MCRCategoryID id : allClassIds) {
        LOGGER.info(id.getRootID());
    }
    LOGGER.info("");
}
Also used : MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 19 with MCRCommand

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

the class MCRClassification2Commands method updateFromURL.

@MCRCommand(syntax = "update classification from url {0}", help = "The command updates a classification from URL {0} to the system.", order = 25)
public static void updateFromURL(String fileURL) throws SAXParseException, MalformedURLException, URISyntaxException {
    Document xml = MCRXMLParserFactory.getParser().parseXML(new MCRURLContent(new URL(fileURL)));
    MCRCategory category = MCRXMLTransformer.getCategory(xml);
    if (DAO.exist(category.getId())) {
        DAO.replaceCategory(category);
    } else {
        // add if classification does not exist
        DAO.addCategory(null, category);
    }
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRURLContent(org.mycore.common.content.MCRURLContent) Document(org.jdom2.Document) URL(java.net.URL) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 20 with MCRCommand

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

the class MCRClassification2Commands method listClassification.

/**
 * List a MCRClassification.
 *
 * @param classid
 *            the MCRObjectID of the classification
 */
@MCRCommand(syntax = "list classification {0}", help = "The command list the classification with MCRObjectID {0}.", order = 90)
public static void listClassification(String classid) {
    MCRCategoryID clid = MCRCategoryID.rootID(classid);
    MCRCategory cl = DAO.getCategory(clid, -1);
    LOGGER.info(classid);
    if (cl != null) {
        listCategory(cl);
    } else {
        LOGGER.error("Can't find classification {}", classid);
    }
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCategoryID(org.mycore.datamodel.classifications2.MCRCategoryID) 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