diff --git a/mir-module/src/main/java/org/mycore/mir/migration/MIRMigrateStaticHistoryContent.java b/mir-module/src/main/java/org/mycore/mir/migration/MIRMigrateStaticHistoryContent.java index 21e382df6c..e32f369f7b 100644 --- a/mir-module/src/main/java/org/mycore/mir/migration/MIRMigrateStaticHistoryContent.java +++ b/mir-module/src/main/java/org/mycore/mir/migration/MIRMigrateStaticHistoryContent.java @@ -1,12 +1,15 @@ package org.mycore.mir.migration; -import jakarta.persistence.EntityManager; -import jakarta.persistence.criteria.CriteriaBuilder; -import jakarta.persistence.criteria.CriteriaQuery; -import jakarta.persistence.criteria.Predicate; -import jakarta.persistence.criteria.Root; -import jakarta.servlet.ServletContext; +import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.mycore.backend.jpa.MCREntityManagerProvider; +import org.mycore.common.config.MCRConfiguration2; import org.mycore.common.events.MCRStartupHandler.AutoExecutable; import org.mycore.services.queuedjob.MCRJob; import org.mycore.services.queuedjob.MCRJobQueue; @@ -15,8 +18,12 @@ import org.mycore.services.queuedjob.MCRJob_; import org.mycore.util.concurrent.MCRTransactionableRunnable; -import java.util.ArrayList; -import java.util.List; +import jakarta.persistence.EntityManager; +import jakarta.persistence.criteria.CriteriaBuilder; +import jakarta.persistence.criteria.CriteriaQuery; +import jakarta.persistence.criteria.Predicate; +import jakarta.persistence.criteria.Root; +import jakarta.servlet.ServletContext; /** * Class creates {@link MCRJob} in {@link MCRJobQueue} that migrates all static history content. @@ -25,6 +32,12 @@ * */ public class MIRMigrateStaticHistoryContent implements AutoExecutable { + private static final Logger LOGGER = LogManager.getLogger(); + + static final Path STATIC_HISTORY_PATH = Path.of( + MCRConfiguration2.getStringOrThrow("MCR.Object.Static.Content.Default.Path") + + File.separator + "mir-history"); + @Override public String getName() { return MIRMigrateStaticHistoryContent.class.getName(); @@ -38,7 +51,11 @@ public int getPriority() { @Override public void startUp(ServletContext servletContext) { MCRTransactionableRunnable runnable = new MCRTransactionableRunnable(() -> { - if (!alreadyDone()) { + if (Files.notExists(STATIC_HISTORY_PATH)) { + LOGGER.info("No static content exists, nothing to do."); + } else if (alreadyDone()) { + LOGGER.info("Static content migration already scheduled, nothing to do."); + } else if (!alreadyDone()) { MCRJobQueueManager .getInstance() .getJobQueue(MIRMigrateStaticHistoryContentJobAction.class) @@ -61,6 +78,6 @@ private boolean alreadyDone() { criteria.where(predicates.toArray(new Predicate[] {})); List resultList = manager.createQuery(criteria).getResultList(); - return resultList.size() > 0; + return !resultList.isEmpty(); } } diff --git a/mir-module/src/main/java/org/mycore/mir/migration/MIRMigrateStaticHistoryContentJobAction.java b/mir-module/src/main/java/org/mycore/mir/migration/MIRMigrateStaticHistoryContentJobAction.java index a30c786de2..0313f7db5c 100644 --- a/mir-module/src/main/java/org/mycore/mir/migration/MIRMigrateStaticHistoryContentJobAction.java +++ b/mir-module/src/main/java/org/mycore/mir/migration/MIRMigrateStaticHistoryContentJobAction.java @@ -1,51 +1,44 @@ package org.mycore.mir.migration; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.concurrent.ExecutionException; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jdom2.Document; import org.jdom2.input.SAXBuilder; -import org.mycore.common.config.MCRConfiguration2; import org.mycore.datamodel.metadata.MCRMetadataManager; import org.mycore.datamodel.metadata.MCRObjectID; import org.mycore.services.queuedjob.MCRJob; import org.mycore.services.queuedjob.MCRJobAction; import org.mycore.services.queuedjob.staticcontent.MCRJobStaticContentGenerator; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.concurrent.ExecutionException; - /** * Class converts all static history content on start up automatically. * * @author shermann (Silvio Hermann) * */ public class MIRMigrateStaticHistoryContentJobAction extends MCRJobAction { - private Path staticHistoryPath; - private Logger logger; + + private static final Logger LOGGER = LogManager.getLogger(); public MIRMigrateStaticHistoryContentJobAction(MCRJob job) { super(job); - logger = LogManager.getLogger(MIRMigrateStaticHistoryContentJobAction.class); - - staticHistoryPath = Path.of( - MCRConfiguration2.getStringOrThrow("MCR.Object.Static.Content.Default.Path") + File.separator - + "mir-history"); } @Override public void execute() throws ExecutionException { try { SAXBuilder builder = new SAXBuilder(); - Files.walkFileTree(staticHistoryPath, new SimpleFileVisitor() { + Files.walkFileTree(MIRMigrateStaticHistoryContent.STATIC_HISTORY_PATH, new SimpleFileVisitor<>() { @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { String filename = file.getFileName().toString(); String id = filename.substring(0, filename.lastIndexOf('.')); @@ -56,19 +49,19 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO try (InputStream is = Files.newInputStream(file)) { Document history = builder.build(is); if ("table".equals(history.getRootElement().getName())) { - logger.info("Migrating static history for object {}", id); + LOGGER.info("Migrating static history for object {}", id); MCRJobStaticContentGenerator generator = new MCRJobStaticContentGenerator( "mir-history"); generator.generate(MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(id))); } } catch (Exception e) { - logger.error("Could not migrate static mcr-history for file {}", file.getFileName(), e); + LOGGER.error("Could not migrate static mcr-history for file {}", file.getFileName(), e); } return FileVisitResult.CONTINUE; } }); } catch (IOException e) { - logger.error("Error occurred during migration of static history content", e); + LOGGER.error("Error occurred during migration of static history content", e); } }