Skip to content

Commit 6d192e4

Browse files
authored
MIR-1343 Do not render mir-admindata-box staticly (#1040)
* MIR-1343 Do not render mir-admindata-box in advance * MIR-1343 Do not generate table in mir-history-static.xsl just get the version history * MIR-1343 Removed mir-admindata-box-static.xsl * MIR-1341 Shorten texts for createdby and modifiedby in mir-admindata-box.xsl * MIR-1343 Added class MIRMigrationStaticContent * MIR-1343 Wrap code into MCRTransactionableRunnable * MIR-1343 Rename MIRMigrationStaticContent to MIRMigrateStaticHistoryContent * MIR-1343 Code format * MIR-1343 Check for valid mcrid * MIR-1343 Added extra MCRJob and check whether content was already migrated * MIR-1343 Fixed sort pom issue * MIR-1343 Check for MCRObjectID before parsing history file * MIR-1343 Set priority of MIRMigrateStaticHistoryContent to Integer.MIN_VALUE * MIR-1343 Use getStringOrThrow for retrieving location of static history files
1 parent aa31095 commit 6d192e4

File tree

11 files changed

+356
-228
lines changed

11 files changed

+356
-228
lines changed

mir-module/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@
196196
<groupId>org.mycore</groupId>
197197
<artifactId>mycore-classifications</artifactId>
198198
</dependency>
199+
<dependency>
200+
<groupId>org.mycore</groupId>
201+
<artifactId>mycore-jobqueue</artifactId>
202+
</dependency>
199203
<dependency>
200204
<groupId>org.mycore</groupId>
201205
<artifactId>mycore-mods</artifactId>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.mycore.mir.migration;
2+
3+
import jakarta.persistence.EntityManager;
4+
import jakarta.persistence.criteria.CriteriaBuilder;
5+
import jakarta.persistence.criteria.CriteriaQuery;
6+
import jakarta.persistence.criteria.Predicate;
7+
import jakarta.persistence.criteria.Root;
8+
import jakarta.servlet.ServletContext;
9+
import org.mycore.backend.jpa.MCREntityManagerProvider;
10+
import org.mycore.common.events.MCRStartupHandler.AutoExecutable;
11+
import org.mycore.services.queuedjob.MCRJob;
12+
import org.mycore.services.queuedjob.MCRJobQueue;
13+
import org.mycore.services.queuedjob.MCRJobQueueManager;
14+
import org.mycore.services.queuedjob.MCRJobStatus;
15+
import org.mycore.services.queuedjob.MCRJob_;
16+
import org.mycore.util.concurrent.MCRTransactionableRunnable;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
21+
/**
22+
* Class creates {@link MCRJob} in {@link MCRJobQueue} that migrates all static history content.
23+
*
24+
* @author shermann (Silvio Hermann)
25+
* */
26+
public class MIRMigrateStaticHistoryContent implements AutoExecutable {
27+
28+
@Override
29+
public String getName() {
30+
return MIRMigrateStaticHistoryContent.class.getName();
31+
}
32+
33+
@Override
34+
public int getPriority() {
35+
return Integer.MIN_VALUE;
36+
}
37+
38+
@Override
39+
public void startUp(ServletContext servletContext) {
40+
MCRTransactionableRunnable runnable = new MCRTransactionableRunnable(() -> {
41+
if (!alreadyDone()) {
42+
MCRJobQueueManager
43+
.getInstance()
44+
.getJobQueue(MIRMigrateStaticHistoryContentJobAction.class)
45+
.offer(new MCRJob(MIRMigrateStaticHistoryContentJobAction.class));
46+
}
47+
});
48+
new Thread(runnable).start();
49+
}
50+
51+
private boolean alreadyDone() {
52+
EntityManager manager = MCREntityManagerProvider.getCurrentEntityManager();
53+
CriteriaBuilder builder = manager.getCriteriaBuilder();
54+
CriteriaQuery<MCRJob> criteria = builder.createQuery(MCRJob.class);
55+
Root<MCRJob> root = criteria.from(MCRJob.class);
56+
57+
List<Predicate> predicates = new ArrayList<Predicate>();
58+
predicates.add(builder.equal(root.get(MCRJob_.action), MIRMigrateStaticHistoryContentJobAction.class));
59+
predicates.add(builder.equal(root.get(MCRJob_.status), MCRJobStatus.FINISHED));
60+
61+
criteria.where(predicates.toArray(new Predicate[] {}));
62+
List<MCRJob> resultList = manager.createQuery(criteria).getResultList();
63+
64+
return resultList.size() > 0;
65+
}
66+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package org.mycore.mir.migration;
2+
3+
import org.apache.logging.log4j.LogManager;
4+
import org.apache.logging.log4j.Logger;
5+
import org.jdom2.Document;
6+
import org.jdom2.input.SAXBuilder;
7+
import org.mycore.common.config.MCRConfiguration2;
8+
import org.mycore.datamodel.metadata.MCRMetadataManager;
9+
import org.mycore.datamodel.metadata.MCRObjectID;
10+
import org.mycore.services.queuedjob.MCRJob;
11+
import org.mycore.services.queuedjob.MCRJobAction;
12+
import org.mycore.services.queuedjob.staticcontent.MCRJobStaticContentGenerator;
13+
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.io.InputStream;
17+
import java.nio.file.FileVisitResult;
18+
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
import java.nio.file.SimpleFileVisitor;
21+
import java.nio.file.attribute.BasicFileAttributes;
22+
import java.util.concurrent.ExecutionException;
23+
24+
/**
25+
* Class converts all static history content on start up automatically.
26+
*
27+
* @author shermann (Silvio Hermann)
28+
* */
29+
public class MIRMigrateStaticHistoryContentJobAction extends MCRJobAction {
30+
private Path staticHistoryPath;
31+
private Logger logger;
32+
33+
public MIRMigrateStaticHistoryContentJobAction(MCRJob job) {
34+
super(job);
35+
logger = LogManager.getLogger(MIRMigrateStaticHistoryContentJobAction.class);
36+
37+
staticHistoryPath = Path.of(
38+
MCRConfiguration2.getStringOrThrow("MCR.Object.Static.Content.Default.Path") + File.separator
39+
+ "mir-history");
40+
}
41+
42+
@Override
43+
public void execute() throws ExecutionException {
44+
try {
45+
SAXBuilder builder = new SAXBuilder();
46+
Files.walkFileTree(staticHistoryPath, new SimpleFileVisitor<Path>() {
47+
@Override
48+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
49+
String filename = file.getFileName().toString();
50+
String id = filename.substring(0, filename.lastIndexOf('.'));
51+
52+
if (!MCRObjectID.isValid(id)) {
53+
return FileVisitResult.CONTINUE;
54+
}
55+
56+
try (InputStream is = Files.newInputStream(file)) {
57+
Document history = builder.build(is);
58+
if ("table".equals(history.getRootElement().getName())) {
59+
logger.info("Migrating static history for object {}", id);
60+
MCRJobStaticContentGenerator generator = new MCRJobStaticContentGenerator(
61+
"mir-history");
62+
generator.generate(MCRMetadataManager.retrieveMCRObject(MCRObjectID.getInstance(id)));
63+
}
64+
} catch (Exception e) {
65+
logger.error("Could not migrate static mcr-history for file {}", file.getFileName(), e);
66+
}
67+
return FileVisitResult.CONTINUE;
68+
}
69+
});
70+
} catch (IOException e) {
71+
logger.error("Error occurred during migration of static history content", e);
72+
}
73+
}
74+
75+
@Override
76+
public boolean isActivated() {
77+
return true;
78+
}
79+
80+
@Override
81+
public String name() {
82+
return MIRMigrateStaticHistoryContent.class.getName();
83+
}
84+
85+
@Override
86+
public void rollback() {
87+
//not implemented
88+
}
89+
}

mir-module/src/main/resources/config/mir/messages_de.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ mir.editor.subject.provider.variantName = Namensvarianten
180180
mir.editor.subject.provider.website = Webseite
181181
mir.editor.subject.provider.gndLink = GND-Link
182182
mir.editor.subject.provider.place.biographicalOrHistoricalInformation = Biographische oder historische Informationen
183-
183+
mir.metaData.detailBox.by = von
184184

185185
mir.subject.bagde.gnd = GND
186186
mir.subject.bagde.lcsh = LCSH

mir-module/src/main/resources/config/mir/messages_en.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ mir.editor.subject.provider.variantName = Variant Name
174174
mir.editor.subject.provider.website = Website
175175
mir.editor.subject.provider.gndLink = GND Link
176176
mir.editor.subject.provider.place.biographicalOrHistoricalInformation = Biographical or Historical Information
177-
177+
mir.metaData.detailBox.by = by
178178
img.alt.search.complex = complex search
179179
img.alt.search.simple = simple search
180180

mir-module/src/main/resources/config/mir/mycore.properties

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -723,12 +723,9 @@ MCR.URIResolver.xslImports.modsmeta=%MCR.URIResolver.xslImports.modsmeta%,toc/my
723723
# Static content #
724724
##############################################################################
725725
MCR.ContentTransformer.mir-history.Stylesheet=xsl/metadata/static/mir-history-static.xsl
726-
MCR.ContentTransformer.mir-admindata-box.Stylesheet=xsl/metadata/static/mir-admindata-box-static.xsl
727726

728727
MCR.Object.Static.Content.Generator.mir-history.Class=org.mycore.services.queuedjob.staticcontent.MCRJobStaticContentGenerator
729728
MCR.Object.Static.Content.Generator.mir-history.Transformer=mir-history
730-
MCR.Object.Static.Content.Generator.mir-admindata-box.Class=org.mycore.services.queuedjob.staticcontent.MCRJobStaticContentGenerator
731-
MCR.Object.Static.Content.Generator.mir-admindata-box.Transformer=mir-admindata-box
732729

733730
##############################################################################
734731
# Workflow #
@@ -783,3 +780,5 @@ MIR.Response.Facet.mods.genre.ClassId=mir_genres
783780

784781
MCR.ContentTransformer.svg-download.Class = org.mycore.common.content.transformer.MCRXSLTransformer
785782
MCR.ContentTransformer.svg-download.Stylesheet = xslt/generate-svg-by-type.xsl
783+
784+
MCR.Startup.Class = %MCR.Startup.Class%,org.mycore.mir.migration.MIRMigrateStaticHistoryContent
Lines changed: 123 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,129 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xlink="http://www.w3.org/1999/xlink"
3-
xmlns:i18n="xalan://org.mycore.services.i18n.MCRTranslation" xmlns:mods="http://www.loc.gov/mods/v3"
4-
xmlns:mcrxsl="xalan://org.mycore.common.xml.MCRXMLFunctions" exclude-result-prefixes="i18n mods xlink mcrxsl">
5-
<xsl:import href="xslImport:modsmeta:metadata/mir-admindata-box.xsl" />
6-
<!-- copied from http://www.loc.gov/standards/mods/v3/MODS3-4_HTML_XSLT1-0.xsl -->
2+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
3+
xmlns:xlink="http://www.w3.org/1999/xlink"
4+
xmlns:i18n="xalan://org.mycore.services.i18n.MCRTranslation" xmlns:mods="http://www.loc.gov/mods/v3"
5+
xmlns:mcrxsl="xalan://org.mycore.common.xml.MCRXMLFunctions"
6+
exclude-result-prefixes="i18n mods xlink mcrxsl">
7+
<xsl:import href="xslImport:modsmeta:metadata/mir-admindata-box.xsl"/>
8+
79
<xsl:template match="/">
8-
<xsl:variable name="ID" select="/mycoreobject/@ID" />
10+
<xsl:variable name="ID" select="/mycoreobject/@ID"/>
911
<div id="mir-admindata">
10-
<xsl:copy-of select="document(concat('staticcontent:mir-admindata-box:', $ID))/div" />
12+
<div id="system_box" class="detailbox">
13+
<h4 id="system_switch" class="block_switch">
14+
<xsl:value-of select="i18n:translate('component.mods.metaData.dictionary.systembox')"/>
15+
</h4>
16+
<div id="system_content" class="block_content">
17+
<table class="metaData">
18+
<!--*** publication status ************************************* -->
19+
<tr>
20+
<td class="metaname">
21+
<xsl:value-of select="concat(i18n:translate('component.mods.metaData.dictionary.status'),':')"/>
22+
</td>
23+
<td class="metavalue">
24+
<xsl:call-template name="printClass">
25+
<xsl:with-param select="mycoreobject/service/servstates/servstate" name="nodes"/>
26+
</xsl:call-template>
27+
</td>
28+
</tr>
29+
<xsl:call-template name="printMetaDate">
30+
<xsl:with-param select="mycoreobject/service/servdates/servdate[@type='createdate']" name="nodes"/>
31+
<xsl:with-param select="i18n:translate('metaData.createdAt')" name="label"/>
32+
</xsl:call-template>
33+
<xsl:call-template name="printMetaDate">
34+
<xsl:with-param select="mycoreobject/service/servflags/servflag[@type='createdby']" name="nodes"/>
35+
<xsl:with-param select="i18n:translate('mir.metaData.detailBox.by')" name="label"/>
36+
</xsl:call-template>
37+
<xsl:for-each select="mycoreobject/metadata/def.modsContainer/modsContainer/mods:mods/mods:note">
38+
<xsl:variable name="noteType">
39+
<xsl:choose>
40+
<xsl:when test="@type">
41+
<xsl:value-of select="@type"/>
42+
</xsl:when>
43+
<xsl:otherwise>
44+
<xsl:value-of select="'admin'"/>
45+
</xsl:otherwise>
46+
</xsl:choose>
47+
</xsl:variable>
48+
<xsl:variable name="myURI"
49+
select="concat('classification:metadata:0:children:noteTypes:', mcrxsl:regexp($noteType,' ', '_'))"/>
50+
<xsl:variable name="x-access">
51+
<xsl:value-of select="document($myURI)//label[@xml:lang='x-access']/@text"/>
52+
</xsl:variable>
53+
<xsl:variable name="noteLabel">
54+
<xsl:value-of select="document($myURI)//category/label[@xml:lang=$CurrentLang]/@text"/>
55+
</xsl:variable>
56+
<xsl:if test="contains($x-access, 'editor') or contains($x-access, 'admin')">
57+
<xsl:call-template name="printMetaDate">
58+
<xsl:with-param select="." name="nodes"/>
59+
<xsl:with-param select="$noteLabel" name="label"/>
60+
</xsl:call-template>
61+
</xsl:if>
62+
</xsl:for-each>
63+
<!--*** Last Modified ************************************* -->
64+
<xsl:call-template name="printMetaDate">
65+
<xsl:with-param select="mycoreobject/service/servdates/servdate[@type='modifydate']" name="nodes"/>
66+
<xsl:with-param select="i18n:translate('metaData.lastChanged')" name="label"/>
67+
</xsl:call-template>
68+
<xsl:call-template name="printMetaDate">
69+
<xsl:with-param select="mycoreobject/service/servflags/servflag[@type='modifiedby']" name="nodes"/>
70+
<xsl:with-param select="i18n:translate('mir.metaData.detailBox.by')" name="label"/>
71+
</xsl:call-template>
72+
<!--*** MyCoRe-ID and intern ID *************************** -->
73+
<tr>
74+
<td class="metaname">
75+
<xsl:value-of select="concat(i18n:translate('metaData.ID'),':')"/>
76+
</td>
77+
<td class="metavalue">
78+
<xsl:value-of select="mycoreobject/@ID"/>
79+
</td>
80+
</tr>
81+
<xsl:call-template name="printMetaDate">
82+
<xsl:with-param
83+
select="mycoreobject/metadata/def.modsContainer/modsContainer/mods:mods/mods:identifier[@type='intern']"
84+
name="nodes"/>
85+
<xsl:with-param select="i18n:translate('component.mods.metaData.dictionary.identifier.intern')"
86+
name="label"/>
87+
</xsl:call-template>
88+
89+
<tr>
90+
<td class="metaname">
91+
<xsl:value-of select="i18n:translate('metadata.versionInfo.version')"/>
92+
<xsl:text>:</xsl:text>
93+
</td>
94+
<td class="metavalue">
95+
<xsl:variable name="verinfo" select="document(concat('versioninfo:',mycoreobject/@ID))"/>
96+
<xsl:variable name="revision">
97+
<xsl:call-template name="UrlGetParam">
98+
<xsl:with-param name="url" select="$RequestURL"/>
99+
<xsl:with-param name="par" select="'r'"/>
100+
</xsl:call-template>
101+
</xsl:variable>
102+
<xsl:choose>
103+
<xsl:when test="not($revision = '')">
104+
<xsl:for-each select="$verinfo/versions/version">
105+
<xsl:sort order="descending" select="position()" data-type="number"/>
106+
<xsl:if test="$revision = @r">
107+
<xsl:number/>
108+
</xsl:if>
109+
</xsl:for-each>
110+
</xsl:when>
111+
<xsl:otherwise>
112+
<xsl:value-of select="count($verinfo/versions/version)"/>
113+
</xsl:otherwise>
114+
</xsl:choose>
115+
<br/>
116+
<a id="historyStarter" style="cursor: pointer">
117+
<xsl:value-of select="i18n:translate('metadata.versionInfo.startLabel')"/>
118+
</a>
119+
</td>
120+
</tr>
121+
</table>
122+
</div>
123+
</div>
124+
11125
</div>
12-
<xsl:apply-imports />
126+
<xsl:apply-imports/>
13127
</xsl:template>
14128

15-
</xsl:stylesheet>
129+
</xsl:stylesheet>

mir-module/src/main/resources/xsl/metadata/mir-history.xsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
<xsl:template match="/">
88
<xsl:variable name="ID" select="/mycoreobject/@ID" />
99
<div id="mir-historydata" class="table-responsive col-sm-12">
10-
<xsl:copy-of select="document(concat('staticcontent:mir-history:', $ID))/table" />
10+
<xsl:copy-of select="document(concat('staticcontent:mir-history:', $ID))" />
1111
</div>
1212
<xsl:apply-imports />
1313
</xsl:template>
1414

15-
</xsl:stylesheet>
15+
</xsl:stylesheet>

0 commit comments

Comments
 (0)