-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UBO-378 Added class EnrichmentConfigMgr to reuse code in ImportListJo…
…bAction
- Loading branch information
Showing
3 changed files
with
81 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
ubo-common/src/main/java/org/mycore/ubo/importer/EnrichmentConfigMgr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters