Skip to content

Commit

Permalink
add multiple values for same key in mass import csv
Browse files Browse the repository at this point in the history
  • Loading branch information
BartChris committed Mar 7, 2024
1 parent 023d949 commit 14b9aa0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public void startMassImport() {
importSuccessMap = new HashMap<>();
PrimeFaces.current().ajax().update("massImportResultDialog");
try {
Map<String, Map<String, String>> presetMetadata = massImportService.prepareMetadata(metadataKeys, records);
Map<String, Map<String, List<String>>> presetMetadata = massImportService.prepareMetadata(metadataKeys, records);
importRecords(presetMetadata);
PrimeFaces.current().executeScript("PF('massImportResultDialog').show();");
PrimeFaces.current().ajax().update("massImportResultDialog");
Expand Down Expand Up @@ -181,10 +181,10 @@ public void prepare() {
*
* @param processMetadata Map containing record IDs as keys and preset metadata lists as values
*/
private void importRecords(Map<String, Map<String, String>> processMetadata) {
private void importRecords(Map<String, Map<String, List<String>>> processMetadata) {
ImportService importService = ServiceManager.getImportService();
PrimeFaces.current().ajax().update("massImportProgressDialog");
for (Map.Entry<String, Map<String, String>> entry : processMetadata.entrySet()) {
for (Map.Entry<String, Map<String, List<String>>> entry : processMetadata.entrySet()) {
try {
importService.importProcess(entry.getKey(), projectId, templateId, importConfiguration,
entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ public static void processTempProcess(TempProcess tempProcess, RulesetManagement
* @return the importedProcess
*/
public Process importProcess(String ppn, int projectId, int templateId, ImportConfiguration importConfiguration,
Map<String, String> presetMetadata) throws ImportException {
Map<String, List<String>> presetMetadata) throws ImportException {
LinkedList<TempProcess> processList = new LinkedList<>();
TempProcess tempProcess;
Template template;
Expand Down Expand Up @@ -1257,14 +1257,16 @@ private static Collection<String> getFunctionalMetadata(Ruleset ruleset, Functio
return rulesetManagement.getFunctionalKeys(metadata);
}

private List<MetadataEntry> createMetadata(Map<String, String> presetMetadata) {
private List<MetadataEntry> createMetadata(Map<String, List<String>> presetMetadata) {
List<MetadataEntry> metadata = new LinkedList<>();
for (Map.Entry<String, String> presetMetadataEntry : presetMetadata.entrySet()) {
MetadataEntry metadataEntry = new MetadataEntry();
metadataEntry.setKey(presetMetadataEntry.getKey());
metadataEntry.setValue(presetMetadataEntry.getValue());
metadataEntry.setDomain(MdSec.DMD_SEC);
metadata.add(metadataEntry);
for (Map.Entry<String, List<String>> presetMetadataEntry : presetMetadata.entrySet()) {
for (String presetMetadataEntryValue : presetMetadataEntry.getValue()) {
MetadataEntry metadataEntry = new MetadataEntry();
metadataEntry.setKey(presetMetadataEntry.getKey());
metadataEntry.setValue(presetMetadataEntryValue);
metadataEntry.setDomain(MdSec.DMD_SEC);
metadata.add(metadataEntry);
}
}
return metadata;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ public List<CsvRecord> parseLines(List<String> lines, String separator) {
* @param metadataKeys metadata keys for additional metadata added to individual records during import
* @param records list of CSV records
*/
public Map<String, Map<String, String>> prepareMetadata(List<String> metadataKeys, List<CsvRecord> records)
public Map<String, Map<String, List<String>>> prepareMetadata(List<String> metadataKeys, List<CsvRecord> records)
throws ImportException {
Map<String, Map<String, String>> presetMetadata = new LinkedHashMap<>();
Map<String, Map<String, List<String>>> presetMetadata = new LinkedHashMap<>();
for (CsvRecord record : records) {
Map<String, String> processMetadata = new HashMap<>();
Map<String, List<String>> processMetadata = new HashMap<>();
// skip first metadata key as it always contains the record ID to be used for search
for (int index = 1; index < metadataKeys.size(); index++) {
String metadataKey = metadataKeys.get(index);
if (StringUtils.isNotBlank(metadataKey)) {
processMetadata.put(metadataKey, record.getCsvCells().get(index).getValue());
List<String> values = processMetadata.computeIfAbsent(metadataKey, k -> new ArrayList<>());
values.add(record.getCsvCells().get(index).getValue());
}
}
presetMetadata.put(record.getCsvCells().get(0).getValue(), processMetadata);
Expand Down

0 comments on commit 14b9aa0

Please sign in to comment.