argos/dmp-backend/web/src/main/java/eu/eudat/models/data/rda/DatasetRDAExportModel.java

145 lines
4.8 KiB
Java
Raw Normal View History

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 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.
private String language;
private String title;
private String sensitive_data;
private String data_quality_assurance;
private DatasetMetadataRDAExportModel metadata;
public DatasetIdRDAExportModel getDataset_id() {
return dataset_id;
}
public void setDataset_id(DatasetIdRDAExportModel dataset_id) {
this.dataset_id = dataset_id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Date getIssued() {
return issued;
}
public void setIssued(Date issued) {
this.issued = issued;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
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) {
// 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;
}
}