#24494 Converting multiple values stored as array (e.g. [a,b,c]) in

multiple MetadataFieldWrapper repeatable fields
This commit is contained in:
Francesco Mangiacrapa 2023-02-01 11:15:01 +01:00
parent b1a4a22cc6
commit 132891988a
1 changed files with 41 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package org.gcube.application.geoportaldatamapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
@ -219,24 +220,61 @@ public class Geoportal_JSON_Mapper {
List<MetadataFieldWrapper> cloneListOfMFW = cloneList(theProfileBean.getMetadataFields());
List<MetadataFieldWrapper> duplicatedList = new ArrayList<MetadataFieldWrapper>();
for (MetadataFieldWrapper metadataField : cloneListOfMFW) {
String theFieldName = metadataField.getFieldId() != null ? metadataField.getFieldId()
: metadataField.getFieldName();
LOG.debug("reading theFieldName: " + theFieldName);
Object theOBJFieldValue = fromSectionDoc.get(theFieldName);
metadataField.setCurrentValue(theOBJFieldValue + "");
if (access != null) {
if (theFieldName.equalsIgnoreCase("policy")) {
metadataField.setCurrentValue(access.getPolicy().name());
continue;
} else if (theFieldName.equalsIgnoreCase("licenseID")) {
metadataField.setCurrentValue(access.getLicense());
continue;
}
}
Object theOBJFieldValue = fromSectionDoc.get(theFieldName);
if(theOBJFieldValue!=null) {
//Converting multiple values stored as array (e.g. [a,b,c]) in multiple MetadataFieldWrapper
//repeatable fields
LOG.debug("value "+theOBJFieldValue+ " is instanceof Array");
try {
JSONArray dataArray = new JSONArray(theOBJFieldValue+"");
LOG.debug("It is an Array");
for (int j=0; j<dataArray.length(); j++) {
List<MetadataFieldWrapper> cloned = cloneList(Arrays.asList(metadataField));
MetadataFieldWrapper mfw = cloned.get(0);
mfw.setCurrentValue(dataArray.getString(j));
//From the second repeated field settings
//mandatory false and one instance
//These properties are managed properly with the
//first occurrence of the field
if(j>=1) {
mfw.setMandatory(false);
mfw.setMaxOccurs(1);
}
duplicatedList.add(mfw);
}
continue;
}catch (Exception e) {
LOG.debug("It is not an Array");
String theValue = theOBJFieldValue + "";
metadataField.setCurrentValue(theValue);
}
}
duplicatedList.add(metadataField);
}
theProfileBeanExt.setMetadataFields(new ArrayList<MetadataFieldWrapper>(cloneListOfMFW));
theProfileBeanExt.setMetadataFields(new ArrayList<MetadataFieldWrapper>(duplicatedList));
theProfileBeanExt.setDocumentSectionAsJSON(fromSectionDoc.toJson());
theProfileBeanExt.setGcubeProfile(gcubeProfileDV);
LOG.debug("Metadata fields are: " + theProfileBeanExt.getMetadataFields());