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