Skip to content

Commit

Permalink
UBO-378 Load existing configs via uri resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
Possommi committed Dec 3, 2024
1 parent 82dfb7a commit 4a889de
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ private void handleImportJob(HttpServletRequest req, HttpServletResponse res) th

boolean enrich = "true".equals(formInput.getAttributeValue("enrich"));
if (enrich) {
String dataSources = EnrichmentConfigMgr.instance().getDataSource(formInput);
if (dataSources != null) {
String enricherId = EnrichmentConfigMgr.instance().getOrCreateEnrichmentConfig(dataSources);
String enricherId = EnrichmentConfigMgr.getEnricherId(formInput);
if (enricherId != null) {
importJob.enrich(enricherId);
} else {
importJob.enrich();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,42 @@
import org.jdom2.Element;
import org.mycore.common.config.MCRConfiguration2;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Optional;
import java.util.UUID;

/**
* Class handles dynamically created enrichment resolver configurations.
* Class handles retrieves the enricher id from the form input of import-list.xed.
*
* @author shermann (Silvio Hermann)
*/
public class EnrichmentConfigMgr {

private final HashMap<String, String> dynamicEnrichmentConfigIds = new HashMap<>();

private static EnrichmentConfigMgr INSTANCE;
static final String DEFAULT_CONFIG_ID = "import-list-custom";

private EnrichmentConfigMgr() {
}

/**
* Creates and returns a singleton instance of this class.
*
* @return the instance
*/
public static EnrichmentConfigMgr instance() {
if (INSTANCE != null) {
return INSTANCE;
}
INSTANCE = new EnrichmentConfigMgr();
return INSTANCE;
}

/**
* Creates and registers an enrichment configuration id.
*
* @param dataSources the data source
*
* @return the enrichment configuration id for the given data source
* */
public String getOrCreateEnrichmentConfig(String dataSources) {
if (dynamicEnrichmentConfigIds.containsKey(dataSources)) {
return dynamicEnrichmentConfigIds.get(dataSources);
}

String id = UUID.nameUUIDFromBytes(dataSources.getBytes(StandardCharsets.UTF_8)).toString();
String property = "MCR.MODS.EnrichmentResolver.DataSources." + id;
MCRConfiguration2.set(property, dataSources);
dynamicEnrichmentConfigIds.put(dataSources, id);
return id;
}

/**
* Retrieves the first DataSource text content from the import list form element.
* Retrieves the enricher id from the import list form element.
*
* @param formInput the form input (usually provided by import-list.xed)
*
* @return the first DataSource text or <code>null</code>
* @return the enricher id (maybe of type <code>custom</code>) text or <code>null</code>
*/
public String getDataSource(Element formInput) {
public static String getEnricherId(Element formInput) {
Optional<Element> dataSource = formInput.getChildren("DataSources")
.stream()
.filter(element -> !element.getText().isEmpty())
.findFirst();
return dataSource.isPresent() ? dataSource.get().getText() : null;

String enricherId = dataSource.isPresent() ? dataSource.get().getText() : null;
if (enricherId != null) {
if (MCRConfiguration2.getString("MCR.MODS.EnrichmentResolver.DataSources." + enricherId).isPresent()) {
return enricherId;
} else {
String property = "MCR.MODS.EnrichmentResolver.DataSources." + DEFAULT_CONFIG_ID;
MCRConfiguration2.set(property, dataSource.get().getText());
return DEFAULT_CONFIG_ID;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ public void execute() throws ExecutionException {
job.setParameter(IMPORT_JOB_ID_PARAMETER, importJob.getID());

if ("true".equals(formInput.getAttributeValue("enrich"))) {
String dataSources = EnrichmentConfigMgr.instance().getDataSource(formInput);
if (dataSources != null) {
String enricherId = EnrichmentConfigMgr.instance().getOrCreateEnrichmentConfig(dataSources);
String enricherId = EnrichmentConfigMgr.getEnricherId(formInput);
if (enricherId != null) {
importJob.enrich(enricherId);
} else {
importJob.enrich();
Expand Down
13 changes: 1 addition & 12 deletions ubo-common/src/main/resources/META-INF/resources/import-list.xed
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,7 @@
<option value="">
<xed:output i18n="search.select"/>
</option>
<option value="GBV">
GBV
</option>
<option value="{$MCR.MODS.EnrichmentResolver.DataSources.import}">
<xed:output value="$MCR.MODS.EnrichmentResolver.DataSources.import"/>
</option>
<option value="{$MCR.MODS.EnrichmentResolver.DataSources.import-list}">
<xed:output value="$MCR.MODS.EnrichmentResolver.DataSources.import-list"/>
</option>
<option value="{$MCR.MODS.EnrichmentResolver.DataSources.scopus-import}">
<xed:output value="$MCR.MODS.EnrichmentResolver.DataSources.scopus-import"/>
</option>
<xed:include uri="xslStyle:enrichers2options:enrichmentConfig:custom" />
</select>
</xed:bind>

Expand Down
19 changes: 19 additions & 0 deletions ubo-common/src/main/resources/xsl/enrichers2options.xsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xsl">
<xsl:output method="html"/>

<xsl:template match="/">
<result>
<xsl:apply-templates select="enrichmentDebugger/enrichers/enricher">
<xsl:sort select="@id"/>
</xsl:apply-templates>
</result>
</xsl:template>

<xsl:template match="enricher">
<option value="{@id}" title="{@id}">
<xsl:value-of select="text()"/>
</option>
</xsl:template>
</xsl:stylesheet>

0 comments on commit 4a889de

Please sign in to comment.