Implements the logic that maps Dataset Description values to RDA export json file.

This commit is contained in:
gkolokythas 2019-10-17 13:11:13 +03:00
parent c96d1c7afe
commit 9d3f0ec1a2
2 changed files with 106 additions and 7 deletions

View File

@ -1,10 +1,21 @@
package eu.eudat.models.data.rda;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.data.entities.Dataset;
import eu.eudat.logic.utilities.builders.XmlBuilder;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.Date;
import javax.xml.xpath.*;
import java.util.*;
public class DatasetRDAExportModel {
private static final ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);;
private DatasetIdRDAExportModel dataset_id;
private String description;
private Date issued; // Created Date, could also use finalized one.
@ -49,12 +60,85 @@ public class DatasetRDAExportModel {
this.title = title;
}
public String getSensitive_data() {
return sensitive_data;
}
public void setSensitive_data(String sensitive_data) {
this.sensitive_data = sensitive_data;
}
public String getData_quality_assurance() {
return data_quality_assurance;
}
public void setData_quality_assurance(String data_quality_assurance) {
this.data_quality_assurance = data_quality_assurance;
}
public DatasetMetadataRDAExportModel getMetadata() {
return metadata;
}
public void setMetadata(DatasetMetadataRDAExportModel metadata) {
this.metadata = metadata;
}
public DatasetRDAExportModel fromDataModel(Dataset dataset) {
this.title = dataset.getLabel();
this.issued = dataset.getCreated();
this.language = "en"; // mock data
this.dataset_id = new DatasetIdRDAExportModel(dataset.getId().toString(), "argos_internal");
return this;
// Parsing dataset template definition to create a map of which question of the template corresponds to the RDA common standard. Map: TemplateId -> rdaProperty
List<String> rdaPropertiesThatExistOnDatasetTemplate = xmlNodeListFromExpression(dataset.getProfile().getDefinition(), "//rdaCommonStandard/text()");
List<String> rdaProperties = new LinkedList<>();
for (String item : rdaPropertiesThatExistOnDatasetTemplate) {
item = item.replace("dataset.", "");
rdaProperties.add(item);
}
List<String> datasetTemplateIdsWithRdaProperties = xmlNodeListFromExpression(dataset.getProfile().getDefinition(), "//rdaCommonStandard/text()/ancestor::field/@id");
Map<String, String> templateIdsToRDAProperties = combineListsIntoOrderedMap(datasetTemplateIdsWithRdaProperties, rdaProperties);
// Parsing dataset answers from json to map. Map: TemplateId -> datasetValue
JSONObject jObject = new JSONObject(dataset.getProperties());
Map<String, Object> templateIdsToValues = jObject.toMap();
// Map: rdaProperty -> datasetValue
Map<String, Object> rdaToValue = new HashMap<>();
for (String templateId : templateIdsToRDAProperties.keySet()) {
if (templateIdsToValues.containsKey(templateId))
rdaToValue.put(templateIdsToRDAProperties.get(templateId), templateIdsToValues.get(templateId));
}
DatasetRDAExportModel datasetRDAExportModel = mapper.convertValue(rdaToValue, DatasetRDAExportModel.class);
datasetRDAExportModel.title = dataset.getLabel();
datasetRDAExportModel.issued = dataset.getCreated();
datasetRDAExportModel.language = "en"; // mock data;
datasetRDAExportModel.dataset_id = new DatasetIdRDAExportModel(dataset.getId().toString(), "argos_internal");
return datasetRDAExportModel;
}
private List<String> xmlNodeListFromExpression(String xml, String expression) {
List<String> valuesList = new LinkedList<>();
Document document = XmlBuilder.fromXml(xml);
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
try {
XPathExpression expr = xpath.compile(expression);
NodeList nodeList = (NodeList) expr.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
valuesList.add(node.getNodeValue());
}
} catch (XPathExpressionException e) {
e.printStackTrace();
}
return valuesList;
}
private Map<String,String> combineListsIntoOrderedMap (List<String> keys, List<String> values) {
if (keys.size() != values.size())
throw new IllegalArgumentException ("Cannot combine lists with dissimilar sizes");
Map<String,String> map = new LinkedHashMap<>();
for (int i=0; i<keys.size(); i++) {
map.put(keys.get(i), values.get(i));
}
return map;
}
}

View File

@ -28,4 +28,19 @@ project.funding.funder_id.funder_id
project.funding.funder_id.funder_id_type
project.funding.grant_id
project.funding.grant_id.grant_id
project.funding.grant_id.grant_id_type
project.funding.grant_id.grant_id_type
dataset
dataset.title
dataset.type
dataset.personal_data
dataset.sensitive_data
dataset.dataset_id
dataset.dataset_id.dataset_id
dataset.dataset_id.dataset_id_type
dataset.metadata
dataset.metadata.language
dataset.metadata.metadata_id
dataset.metadata.metadata_id.metadata_id
dataset.metadata.metadata_id.metadata_id_type
dataset.security_and_privacy
dataset.security_and_privacy.title