Skip to content

Commit

Permalink
UBO-378 Added class EnrichmentConfigMgr to reuse code in ImportListJo…
Browse files Browse the repository at this point in the history
…bAction
  • Loading branch information
Possommi committed Dec 3, 2024
1 parent 796d00e commit 82dfb7a
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String, String> DYNAMIC_ENRICHMENT_CONFIG_IDS = new HashMap<>();

@Override
public void doGetPost(MCRServletJob job) throws Exception {
HttpServletRequest req = job.getRequest();
Expand Down Expand Up @@ -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();
Expand All @@ -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<Element> 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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 <code>null</code>
*/
public String getDataSource(Element formInput) {
Optional<Element> dataSource = formInput.getChildren("DataSources")
.stream()
.filter(element -> !element.getText().isEmpty())
.findFirst();
return dataSource.isPresent() ? dataSource.get().getText() : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down

0 comments on commit 82dfb7a

Please sign in to comment.