Search in sources :

Example 76 with MCRCommand

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

the class MCRLoggingCommands method changeLogLevel.

/**
 * @param name
 *            the name of the java class or java package to set the log
 *            level for
 * @param logLevelToSet
 *            the log level to set e.g. TRACE, DEBUG, INFO, WARN, ERROR and
 *            FATAL, providing any other value will lead to DEBUG as new log
 *            level
 */
@MCRCommand(syntax = "change log level of {0} to {1}", help = "{0} the package or class name for which to change the log level, {1} the log level to set.", order = 10)
public static synchronized void changeLogLevel(String name, String logLevelToSet) {
    LOGGER.info("Setting log level for \"{}\" to \"{}\"", name, logLevelToSet);
    Level newLevel = Level.getLevel(logLevelToSet);
    if (newLevel == null) {
        LOGGER.error("Unknown log level \"{}\"", logLevelToSet);
        return;
    }
    Logger log = "ROOT".equals(name) ? LogManager.getRootLogger() : LogManager.getLogger(name);
    if (log == null) {
        LOGGER.error("Could not get logger for \"{}\"", name);
        return;
    }
    LOGGER.info("Change log level from {} to {}", log.getLevel(), newLevel);
    Configurator.setLevel(log.getName(), newLevel);
}
Also used : Level(org.apache.logging.log4j.Level) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 77 with MCRCommand

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

the class MCRObjectCommands method repairMCRLinkHrefTable.

@MCRCommand(syntax = "repair mcrlinkhref table", help = "Runs through the whole table and checks for already deleted mcr objects and deletes them.", order = 185)
public static void repairMCRLinkHrefTable() {
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    MCRStreamQuery<String> fromQuery = MCRStreamQuery.getInstance(em, "SELECT DISTINCT m.key.mcrfrom FROM MCRLINKHREF m", String.class);
    MCRStreamQuery<String> toQuery = MCRStreamQuery.getInstance(em, "SELECT DISTINCT m.key.mcrto FROM MCRLINKHREF m", String.class);
    String query = "DELETE FROM MCRLINKHREF m WHERE m.key.mcrfrom IN (:invalidIds) or m.key.mcrto IN (:invalidIds)";
    // open streams
    try (Stream<String> fromStream = fromQuery.getResultStream()) {
        try (Stream<String> toStream = toQuery.getResultStream()) {
            List<String> invalidIds = Stream.concat(fromStream, toStream).distinct().filter(MCRObjectID::isValid).map(MCRObjectID::getInstance).filter(MCRStreamUtils.not(MCRMetadataManager::exists)).map(MCRObjectID::toString).collect(Collectors.toList());
            // delete
            em.createQuery(query).setParameter("invalidIds", invalidIds).executeUpdate();
        }
    }
}
Also used : EntityManager(javax.persistence.EntityManager) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 78 with MCRCommand

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

the class MCRObjectCommands method deleteTopologicalAllObjects.

/**
 * Delete all MCRObjects from the datastore in topological order
 */
