From 7ff7442c5757c22fff06f7d37a5c4c097b171386 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 | 17 ++++++++++++- ...RMigrateStaticHistoryContentJobAction.java | 24 ++++++++----------- 2 files changed, 26 insertions(+), 15 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 6dfb4379f9..3981abcf6e 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,11 @@ import jakarta.persistence.criteria.Predicate; import jakarta.persistence.criteria.Root; import jakarta.servlet.ServletContext; + +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,6 +19,8 @@ import org.mycore.services.queuedjob.MCRJob_; import org.mycore.util.concurrent.MCRTransactionableRunnable; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -25,6 +31,11 @@ * */ 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"), "mir-history"); + @Override public String getName() { return MIRMigrateStaticHistoryContent.class.getName(); @@ -38,7 +49,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) 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..dcd6ad0123 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,17 @@ package org.mycore.mir.migration; +import static org.mycore.mir.migration.MIRMigrateStaticHistoryContent.STATIC_HISTORY_PATH; + 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 +21,27 @@ 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(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 +52,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); } }