Search in sources :

Example 56 with MCRCommand

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);
    }
}
Also used : XMLOutputter(org.jdom2.output.XMLOutputter) FileOutputStream(java.io.FileOutputStream) File(java.io.File) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 57 with MCRCommand

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);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 58 with MCRCommand

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);
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 59 with MCRCommand

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);
}
Also used : EntityManager(javax.persistence.EntityManager) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Example 60 with MCRCommand

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();
}
Also used : IntStream(java.util.stream.IntStream) MCRTransactionableCallable(org.mycore.util.concurrent.MCRTransactionableCallable) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) ArrayList(java.util.ArrayList) JDOMException(org.jdom2.JDOMException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) CriteriaDelete(javax.persistence.criteria.CriteriaDelete) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) MCRCreatorCache(org.mycore.datamodel.common.MCRCreatorCache) ExecutorService(java.util.concurrent.ExecutorService) Root(javax.persistence.criteria.Root) MCRMetadataVersion(org.mycore.datamodel.ifs2.MCRMetadataVersion) MCRMetadataManager(org.mycore.datamodel.metadata.MCRMetadataManager) IOException(java.io.IOException) EntityManager(javax.persistence.EntityManager) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) MCRVersionedMetadata(org.mycore.datamodel.ifs2.MCRVersionedMetadata) List(java.util.List) Stream(java.util.stream.Stream) MCRSystemUserInformation(org.mycore.common.MCRSystemUserInformation) MCRVersioningMetadataStore(org.mycore.datamodel.ifs2.MCRVersioningMetadataStore) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRSession(org.mycore.common.MCRSession) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRSessionMgr(org.mycore.common.MCRSessionMgr) MCRObject(org.mycore.datamodel.metadata.MCRObject) SAXException(org.xml.sax.SAXException) Optional(java.util.Optional) MCRMetadataStore(org.mycore.datamodel.ifs2.MCRMetadataStore) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) MCRTransactionableCallable(org.mycore.util.concurrent.MCRTransactionableCallable) MCRMetadataStore(org.mycore.datamodel.ifs2.MCRMetadataStore) EntityManager(javax.persistence.EntityManager) MCRSession(org.mycore.common.MCRSession) MCRVersioningMetadataStore(org.mycore.datamodel.ifs2.MCRVersioningMetadataStore) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) 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