Skip to content

Commit

Permalink
MIR-1351 consider case of a fresh repository w/o existing static data
Browse files Browse the repository at this point in the history
  • Loading branch information
toKrause committed Sep 24, 2024
1 parent 6b5d2fe commit 7ff7442
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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();
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Path>() {
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('.'));

Expand All @@ -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);
}
}

Expand Down

0 comments on commit 7ff7442

Please sign in to comment.