Skip to content

Commit

Permalink
MIR-1343 Added class MIRMigrationStaticContent
Browse files Browse the repository at this point in the history
  • Loading branch information
Possommi committed Aug 20, 2024
1 parent 65f9518 commit 841910d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.mycore.mir.migration;

import jakarta.servlet.ServletContext;
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.common.events.MCRStartupHandler.AutoExecutable;
import org.mycore.datamodel.metadata.MCRMetadataManager;
import org.mycore.datamodel.metadata.MCRObjectID;
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;

/**
* Class converts automatically all static history content on start up.
*
* @author shermann (Silvio Hermann)
* */
public class MIRMigrationStaticContent implements AutoExecutable {

protected static final Path MIR_STATIC_HISTORY_PATH = Path.of(
MCRConfiguration2.getString("MCR.Object.Static.Content.Default.Path").get() + File.separator + "mir-history");

private static Logger LOGGER = LogManager.getLogger(MIRMigrationStaticContent.class);

@Override
public String getName() {

return MIRMigrationStaticContent.class.getName();
}

@Override
public int getPriority() {
return 0;
}

@Override
public void startUp(ServletContext servletContext) {
try {
SAXBuilder builder = new SAXBuilder();
Files.walkFileTree(MIR_STATIC_HISTORY_PATH, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
try (InputStream is = Files.newInputStream(file)) {
Document history = builder.build(is);
if ("table".equals(history.getRootElement().getName())) {
String filename = file.getFileName().toString();
String id = filename.substring(0, filename.lastIndexOf('.'));
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);
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
LOGGER.error("Error occured during migration of static history content", e);
}
}
}
2 changes: 2 additions & 0 deletions mir-module/src/main/resources/config/mir/mycore.properties
Original file line number Diff line number Diff line change
Expand Up @@ -780,3 +780,5 @@ MIR.Response.Facet.mods.genre.ClassId=mir_genres

MCR.ContentTransformer.svg-download.Class = org.mycore.common.content.transformer.MCRXSLTransformer
MCR.ContentTransformer.svg-download.Stylesheet = xslt/generate-svg-by-type.xsl

MCR.Startup.Class = %MCR.Startup.Class%,org.mycore.mir.migration.MIRMigrationStaticContent

0 comments on commit 841910d

Please sign in to comment.