diff --git a/ubo-common/src/main/java/org/mycore/ubo/importer/DozBibImportServlet.java b/ubo-common/src/main/java/org/mycore/ubo/importer/DozBibImportServlet.java index 3ecae76e..e1d5bc46 100644 --- a/ubo-common/src/main/java/org/mycore/ubo/importer/DozBibImportServlet.java +++ b/ubo-common/src/main/java/org/mycore/ubo/importer/DozBibImportServlet.java @@ -15,7 +15,6 @@ import org.jdom2.Element; import org.jdom2.output.XMLOutputter; import org.mycore.access.MCRAccessException; -import org.mycore.common.config.MCRConfiguration2; import org.mycore.common.content.MCRJDOMContent; import org.mycore.frontend.MCRFrontendUtil; import org.mycore.frontend.servlets.MCRServlet; @@ -32,18 +31,10 @@ import java.io.IOException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Optional; -import java.util.UUID; @SuppressWarnings("serial") public class DozBibImportServlet extends MCRServlet { - /** - * Cache dynamically created enrichment - * */ - protected static final HashMap DYNAMIC_ENRICHMENT_CONFIG_IDS = new HashMap<>(); - @Override public void doGetPost(MCRServletJob job) throws Exception { HttpServletRequest req = job.getRequest(); @@ -83,9 +74,9 @@ private void handleImportJob(HttpServletRequest req, HttpServletResponse res) th boolean enrich = "true".equals(formInput.getAttributeValue("enrich")); if (enrich) { - String dataSources = getDataSource(formInput); + String dataSources = EnrichmentConfigMgr.instance().getDataSource(formInput); if (dataSources != null) { - String enricherId = getOrCreateEnrichmentConfig(dataSources); + String enricherId = EnrichmentConfigMgr.instance().getOrCreateEnrichmentConfig(dataSources); importJob.enrich(enricherId); } else { importJob.enrich(); @@ -100,33 +91,6 @@ private void handleImportJob(HttpServletRequest req, HttpServletResponse res) th } } - /** - * Creates and registers an enrichment configuration id. - * - * @param dataSources the data source - * - * @return the enrichment configuration id for the given data source - * */ - private String getOrCreateEnrichmentConfig(String dataSources) { - if (DYNAMIC_ENRICHMENT_CONFIG_IDS.containsKey(dataSources)) { - return DYNAMIC_ENRICHMENT_CONFIG_IDS.get(dataSources); - } - - String id = UUID.nameUUIDFromBytes(dataSources.getBytes(StandardCharsets.UTF_8)).toString(); - String property = "MCR.MODS.EnrichmentResolver.DataSources." + id; - MCRConfiguration2.set(property, dataSources); - DYNAMIC_ENRICHMENT_CONFIG_IDS.put(dataSources, id); - return id; - } - - private String getDataSource(Element formInput) { - Optional dataSource = formInput.getChildren("DataSources") - .stream() - .filter(element -> !element.getText().isEmpty()) - .findFirst(); - return dataSource.isPresent() ? dataSource.get().getText() : null; - } - private ImportJob buildImportJob(Element formInput) { String sourceType = formInput.getAttributeValue("sourceType"); return ("Evaluna".equals(sourceType) ? new EvalunaImportJob() : new ListImportJob(sourceType)); diff --git a/ubo-common/src/main/java/org/mycore/ubo/importer/EnrichmentConfigMgr.java b/ubo-common/src/main/java/org/mycore/ubo/importer/EnrichmentConfigMgr.java new file mode 100644 index 00000000..51fcad84 --- /dev/null +++ b/ubo-common/src/main/java/org/mycore/ubo/importer/EnrichmentConfigMgr.java @@ -0,0 +1,71 @@ +package org.mycore.ubo.importer; + +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. + * + * @author shermann (Silvio Hermann) + */ +public class EnrichmentConfigMgr { + + private final HashMap dynamicEnrichmentConfigIds = new HashMap<>(); + + private static EnrichmentConfigMgr INSTANCE; + + 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. + * + * @param formInput the form input (usually provided by import-list.xed) + * + * @return the first DataSource text or null + */ + public String getDataSource(Element formInput) { + Optional dataSource = formInput.getChildren("DataSources") + .stream() + .filter(element -> !element.getText().isEmpty()) + .findFirst(); + return dataSource.isPresent() ? dataSource.get().getText() : null; + } +} diff --git a/ubo-common/src/main/java/org/mycore/ubo/importer/ImportListJobAction.java b/ubo-common/src/main/java/org/mycore/ubo/importer/ImportListJobAction.java index 54e23c3c..154718c7 100644 --- a/ubo-common/src/main/java/org/mycore/ubo/importer/ImportListJobAction.java +++ b/ubo-common/src/main/java/org/mycore/ubo/importer/ImportListJobAction.java @@ -61,7 +61,13 @@ public void execute() throws ExecutionException { job.setParameter(IMPORT_JOB_ID_PARAMETER, importJob.getID()); if ("true".equals(formInput.getAttributeValue("enrich"))) { - importJob.enrich(); + String dataSources = EnrichmentConfigMgr.instance().getDataSource(formInput); + if (dataSources != null) { + String enricherId = EnrichmentConfigMgr.instance().getOrCreateEnrichmentConfig(dataSources); + importJob.enrich(enricherId); + } else { + importJob.enrich(); + } } try { @@ -80,7 +86,7 @@ private void sendMail(ImportJob importJob) { String userName = job.getParameter(ImportListJobAction.USER_ID_PARAMETER); MCRUser mcrUser = MCRUserManager.getUser(userName); - if(mcrUser == null) { + if (mcrUser == null) { LOGGER.error("User {} not found", userName); return; }