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!");
}
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;
}
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);
}
}
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);
}
}
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());
}
Aggregations