From 6b3493f93e4e6c86c349a43d100f70f1d1924053 Mon Sep 17 00:00:00 2001 From: Torsten Krause Date: Wed, 4 Sep 2024 16:52:33 +0200 Subject: [PATCH] MIR-1351 consider case of a fresh repository w/o existing static data --- .../MIRMigrateStaticHistoryContent.java | 22 ++++++++++++-- ...RMigrateStaticHistoryContentJobAction.java | 30 ++++++++----------- 2 files changed, 32 insertions(+), 20 deletions(-) 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..2f6ef4a84e 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 @@ -6,7 +6,9 @@ import jakarta.persistence.criteria.Predicate; import jakarta.persistence.criteria.Root; import jakarta.servlet.ServletContext; + 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,9 +17,15 @@ import org.mycore.services.queuedjob.MCRJob_; import org.mycore.util.concurrent.MCRTransactionableRunnable; +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; + /** * Class creates {@link MCRJob} in {@link MCRJobQueue} that migrates all static history content. * @@ -25,6 +33,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 +52,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 +79,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..33c9374ff9 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,17 +1,11 @@ package org.mycore.mir.migration; -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; @@ -21,31 +15,31 @@ 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; + /** * 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 +50,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); } }