Search in sources :

Example 31 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory 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 32 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory 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 33 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory 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 34 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory 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)

Example 35 with MCRCategory

use of org.mycore.datamodel.classifications2.MCRCategory in project mycore by MyCoRe-Org.

the class MCRClassification2Commands method countChildren.

/**
 * Counts the classification categories on top level
 *
 * @param classID classification ID
 */
@MCRCommand(syntax = "count classification children of {0}", help = "The command count the categoies of the classification with MCRObjectID {0} in the system.", order = 80)
public static void countChildren(String classID) {
    MCRCategory category = DAO.getCategory(MCRCategoryID.rootID(classID), 1);
    System.out.printf(Locale.ROOT, "%s has %d children", category.getId(), category.getChildren().size());
}
Also used : MCRCategory(org.mycore.datamodel.classifications2.MCRCategory) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

MCRCategory (org.mycore.datamodel.classifications2.MCRCategory)87 MCRCategoryID (org.mycore.datamodel.classifications2.MCRCategoryID)36 Test (org.junit.Test)24 MCRLabel (org.mycore.datamodel.classifications2.MCRLabel)17 MCRCategoryDAO (org.mycore.datamodel.classifications2.MCRCategoryDAO)10 ArrayList (java.util.ArrayList)9 Document (org.jdom2.Document)9 Element (org.jdom2.Element)8 MCRException (org.mycore.common.MCRException)8 IOException (java.io.IOException)6 EntityManager (javax.persistence.EntityManager)6 URI (java.net.URI)5 Collection (java.util.Collection)5 HashMap (java.util.HashMap)5 LogManager (org.apache.logging.log4j.LogManager)5 Logger (org.apache.logging.log4j.Logger)5 MCRCommand (org.mycore.frontend.cli.annotation.MCRCommand)5 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 List (java.util.List)4