Search in sources :

Example 1 with MCRTopologicalSort

use of org.mycore.tools.MCRTopologicalSort in project mycore by MyCoRe-Org.

the class MCRObjectCommands method processFromDirectory.

/**
 * Load or update MCRObject's from all XML files in a directory.
 *
 * @param topological
 *            if true, the dependencies of parent and child objects will be respected
 * @param directory
 *            the directory containing the XML files
 * @param update
 *            if true, object will be updated, else object is created
 */
private static List<String> processFromDirectory(boolean topological, String directory, boolean update) {
    File dir = new File(directory);
    if (!dir.isDirectory()) {
        LOGGER.warn("{} ignored, is not a directory.", directory);
        return null;
    }
    String[] list = dir.list();
    if (list == null || list.length == 0) {
        LOGGER.warn("No files found in directory {}", directory);
        return null;
    }
    Predicate<String> isMetaXML = file -> file.endsWith(".xml") && !file.contains("derivate");
    Function<String, String> cmdFromFile = file -> (update ? "update" : "load") + " object from file " + new File(dir, file).getAbsolutePath();
    if (topological) {
        MCRTopologicalSort ts = new MCRTopologicalSort();
        ts.prepareData(list, dir);
        return Optional.ofNullable(ts.doTopoSort()).map(Arrays::stream).map(is -> is.mapToObj(i -> list[i])).orElse(Stream.empty()).filter(isMetaXML).map(cmdFromFile).collect(Collectors.toList());
    } else {
        return Arrays.stream(list).filter(isMetaXML).sorted().map(cmdFromFile).collect(Collectors.toList());
    }
}
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) File(java.io.File)

Example 2 with MCRTopologicalSort

use of org.mycore.tools.MCRTopologicalSort 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)

Aggregations

File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 URISyntaxException (java.net.URISyntaxException)2 URL (java.net.URL)2 Files (java.nio.file.Files)2 MessageFormat (java.text.MessageFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 Hashtable (java.util.Hashtable)2 List (java.util.List)2 Locale (java.util.Locale)2 Entry (java.util.Map.Entry)2 Objects (java.util.Objects)2 Optional (java.util.Optional)2 Function (java.util.function.Function)2 Predicate (java.util.function.Predicate)2