@MCRCommand(syntax = "delete all objects in topological order", help = "Removes all MCRObjects in topological order.", order = 25)
public static List<String> deleteTopologicalAllObjects() {
    final List<String> objectIds = MCRXMLMetadataManager.instance().listIDs();
    String[] objects = objectIds.stream().filter(id -> !id.contains("_derivate_")).toArray(String[]::new);
    MCRTopologicalSort ts = new MCRTopologicalSort();
    ts.prepareMCRObjects(objects);
    int[] order = ts.doTopoSort();
    List<String> cmds = new ArrayList<>(objectIds.size());
    if (order != null) {
        // delete in reverse order
        for (int o = order.length - 1; o >= 0; o--) {
            cmds.add("delete object " + ts.getNodeName(order[o]));
        }
    }
    return cmds;
}
Also used : Transformer(javax.xml.transform.Transformer) MCRTopologicalSort(org.mycore.tools.MCRTopologicalSort) Arrays(java.util.Arrays) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) StreamResult(javax.xml.transform.stream.StreamResult) MCRStreamQuery(org.mycore.backend.jpa.MCRStreamQuery) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRRecursiveDeleter(org.mycore.datamodel.niofs.utils.MCRRecursiveDeleter) Document(org.jdom2.Document) JDOMSource(org.jdom2.transform.JDOMSource) JDOMException(org.jdom2.JDOMException) Locale(java.util.Locale) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) MCRLinkTableManager(org.mycore.datamodel.common.MCRLinkTableManager) MCRContent(org.mycore.common.content.MCRContent) Predicate(java.util.function.Predicate) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRPersistenceException(org.mycore.common.MCRPersistenceException) Collectors(java.util.stream.Collectors) MCRObjectUtils(org.mycore.datamodel.metadata.MCRObjectUtils) Objects(java.util.Objects) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) List(java.util.List) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) SAXException(org.xml.sax.SAXException) Entry(java.util.Map.Entry) Optional(java.util.Optional) MCREntityResolver(org.mycore.common.xml.MCREntityResolver) MCRErrorListener(org.mycore.common.xsl.MCRErrorListener) MCRStreamUtils(org.mycore.common.MCRStreamUtils) MCRActiveLinkException(org.mycore.datamodel.common.MCRActiveLinkException) MCRMetaLinkID(org.mycore.datamodel.metadata.MCRMetaLinkID) MCRBase(org.mycore.datamodel.metadata.MCRBase) TransformerException(javax.xml.transform.TransformerException) StreamSource(javax.xml.transform.stream.StreamSource) SimpleDateFormat(java.text.SimpleDateFormat) MCRConfiguration(org.mycore.common.config.MCRConfiguration) Function(java.util.function.Function) MCRException(org.mycore.common.MCRException) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) XMLReader(org.xml.sax.XMLReader) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRAccessException(org.mycore.access.MCRAccessException) Hashtable(java.util.Hashtable) MCRTreeCopier(org.mycore.datamodel.niofs.utils.MCRTreeCopier) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) InputSource(org.xml.sax.InputSource) MalformedURLException(java.net.MalformedURLException) Files(java.nio.file.Files) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JDOMResult(org.jdom2.transform.JDOMResult) EntityManager(javax.persistence.EntityManager) OutputKeys(javax.xml.transform.OutputKeys) File(java.io.File) MCRURIResolver(org.mycore.common.xml.MCRURIResolver) SAXSource(javax.xml.transform.sax.SAXSource) SAXParseException(org.xml.sax.SAXParseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MCRSessionMgr(org.mycore.common.MCRSessionMgr) MCRObject(org.mycore.datamodel.metadata.MCRObject) MCRXMLHelper(org.mycore.common.xml.MCRXMLHelper) TransformerFactory(javax.xml.transform.TransformerFactory) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRXMLParserFactory(org.mycore.common.xml.MCRXMLParserFactory) MCRTopologicalSort(org.mycore.tools.MCRTopologicalSort) ArrayList(java.util.ArrayList) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 79 with MCRCommand

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

the class MCRObjectCommands method listRevisions.

/**
 * List revisions of an MyCoRe Object.
 *
 * @param id
 *            id of MyCoRe Object
 */
@MCRCommand(syntax = "list revisions of {0}", help = "List revisions of MCRObject.", order = 260)
public static void listRevisions(String id) {
    MCRObjectID mcrId = MCRObjectID.getInstance(id);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ROOT);
    try {
        StringBuilder log = new StringBuilder("Revisions:\n");
        List<MCRMetadataVersion> revisions = MCRXMLMetadataManager.instance().listRevisions(mcrId);
        for (MCRMetadataVersion revision : revisions) {
            log.append(revision.getRevision()).append(" ");
            log.append(revision.getType()).append(" ");
            log.append(sdf.format(revision.getDate())).append(" ");
            log.append(revision.getUser());
            log.append("\n");
        }
        LOGGER.info(log.toString());
    } catch (Exception exc) {
        LOGGER.error("While print revisions.", exc);
    }
}
Also used : MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) SimpleDateFormat(java.text.SimpleDateFormat) 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 80 with MCRCommand

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

the class MCRObjectCommands method repairMetadataSearchForID.

/**
 * The method start the repair of the metadata search for a given MCRObjectID as String.
 *
 * @param id
 *            the MCRObjectID as String
 */
@MCRCommand(syntax = "repair metadata search of ID {0}", help = "Retrieves the MCRObject with the MCRObjectID {0} and restores it in the search store.", order = 180)
public static void repairMetadataSearchForID(String id) {
    LOGGER.info("Start the repair for the ID {}", id);
    if (!MCRObjectID.isValid(id)) {
        LOGGER.error("The String {} is not a MCRObjectID.", id);
        return;
    }
    MCRObjectID mid = MCRObjectID.getInstance(id);
    MCRBase obj = MCRMetadataManager.retrieve(mid);
    MCRMetadataManager.fireRepairEvent(obj);
    LOGGER.info("Repaired {}", mid);
}
Also used : MCRBase(org.mycore.datamodel.metadata.MCRBase) 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