use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRUserCommands method exportRole.
/**
* Exports a single role to the specified directory.
* @throws FileNotFoundException if target directory does not exist
*/
@MCRCommand(syntax = "export role {0} to directory {1}", help = "Export the role {0} to the directory {1}. The filename will be {0}.xml")
public static void exportRole(String roleID, String directory) throws IOException {
MCRRole mcrRole = MCRRoleManager.getRole(roleID);
File targetFile = new File(directory, roleID + ".xml");
try (FileOutputStream fout = new FileOutputStream(targetFile)) {
XMLOutputter xout = new XMLOutputter(Format.getPrettyFormat().setEncoding(MCRConstants.DEFAULT_ENCODING));
xout.output(MCRRoleTransformer.buildExportableXML(mcrRole), fout);
}
}
use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRMetadataHistoryCommands method clearHistory.
@MCRCommand(syntax = "clear metadata history of base {0}", help = "clears metadata history of all objects with base id {0}")
public static void clearHistory(String baseId) {
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaDelete<MCRMetaHistoryItem> delete = cb.createCriteriaDelete(MCRMetaHistoryItem.class);
Root<MCRMetaHistoryItem> item = delete.from(MCRMetaHistoryItem.class);
int rowsDeleted = em.createQuery(delete.where(cb.like(item.get(MCRMetaHistoryItem_.id).as(String.class), baseId + "_".replace("_", "$_") + '%', '$'))).executeUpdate();
LogManager.getLogger().info("Deleted {} items in history of {}", rowsDeleted, baseId);
}
use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRMetadataHistoryCommands method clearSingleHistory.
@MCRCommand(syntax = "clear metadata history of id {0}", help = "clears metadata history of object/derivate with id {0}")
public static void clearSingleHistory(String mcrId) {
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaDelete<MCRMetaHistoryItem> delete = cb.createCriteriaDelete(MCRMetaHistoryItem.class);
Root<MCRMetaHistoryItem> item = delete.from(MCRMetaHistoryItem.class);
int rowsDeleted = em.createQuery(delete.where(cb.equal(item.get(MCRMetaHistoryItem_.id).as(String.class), mcrId))).executeUpdate();
LogManager.getLogger().info("Deleted {} items in history of {}", rowsDeleted, mcrId);
}
use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRMetadataHistoryCommands method buildSingleHistory.
@MCRCommand(syntax = "build metadata history of id {0}", help = "build metadata history of object/derivate with id {0}")
public static void buildSingleHistory(String mcrId) {
MCRObjectID objId = MCRObjectID.getInstance(mcrId);
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
getHistoryItems(objId).sequential().forEach(em::persist);
}
use of org.mycore.frontend.cli.annotation.MCRCommand in project mycore by MyCoRe-Org.
the class MCRMetadataHistoryCommands method buildHistory.
@MCRCommand(syntax = "build metadata history of base {0}", help = "build metadata history of all objects with base id {0}")
public static List<String> buildHistory(String baseId) {
MCRMetadataStore store = MCRXMLMetadataManager.instance().getStore(baseId);
if (store instanceof MCRVersioningMetadataStore) {
LogManager.getLogger().info("Verify SVN history of {}", baseId);
((MCRVersioningMetadataStore) store).verify();
}
ExecutorService executorService = Executors.newWorkStealingPool();
MCRSession currentSession = MCRSessionMgr.getCurrentSession();
int maxId = store.getHighestStoredID();
AtomicInteger completed = new AtomicInteger(maxId);
IntStream.rangeClosed(1, maxId).parallel().mapToObj(i -> MCRObjectID.formatID(baseId, i)).map(MCRObjectID::getInstance).map(id -> new MCRTransactionableCallable<>(Executors.callable(() -> {
EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
getHistoryItems(id).sequential().forEach(em::persist);
completed.decrementAndGet();
}), currentSession)).forEach(executorService::submit);
executorService.shutdown();
boolean waitToFinish = true;
while (!executorService.isTerminated() && waitToFinish) {
LogManager.getLogger().info("Waiting for history of {} objects/derivates.", completed.get());
try {
executorService.awaitTermination(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
waitToFinish = false;
}
}
return Collections.emptyList();
}
Aggregations