2019-10-11 15:39:29 +02:00
|
|
|
package eu.eudat.models.data.rda;
|
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2019-10-17 12:11:13 +02:00
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2019-12-11 11:48:13 +01:00
|
|
|
import com.jayway.jsonpath.JsonPath;
|
2019-10-11 15:39:29 +02:00
|
|
|
import eu.eudat.data.entities.Dataset;
|
2019-12-11 11:48:13 +01:00
|
|
|
import eu.eudat.logic.managers.DatasetManager;
|
2019-10-17 12:11:13 +02:00
|
|
|
import eu.eudat.logic.utilities.builders.XmlBuilder;
|
2020-02-26 12:36:42 +01:00
|
|
|
import eu.eudat.models.data.security.Principal;
|
2019-12-11 11:48:13 +01:00
|
|
|
import org.json.JSONArray;
|
2019-10-17 12:11:13 +02:00
|
|
|
import org.json.JSONObject;
|
2020-01-16 16:46:24 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2019-10-17 12:11:13 +02:00
|
|
|
import org.w3c.dom.Document;
|
|
|
|
import org.w3c.dom.Node;
|
|
|
|
import org.w3c.dom.NodeList;
|
2019-10-11 15:39:29 +02:00
|
|
|
|
2019-10-17 12:11:13 +02:00
|
|
|
import javax.xml.xpath.*;
|
2019-11-28 12:13:33 +01:00
|
|
|
import java.text.DateFormat;
|
2019-10-17 12:11:13 +02:00
|
|
|
import java.util.*;
|
2019-10-11 15:39:29 +02:00
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
import static java.util.stream.Collectors.groupingBy;
|
|
|
|
|
2019-10-11 15:39:29 +02:00
|
|
|
public class DatasetRDAExportModel {
|
2020-01-16 16:46:24 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(DatasetRDAExportModel.class);
|
2019-10-17 12:11:13 +02:00
|
|
|
|
2019-10-25 12:53:14 +02:00
|
|
|
private static final ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
2019-12-11 11:48:13 +01:00
|
|
|
private Map<String, String> multiplicityIdToFieldSetId = new HashMap<>();
|
2019-10-17 12:11:13 +02:00
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
private List<String> data_quality_assurance;
|
2019-11-28 12:13:33 +01:00
|
|
|
private IdRDAExportModel dataset_id;
|
2019-10-11 15:39:29 +02:00
|
|
|
private String description;
|
2019-11-28 12:13:33 +01:00
|
|
|
private List<DatasetDistributionRDAExportModel> distribution;
|
|
|
|
private String issued; // Created Date, could also use finalized one.
|
|
|
|
private List<String> keyword;
|
2019-10-11 15:39:29 +02:00
|
|
|
private String language;
|
2019-10-25 12:53:14 +02:00
|
|
|
private List<DatasetMetadataRDAExportModel> metadata;
|
2019-11-28 12:13:33 +01:00
|
|
|
private String personal_data; // Allowed Values: yes no unknown.
|
|
|
|
private String preservation_statement;
|
|
|
|
private List<DatasetSecurityAndPrivacyRDAExportModel> security_and_privacy;
|
|
|
|
private String sensitive_data; // Allowed Values: yes no unknown.
|
|
|
|
private List<DatasetTechnicalResourceRDAExportModel> technical_resource;
|
|
|
|
private String title;
|
|
|
|
private String type; // Type according to: http://vocabularies.coar-repositories.org/pubby/resource_type.html
|
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
public List<String> getData_quality_assurance() {
|
2019-11-28 12:13:33 +01:00
|
|
|
return data_quality_assurance;
|
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
public void setData_quality_assurance(List<String> data_quality_assurance) {
|
2019-11-28 12:13:33 +01:00
|
|
|
this.data_quality_assurance = data_quality_assurance;
|
|
|
|
}
|
2019-10-11 15:39:29 +02:00
|
|
|
|
2019-11-28 12:13:33 +01:00
|
|
|
public IdRDAExportModel getDataset_id() {
|
2019-10-11 15:39:29 +02:00
|
|
|
return dataset_id;
|
|
|
|
}
|
2019-11-28 12:13:33 +01:00
|
|
|
public void setDataset_id(IdRDAExportModel dataset_id) {
|
2019-10-11 15:39:29 +02:00
|
|
|
this.dataset_id = dataset_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getDescription() {
|
|
|
|
return description;
|
|
|
|
}
|
|
|
|
public void setDescription(String description) {
|
|
|
|
this.description = description;
|
|
|
|
}
|
|
|
|
|
2019-11-28 12:13:33 +01:00
|
|
|
public List<DatasetDistributionRDAExportModel> getDistribution() {
|
|
|
|
return distribution;
|
|
|
|
}
|
|
|
|
public void setDistribution(List<DatasetDistributionRDAExportModel> distribution) {
|
|
|
|
this.distribution = distribution;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getIssued() {
|
2019-10-11 15:39:29 +02:00
|
|
|
return issued;
|
|
|
|
}
|
2019-11-28 12:13:33 +01:00
|
|
|
public void setIssued(String issued) {
|
2019-10-11 15:39:29 +02:00
|
|
|
this.issued = issued;
|
|
|
|
}
|
|
|
|
|
2019-11-28 12:13:33 +01:00
|
|
|
public List<String> getKeyword() {
|
|
|
|
return keyword;
|
|
|
|
}
|
|
|
|
public void setKeyword(List<String> keyword) {
|
|
|
|
this.keyword = keyword;
|
|
|
|
}
|
|
|
|
|
2019-10-11 15:39:29 +02:00
|
|
|
public String getLanguage() {
|
|
|
|
return language;
|
|
|
|
}
|
|
|
|
public void setLanguage(String language) {
|
|
|
|
this.language = language;
|
|
|
|
}
|
|
|
|
|
2019-11-28 12:13:33 +01:00
|
|
|
public List<DatasetMetadataRDAExportModel> getMetadata() {
|
|
|
|
return metadata;
|
2019-10-11 15:39:29 +02:00
|
|
|
}
|
2019-11-28 12:13:33 +01:00
|
|
|
public void setMetadata(List<DatasetMetadataRDAExportModel> metadata) {
|
|
|
|
this.metadata = metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPersonal_data() {
|
|
|
|
return personal_data;
|
|
|
|
}
|
|
|
|
public void setPersonal_data(String personal_data) {
|
|
|
|
this.personal_data = personal_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getPreservation_statement() {
|
|
|
|
return preservation_statement;
|
|
|
|
}
|
|
|
|
public void setPreservation_statement(String preservation_statement) {
|
|
|
|
this.preservation_statement = preservation_statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<DatasetSecurityAndPrivacyRDAExportModel> getSecurity_and_privacy() {
|
|
|
|
return security_and_privacy;
|
|
|
|
}
|
|
|
|
public void setSecurity_and_privacy(List<DatasetSecurityAndPrivacyRDAExportModel> security_and_privacy) {
|
|
|
|
this.security_and_privacy = security_and_privacy;
|
2019-10-11 15:39:29 +02:00
|
|
|
}
|
|
|
|
|
2019-10-17 12:11:13 +02:00
|
|
|
public String getSensitive_data() {
|
|
|
|
return sensitive_data;
|
|
|
|
}
|
|
|
|
public void setSensitive_data(String sensitive_data) {
|
|
|
|
this.sensitive_data = sensitive_data;
|
|
|
|
}
|
|
|
|
|
2019-11-28 12:13:33 +01:00
|
|
|
public List<DatasetTechnicalResourceRDAExportModel> getTechnical_resource() {
|
|
|
|
return technical_resource;
|
2019-10-17 12:11:13 +02:00
|
|
|
}
|
2019-11-28 12:13:33 +01:00
|
|
|
public void setTechnical_resource(List<DatasetTechnicalResourceRDAExportModel> technical_resource) {
|
|
|
|
this.technical_resource = technical_resource;
|
2019-10-17 12:11:13 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 12:13:33 +01:00
|
|
|
public String getTitle() {
|
|
|
|
return title;
|
2019-10-17 12:11:13 +02:00
|
|
|
}
|
2019-11-28 12:13:33 +01:00
|
|
|
public void setTitle(String title) {
|
|
|
|
this.title = title;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getType() {
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
public void setType(String type) {
|
|
|
|
this.type = type;
|
2019-10-17 12:11:13 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:39:29 +02:00
|
|
|
|
2020-02-26 12:36:42 +01:00
|
|
|
public DatasetRDAExportModel fromDataModel(Dataset dataset, DatasetManager datasetManager, Principal principal) {
|
2019-12-11 11:48:13 +01:00
|
|
|
// Map of template Ids to rda values.
|
|
|
|
JSONObject jObject = new JSONObject(dataset.getProperties());
|
|
|
|
Map<String, Object> templateIdsToValues = jObject.toMap();
|
2019-11-28 12:13:33 +01:00
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
/*--------- Building dataset rda export model ---------*/
|
|
|
|
DatasetRDAExportModel datasetRDAExportModel = new DatasetRDAExportModel();
|
2019-11-28 12:13:33 +01:00
|
|
|
datasetRDAExportModel.setDataset_id(new IdRDAExportModel(dataset.getId().toString(), "other"));
|
2019-12-11 11:48:13 +01:00
|
|
|
if (dataset.getDescription() != null) datasetRDAExportModel.setDescription(dataset.getDescription().replace("\n", " "));
|
2019-11-28 12:13:33 +01:00
|
|
|
datasetRDAExportModel.setIssued(DateFormat.getDateInstance(DateFormat.SHORT).format(dataset.getCreated()));
|
2019-12-11 11:48:13 +01:00
|
|
|
datasetRDAExportModel.setLanguage("en"); // mock data
|
2019-11-28 12:13:33 +01:00
|
|
|
datasetRDAExportModel.setTitle(dataset.getLabel());
|
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
// Transform the answered dataset description to json so we can parse it and fill the rda model.
|
|
|
|
JSONObject datasetDescriptionJson = null;
|
2019-11-28 12:13:33 +01:00
|
|
|
try {
|
2020-02-26 12:36:42 +01:00
|
|
|
String jsonResult = mapper.writeValueAsString(datasetManager.getSingle(dataset.getId().toString(), principal).getDatasetProfileDefinition());
|
2019-12-11 11:48:13 +01:00
|
|
|
datasetDescriptionJson = new JSONObject(jsonResult);
|
|
|
|
} catch (JsonProcessingException e) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2019-11-28 12:13:33 +01:00
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
setMultiplicityIdToFieldSetId(datasetDescriptionJson);
|
|
|
|
|
|
|
|
/*--------- Building personal data. ---------*/
|
|
|
|
String personalData = buildSingleProperties("dataset.personal_data", datasetDescriptionJson, templateIdsToValues);
|
|
|
|
if (personalData != null) {
|
|
|
|
datasetRDAExportModel.setPersonal_data(personalData);
|
|
|
|
} else {
|
|
|
|
datasetRDAExportModel.setPersonal_data("unknown");
|
|
|
|
}
|
2019-11-28 12:13:33 +01:00
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
/*--------- Building preservation statement. ---------*/
|
|
|
|
datasetRDAExportModel.setPreservation_statement(buildSingleProperties("dataset.preservation_statement", datasetDescriptionJson, templateIdsToValues));
|
|
|
|
|
|
|
|
/*--------- Building sensitive data. ---------*/
|
|
|
|
String sensitiveData = buildSingleProperties("dataset.sensitive_data", datasetDescriptionJson, templateIdsToValues);
|
|
|
|
if (personalData != null) {
|
|
|
|
datasetRDAExportModel.setSensitive_data(sensitiveData);
|
|
|
|
} else {
|
|
|
|
datasetRDAExportModel.setSensitive_data("unknown");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------- Building type. ---------*/
|
|
|
|
datasetRDAExportModel.setType(buildSingleProperties("dataset.type", datasetDescriptionJson, templateIdsToValues));
|
|
|
|
|
|
|
|
/*--------- Building data_quality_assurance. ---------*/
|
|
|
|
datasetRDAExportModel.setData_quality_assurance(buildDataQualityAssurance(datasetDescriptionJson, templateIdsToValues, dataset.getProfile().getDefinition()));
|
|
|
|
|
2020-01-14 13:18:14 +01:00
|
|
|
/*--------- Building distribution. ---------*/
|
|
|
|
datasetRDAExportModel.setDistribution(buildDistribution(datasetDescriptionJson, templateIdsToValues, dataset.getProfile().getDefinition()));
|
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
/*--------- Building keywords. ---------*/
|
|
|
|
datasetRDAExportModel.setKeyword(buildKeywords(datasetDescriptionJson, templateIdsToValues, dataset.getProfile().getDefinition()));
|
|
|
|
|
|
|
|
/*--------- Building metadata items. ---------*/
|
|
|
|
datasetRDAExportModel.setMetadata(buildMetadata(datasetDescriptionJson, templateIdsToValues, dataset.getProfile().getDefinition()));
|
|
|
|
|
|
|
|
/*--------- Building security and privacy items. ---------*/
|
|
|
|
datasetRDAExportModel.setSecurity_and_privacy(buildSecurityAndPrivacy(datasetDescriptionJson, templateIdsToValues, dataset.getProfile().getDefinition()));
|
|
|
|
|
|
|
|
/*--------- Building technical_resource. ---------*/
|
|
|
|
datasetRDAExportModel.setTechnical_resource(buildTechnicalResource(datasetDescriptionJson, templateIdsToValues, dataset.getProfile().getDefinition()));
|
|
|
|
|
|
|
|
return datasetRDAExportModel;
|
2019-11-28 12:13:33 +01:00
|
|
|
}
|
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
private String buildSingleProperties(String rdaKey, JSONObject datasetDescriptionJson, Map<String, Object> templateIdsToValues) {
|
|
|
|
String expression = "$..fields[*][?(@.rdaProperty == \"" + rdaKey + "\" )].id";
|
|
|
|
List<String> list = jsonValueListFromExpression(datasetDescriptionJson, expression);
|
|
|
|
if (!list.isEmpty()) {
|
|
|
|
return templateIdsToValues.get(list.get(0)).toString();
|
|
|
|
} else {
|
|
|
|
return null;
|
2019-11-28 12:13:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
private List<String> buildDataQualityAssurance(JSONObject datasetDescriptionJson, Map<String, Object> templateIdsToValues, String datasetProfileDefinition) {
|
|
|
|
List<RdaField> dataQualityFields = getRDAFieldsFromJson(datasetDescriptionJson, new String[]{"dataset.data_quality_assurance"}, datasetProfileDefinition);
|
|
|
|
for (RdaField rdaField : dataQualityFields) {
|
|
|
|
rdaField.setRdaValue(templateIdsToValues.get(rdaField.getFieldId()).toString());
|
|
|
|
}
|
|
|
|
List<String> dataQualityAssuranceList = new LinkedList<>();
|
|
|
|
for (RdaField rdaField : dataQualityFields) {
|
|
|
|
dataQualityAssuranceList.add(rdaField.getRdaValue());
|
2019-10-17 12:11:13 +02:00
|
|
|
}
|
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
return dataQualityAssuranceList;
|
|
|
|
}
|
2019-10-17 12:11:13 +02:00
|
|
|
|
2020-01-14 13:18:14 +01:00
|
|
|
private List<DatasetDistributionRDAExportModel> buildDistribution(JSONObject datasetDescriptionJson, Map<String, Object> templateIdsToValues, String datasetProfileDefinition) {
|
|
|
|
DatasetDistributionRDAExportModel distributionModel = new DatasetDistributionRDAExportModel();
|
|
|
|
distributionModel.setAccess_url(buildSingleProperties("dataset.distribution.access_url", datasetDescriptionJson, templateIdsToValues));
|
|
|
|
distributionModel.setAvailable_till(buildSingleProperties("dataset.distribution.available_till", datasetDescriptionJson, templateIdsToValues));
|
|
|
|
distributionModel.setByte_size(buildSingleProperties("dataset.distribution.byte_size", datasetDescriptionJson, templateIdsToValues));
|
|
|
|
distributionModel.setData_access(buildSingleProperties("dataset.distribution.data_access", datasetDescriptionJson, templateIdsToValues));
|
|
|
|
distributionModel.setDescription(buildSingleProperties("dataset.distribution.description", datasetDescriptionJson, templateIdsToValues));
|
|
|
|
distributionModel.setDownload_url(buildSingleProperties("dataset.distribution.download_url", datasetDescriptionJson, templateIdsToValues));
|
|
|
|
distributionModel.setTitle(buildSingleProperties("dataset.distribution.title", datasetDescriptionJson, templateIdsToValues));
|
|
|
|
|
|
|
|
/*--------- Building format. ---------*/
|
|
|
|
|
|
|
|
|
|
|
|
// We currently support the return of only one distribution.
|
|
|
|
List<DatasetDistributionRDAExportModel> distributionList = new LinkedList<>();
|
|
|
|
if (distributionModel.isValid()) {
|
|
|
|
distributionList.add(distributionModel);
|
|
|
|
} else {
|
|
|
|
DatasetDistributionRDAExportModel model = new DatasetDistributionRDAExportModel();
|
|
|
|
model.setDescription("Distribution data was not valid");
|
|
|
|
distributionList.add(model);
|
|
|
|
}
|
|
|
|
|
|
|
|
return distributionList;
|
|
|
|
}
|
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
private List<String> buildKeywords(JSONObject datasetDescriptionJson, Map<String, Object> templateIdsToValues, String datasetProfileDefinition) {
|
|
|
|
List<RdaField> keywordFields = getRDAFieldsFromJson(datasetDescriptionJson, new String[]{"dataset.keyword"}, datasetProfileDefinition);
|
|
|
|
for (RdaField rdaField : keywordFields) {
|
|
|
|
rdaField.setRdaValue(templateIdsToValues.get(rdaField.getFieldId()).toString());
|
2019-10-17 12:11:13 +02:00
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
List<String> keywordsList = new LinkedList<>();
|
|
|
|
for (RdaField rdaField : keywordFields) {
|
|
|
|
keywordsList.add(rdaField.getRdaValue());
|
|
|
|
}
|
|
|
|
|
|
|
|
return keywordsList;
|
2019-11-28 12:13:33 +01:00
|
|
|
}
|
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
private List<DatasetMetadataRDAExportModel> buildMetadata(JSONObject datasetDescriptionJson, Map<String, Object> templateIdsToValues, String datasetProfileDefinition) {
|
|
|
|
List<RdaField> metadataFields = getRDAFieldsFromJson(datasetDescriptionJson,
|
|
|
|
new String[]{"dataset.metadata.metadata_standard_id.type", "dataset.metadata.metadata_standard_id.identifier", "dataset.metadata.description", "dataset.metadata.language", "dataset.metadata.metadata_standard_id"},
|
|
|
|
datasetProfileDefinition);
|
|
|
|
|
|
|
|
// Adding rdaValue and FieldSetIds on metadataFields.
|
|
|
|
for (RdaField rdaField : metadataFields) {
|
|
|
|
rdaField.setRdaValue(templateIdsToValues.get(rdaField.getFieldId()).toString());
|
2019-11-28 12:13:33 +01:00
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
// Group metadataFields based on their field set id.
|
|
|
|
Map<String, List<RdaField>> groupedMetadataFields = metadataFields.stream().collect(groupingBy(RdaField::getFieldSetId));
|
|
|
|
|
|
|
|
// Creating the metadata.
|
|
|
|
List<DatasetMetadataRDAExportModel> metadataRDAExportModelList = new LinkedList<>();
|
|
|
|
for (String fieldSetId : groupedMetadataFields.keySet()) {
|
|
|
|
DatasetMetadataRDAExportModel metadataRda = new DatasetMetadataRDAExportModel();
|
|
|
|
for (RdaField rdaField : groupedMetadataFields.get(fieldSetId)) {
|
|
|
|
if (rdaField.getRdaProperty().equals("dataset.metadata.metadata_standard_id.identifier")) {
|
|
|
|
if (metadataRda.getMetadata_standard_id() != null) {
|
|
|
|
metadataRda.getMetadata_standard_id().setIdentifier(rdaField.getRdaValue());
|
|
|
|
} else {
|
|
|
|
metadataRda.setMetadata_standard_id(new IdRDAExportModel(rdaField.getRdaValue(), "other"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rdaField.getRdaProperty().equals("dataset.metadata.metadata_standard_id.type")) {
|
|
|
|
if (metadataRda.getMetadata_standard_id() != null) {
|
|
|
|
metadataRda.getMetadata_standard_id().setType(rdaField.getRdaValue());
|
|
|
|
} else {
|
|
|
|
metadataRda.setMetadata_standard_id(new IdRDAExportModel("", rdaField.getRdaValue()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rdaField.getRdaProperty().equals("dataset.metadata.description")) {
|
|
|
|
metadataRda.setDescription(rdaField.getRdaValue());
|
|
|
|
}
|
|
|
|
if (rdaField.getRdaProperty().equals("dataset.metadata.language")) {
|
|
|
|
metadataRda.setLanguage(rdaField.getRdaValue());
|
|
|
|
}
|
2020-03-19 17:22:47 +01:00
|
|
|
if (rdaField.getRdaProperty().equals("dataset.metadata.metadata_standard_id") && !rdaField.getRdaValue().isEmpty()) {
|
2019-12-11 11:48:13 +01:00
|
|
|
JSONArray jsonArray = new JSONArray(rdaField.getRdaValue());
|
|
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
|
|
JSONObject jsonObject = jsonArray.getJSONObject(i);
|
|
|
|
Map<String, Object> jsonObjectMap = jsonObject.toMap();
|
|
|
|
DatasetMetadataRDAExportModel metadataRda1 = new DatasetMetadataRDAExportModel();
|
2020-03-20 17:20:22 +01:00
|
|
|
// metadataRda1.setMetadata_standard_id(new IdRDAExportModel(jsonObjectMap.get("label").toString(), jsonObjectMap.get("source").toString()));
|
|
|
|
metadataRda1.setMetadata_standard_id(new IdRDAExportModel(jsonObjectMap.get("uri").toString(), "url"));
|
2019-12-11 11:48:13 +01:00
|
|
|
metadataRDAExportModelList.add(metadataRda1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (metadataRda.isValid()) {
|
|
|
|
metadataRDAExportModelList.add(metadataRda);
|
2019-11-28 12:13:33 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
|
|
|
|
return new LinkedList<>(metadataRDAExportModelList);
|
2019-11-28 12:13:33 +01:00
|
|
|
}
|
2019-10-17 12:11:13 +02:00
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
private List<DatasetSecurityAndPrivacyRDAExportModel> buildSecurityAndPrivacy(JSONObject datasetDescriptionJson, Map<String, Object> templateIdsToValues, String datasetProfileDefinition) {
|
|
|
|
List<RdaField> secAndPrFields = getRDAFieldsFromJson(
|
|
|
|
datasetDescriptionJson,
|
|
|
|
new String[]{"dataset.security_and_privacy.description", "dataset.security_and_privacy.title", "dataset.security_and_privacy"},
|
|
|
|
datasetProfileDefinition);
|
|
|
|
for (RdaField rdaField : secAndPrFields) {
|
|
|
|
rdaField.setRdaValue(templateIdsToValues.get(rdaField.getFieldId()).toString());
|
2019-10-25 12:53:14 +02:00
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
Map<String, List<RdaField>> groupedSecurityAndPrivacyFields = secAndPrFields.stream().collect(groupingBy(RdaField::getFieldSetId));
|
|
|
|
|
|
|
|
List<DatasetSecurityAndPrivacyRDAExportModel> securityAndPrivacyRDAExportModelList = new LinkedList<>();
|
|
|
|
for (String fieldSetId : groupedSecurityAndPrivacyFields.keySet()) {
|
|
|
|
DatasetSecurityAndPrivacyRDAExportModel securityAndPrivacyModel = new DatasetSecurityAndPrivacyRDAExportModel();
|
|
|
|
for (RdaField rdaField : groupedSecurityAndPrivacyFields.get(fieldSetId)) {
|
|
|
|
if (rdaField.getRdaProperty().equals("dataset.security_and_privacy.description")) {
|
|
|
|
securityAndPrivacyModel.setDescription(rdaField.getRdaValue());
|
|
|
|
}
|
|
|
|
if (rdaField.getRdaProperty().equals("dataset.security_and_privacy.title")) {
|
|
|
|
securityAndPrivacyModel.setTitle(rdaField.getRdaValue());
|
|
|
|
}
|
|
|
|
if (rdaField.getRdaProperty().equals("dataset.security_and_privacy")) {
|
|
|
|
JSONArray jsonArray = new JSONArray(rdaField.getRdaValue());
|
|
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
|
|
JSONObject jsonObject = jsonArray.getJSONObject(i);
|
|
|
|
Map<String, Object> jsonObjectMap = jsonObject.toMap();
|
|
|
|
DatasetSecurityAndPrivacyRDAExportModel secAndPrivacy = new DatasetSecurityAndPrivacyRDAExportModel(jsonObjectMap.get("label").toString(), jsonObjectMap.get("source").toString());
|
|
|
|
securityAndPrivacyRDAExportModelList.add(secAndPrivacy);
|
|
|
|
}
|
|
|
|
}
|
2019-10-25 12:53:14 +02:00
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
securityAndPrivacyRDAExportModelList.add(securityAndPrivacyModel);
|
2019-10-25 12:53:14 +02:00
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
|
|
|
|
return securityAndPrivacyRDAExportModelList;
|
2019-10-17 12:11:13 +02:00
|
|
|
}
|
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
private List<DatasetTechnicalResourceRDAExportModel> buildTechnicalResource(JSONObject datasetDescriptionJson, Map<String, Object> templateIdsToValues, String datasetProfileDefinition) {
|
|
|
|
List<RdaField> dataQualityFields = getRDAFieldsFromJson(datasetDescriptionJson,
|
|
|
|
new String[]{"dataset.technical_resource.technical_resource", "dataset.technical_resource.technical_resource.description", "dataset.technical_resource.technical_resource.name"},
|
|
|
|
datasetProfileDefinition);
|
|
|
|
for (RdaField rdaField : dataQualityFields) {
|
|
|
|
rdaField.setRdaValue(templateIdsToValues.get(rdaField.getFieldId()).toString());
|
2019-10-17 12:11:13 +02:00
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
List<DatasetTechnicalResourceRDAExportModel> technicalResourceList = new LinkedList<>();
|
|
|
|
Map<String, List<RdaField>> groupedDataQualityFields = dataQualityFields.stream().collect(groupingBy(RdaField::getFieldSetId));
|
|
|
|
for (String fieldSetId : groupedDataQualityFields.keySet()) {
|
|
|
|
DatasetTechnicalResourceRDAExportModel technicalResourceModel = new DatasetTechnicalResourceRDAExportModel();
|
|
|
|
for (RdaField rdaField : groupedDataQualityFields.get(fieldSetId)) {
|
|
|
|
if (rdaField.getRdaProperty().equals("dataset.technical_resource.technical_resource.description")) {
|
|
|
|
technicalResourceModel.setDescription(rdaField.getRdaValue());
|
|
|
|
}
|
|
|
|
if (rdaField.getRdaProperty().equals("dataset.technical_resource.technical_resource.name")) {
|
|
|
|
technicalResourceModel.setName(rdaField.getRdaValue());
|
|
|
|
}
|
|
|
|
if (rdaField.getRdaProperty().equals("dataset.security_and_privacy")) {
|
|
|
|
JSONArray jsonArray = new JSONArray(rdaField.getRdaValue());
|
|
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
|
|
JSONObject jsonObject = jsonArray.getJSONObject(i);
|
|
|
|
Map<String, Object> jsonObjectMap = jsonObject.toMap();
|
|
|
|
DatasetTechnicalResourceRDAExportModel technicalResource = new DatasetTechnicalResourceRDAExportModel(jsonObjectMap.get("label").toString(), jsonObjectMap.get("label").toString());
|
|
|
|
technicalResourceList.add(technicalResource);
|
|
|
|
}
|
|
|
|
}
|
2019-11-28 12:13:33 +01:00
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
technicalResourceList.add(technicalResourceModel);
|
2019-11-28 12:13:33 +01:00
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
|
|
|
|
return technicalResourceList;
|
2019-10-17 12:11:13 +02:00
|
|
|
}
|
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
private void setMultiplicityIdToFieldSetId(JSONObject json) {
|
|
|
|
String multiplicityItemsFieldSetIdExp = "$..multiplicityItems[*].id";
|
|
|
|
List<String> multiplicityItemsFieldSetIdList = jsonValueListFromExpression(json, multiplicityItemsFieldSetIdExp);
|
|
|
|
for (String fieldSetId : multiplicityItemsFieldSetIdList) {
|
|
|
|
String fieldsFromFieldSetIdExp = "$..multiplicityItems[*][?(@.id == \""+ fieldSetId +"\")].fields[*].id";
|
|
|
|
List<String> fieldsIdList = jsonValueListFromExpression(json, fieldsFromFieldSetIdExp);
|
|
|
|
for (String fieldId : fieldsIdList) {
|
|
|
|
this.multiplicityIdToFieldSetId.put(fieldId, fieldSetId);
|
2019-11-28 12:13:33 +01:00
|
|
|
}
|
2019-10-17 12:11:13 +02:00
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private List<RdaField> getRDAFieldsFromJson(JSONObject json, String[] rdaKey, String datasetProfileDefinition) {
|
|
|
|
List<RdaField> rdaFields = new LinkedList<>();
|
|
|
|
for (String key : rdaKey) {
|
|
|
|
String fieldIdExpression = "$..fields[*][?(@.rdaProperty == \"" + key + "\" )].id";
|
|
|
|
List<String> listFromExpression = jsonValueListFromExpression(json, fieldIdExpression);
|
|
|
|
for (String fieldId : listFromExpression) {
|
|
|
|
RdaField rdaField = new RdaField();
|
|
|
|
rdaField.setRdaProperty(key);
|
|
|
|
rdaField.setFieldId(fieldId);
|
|
|
|
if (fieldId.startsWith("multiple_")) {
|
|
|
|
rdaField.setFieldSetId(this.multiplicityIdToFieldSetId.get(fieldId));
|
|
|
|
} else {
|
|
|
|
rdaField.setFieldSetId(getFieldSetIdForFieldFromXML(datasetProfileDefinition, fieldId));
|
|
|
|
}
|
|
|
|
rdaFields.add(rdaField);
|
2019-11-28 12:13:33 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-11 11:48:13 +01:00
|
|
|
return rdaFields;
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<String> jsonValueListFromExpression(JSONObject json, String expression) {
|
|
|
|
net.minidev.json.JSONArray jsonArray = JsonPath.parse(json.toString()).read(expression);
|
|
|
|
List<String> valueList = new LinkedList<>();
|
|
|
|
for (Object o : jsonArray) {
|
|
|
|
valueList.add(o.toString());
|
|
|
|
}
|
|
|
|
return valueList;
|
2019-10-11 15:39:29 +02:00
|
|
|
}
|
2019-11-28 12:13:33 +01:00
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
private String getFieldSetIdForFieldFromXML(String datasetProfileDefinition, String fieldId) {
|
|
|
|
String fieldSetIdExpression = "//field[@id ='" + fieldId + "']/ancestor::fieldSet/@id";
|
|
|
|
List<String> listFromExpression = xmlValueListFromExpression(datasetProfileDefinition, fieldSetIdExpression);
|
|
|
|
if (listFromExpression.size() == 1) return listFromExpression.get(0);
|
|
|
|
return null;
|
|
|
|
}
|
2019-11-28 12:13:33 +01:00
|
|
|
|
2019-12-11 11:48:13 +01:00
|
|
|
private List<String> xmlValueListFromExpression(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) {
|
2020-01-16 16:46:24 +01:00
|
|
|
logger.error(e.getMessage(), e);
|
2019-12-11 11:48:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return valuesList;
|
|
|
|
}
|
2019-10-11 15:39:29 +02:00
|
|
|
}
|