From 19ccef69911c69dcf8fae72d5ab88b3044e9d5dd Mon Sep 17 00:00:00 2001 From: George Kalampokis Date: Mon, 23 Mar 2020 18:09:31 +0200 Subject: [PATCH] Improved object coupling for various objects on the dataset form --- .../utilities/helpers/MyStringUtils.java | 21 ++++ .../logic/utilities/json/JsonSearcher.java | 1 + .../user/components/datasetprofile/Field.java | 6 +- .../components/datasetprofile/FieldSet.java | 9 +- .../models/rda/mapper/DatasetRDAMapper.java | 27 ++-- .../rda/mapper/DistributionRDAMapper.java | 107 ++++++++++++++-- .../models/rda/mapper/HostRDAMapper.java | 98 +++++++++++---- .../models/rda/mapper/LicenseRDAMapper.java | 6 +- .../models/rda/mapper/MetadataRDAMapper.java | 119 +++++++++++++++--- .../mapper/SecurityAndPrivacyRDAMapper.java | 57 ++++++++- .../mapper/TechnicalResourceRDAMapper.java | 58 ++++++++- .../dataset-description-form.model.ts | 2 +- 12 files changed, 424 insertions(+), 87 deletions(-) create mode 100644 dmp-backend/web/src/main/java/eu/eudat/logic/utilities/helpers/MyStringUtils.java diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/helpers/MyStringUtils.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/helpers/MyStringUtils.java new file mode 100644 index 000000000..1a071e04c --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/helpers/MyStringUtils.java @@ -0,0 +1,21 @@ +package eu.eudat.logic.utilities.helpers; + +public class MyStringUtils { + + public static int getFirstDifference(String s1, String s2) { + char[] s1ar = s1.toCharArray(); + char[] s2ar = s2.toCharArray(); + + for(int i = 0; i < s1ar.length; i++) { + if (s2ar.length > i) { + if (s1ar[i] != s2ar[i]) { + return i; + } + } else { + return i; + } + } + + return -1; + } +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/json/JsonSearcher.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/json/JsonSearcher.java index ca2372b91..979053830 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/json/JsonSearcher.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/json/JsonSearcher.java @@ -1,6 +1,7 @@ package eu.eudat.logic.utilities.json; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; import java.util.ArrayList; import java.util.Iterator; diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/data/user/components/datasetprofile/Field.java b/dmp-backend/web/src/main/java/eu/eudat/models/data/user/components/datasetprofile/Field.java index 4950261c4..1c735e1a9 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/models/data/user/components/datasetprofile/Field.java +++ b/dmp-backend/web/src/main/java/eu/eudat/models/data/user/components/datasetprofile/Field.java @@ -149,7 +149,7 @@ public class Field implements Comparable, PropertiesModelBuilder, ViewStyleDefin this.rdaProperty = rdaProperty; } - Field cloneForMultiplicity(String key, Map properties) { + Field cloneForMultiplicity(String key, Map properties, int index) { Field newField = new Field(); newField.id = key; newField.ordinal = this.ordinal; @@ -161,6 +161,7 @@ public class Field implements Comparable, PropertiesModelBuilder, ViewStyleDefin newField.data = this.data; newField.validations = this.validations; newField.rdaProperty = this.rdaProperty; + newField.numbering = "mult" + index + "_" + this.numbering; return newField; } @@ -204,8 +205,9 @@ public class Field implements Comparable, PropertiesModelBuilder, ViewStyleDefin } this.multiplicityItems = new LinkedList<>(); List compositeKeys = properties.keySet().stream().filter(keys -> keys.startsWith("multiple_" + this.getId())).collect(Collectors.toList()); + int index = 1; for (String key : compositeKeys) { - this.multiplicityItems.add(this.cloneForMultiplicity(key, properties)); + this.multiplicityItems.add(this.cloneForMultiplicity(key, properties, index)); } } diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/data/user/components/datasetprofile/FieldSet.java b/dmp-backend/web/src/main/java/eu/eudat/models/data/user/components/datasetprofile/FieldSet.java index 51663ddaf..277534d16 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/models/data/user/components/datasetprofile/FieldSet.java +++ b/dmp-backend/web/src/main/java/eu/eudat/models/data/user/components/datasetprofile/FieldSet.java @@ -163,16 +163,18 @@ public class FieldSet implements Comparable, PropertiesModelBuilder, ViewStyleDe }); List compositeKeysFather = properties.keySet().stream().filter(keys -> keys.startsWith("multiple_" + this.getId())).collect(Collectors.toList()); List Ids=new ArrayList<>(); + int index = 1; for (String composite : compositeKeysFather) { String[] split = composite.split("_"); if (!Ids.contains(split[2])) { Ids.add(split[2]); - this.multiplicityItems.add(this.CloneForMultiplicity2(properties.keySet().stream().filter(keys -> keys.startsWith("multiple_" + this.getId() + "_" + split[2])).collect(Collectors.toList()), properties,split)); + this.multiplicityItems.add(this.CloneForMultiplicity2(properties.keySet().stream().filter(keys -> keys.startsWith("multiple_" + this.getId() + "_" + split[2])).collect(Collectors.toList()), properties,split, index)); + index++; } } } - private FieldSet CloneForMultiplicity2(List key, Map properties,String[] ids){ + private FieldSet CloneForMultiplicity2(List key, Map properties,String[] ids, int index){ FieldSet newFieldSet = new FieldSet(); newFieldSet.id = ids[0]+"_"+ids[1]+"_"+ids[2]; newFieldSet.description = this.description; @@ -181,8 +183,9 @@ public class FieldSet implements Comparable, PropertiesModelBuilder, ViewStyleDe newFieldSet.title = this.title; newFieldSet.ordinal = this.ordinal; newFieldSet.fields = new LinkedList(); + for (Field field: this.fields) { - newFieldSet.fields.add(field.cloneForMultiplicity(newFieldSet.id + "_" + field.getId(), properties)); + newFieldSet.fields.add(field.cloneForMultiplicity(newFieldSet.id + "_" + field.getId(), properties, index)); } return newFieldSet; } diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/DatasetRDAMapper.java b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/DatasetRDAMapper.java index 103ba72c6..5eb9cc685 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/DatasetRDAMapper.java +++ b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/DatasetRDAMapper.java @@ -1,21 +1,17 @@ package eu.eudat.models.rda.mapper; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import eu.eudat.logic.managers.DatasetManager; import eu.eudat.logic.utilities.json.JsonSearcher; import eu.eudat.models.data.datasetwizard.DatasetWizardModel; import eu.eudat.models.rda.Dataset; -import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.transaction.Transactional; -import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.stream.Collectors; @@ -41,52 +37,51 @@ public class DatasetRDAMapper { DatasetWizardModel datasetWizardModel = new DatasetWizardModel().fromDataModel(dataset); datasetWizardModel.setDatasetProfileDefinition(datasetManager.getPagedProfile(datasetWizardModel, dataset)); ObjectMapper mapper = new ObjectMapper(); - JsonNode properties = mapper.readTree(dataset.getProperties()); String datasetDescriptionJson = mapper.writeValueAsString(datasetWizardModel.getDatasetProfileDefinition()); JsonNode datasetDescriptionObj = mapper.readTree(datasetDescriptionJson); List typeNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.type"); if (!typeNodes.isEmpty()) { - rda.setType(properties.get(typeNodes.get(0).get("id").asText()).asText()); + rda.setType(typeNodes.get(0).get("value").asText()); } List languageNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.language"); if (!languageNodes.isEmpty()) { - rda.setLanguage(Dataset.Language.fromValue(properties.get(languageNodes.get(0).get("id").asText()).asText())); + rda.setLanguage(Dataset.Language.fromValue(languageNodes.get(0).get("value").asText())); } List metadataNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.metadata"); if (!metadataNodes.isEmpty()) { - rda.setMetadata(metadataNodes.stream().map(node -> MetadataRDAMapper.toRDA(node, properties)).collect(Collectors.toList())); + rda.setMetadata(MetadataRDAMapper.toRDAList(metadataNodes)); } List qaNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.data_quality_assurance"); if (!qaNodes.isEmpty()) { - rda.setDataQualityAssurance(qaNodes.stream().map(qaNode -> properties.get(qaNode.get("id").asText()).asText()).collect(Collectors.toList())); + rda.setDataQualityAssurance(qaNodes.stream().map(qaNode -> qaNode.get("value").asText()).collect(Collectors.toList())); } List preservationNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.preservation_statement"); if (!preservationNodes.isEmpty()) { - rda.setDataQualityAssurance(preservationNodes.stream().map(preservationNode -> properties.get(preservationNode.get("id").asText()).asText()).collect(Collectors.toList())); + rda.setDataQualityAssurance(preservationNodes.stream().map(preservationNode -> preservationNode.get("value").asText()).collect(Collectors.toList())); } List distributionNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.distribution"); if (!distributionNodes.isEmpty()) { - rda.setDistribution(distributionNodes.stream().map(distributionNode -> DistributionRDAMapper.toRDA(distributionNode, properties)).collect(Collectors.toList())); + rda.setDistribution(DistributionRDAMapper.toRDAList(distributionNodes)); } List keywordNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.keyword"); if (!keywordNodes.isEmpty()) { - rda.setKeyword(keywordNodes.stream().map(keywordNode -> properties.get(keywordNode.get("id").asText()).asText()).collect(Collectors.toList())); + rda.setKeyword(keywordNodes.stream().map(keywordNode -> keywordNode.get("value").asText()).collect(Collectors.toList())); } List personalDataNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.personal_data"); if (!personalDataNodes.isEmpty()) { - rda.setPersonalData(personalDataNodes.stream().map(personalDataNode -> Dataset.PersonalData.fromValue(properties.get(personalDataNode.get("id").asText()).asText())).findFirst().get()); + rda.setPersonalData(personalDataNodes.stream().map(personalDataNode -> Dataset.PersonalData.fromValue(personalDataNode.get("value").asText())).findFirst().get()); } List securityAndPrivacyNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.security_and_privacy"); if (!securityAndPrivacyNodes.isEmpty()) { - rda.setSecurityAndPrivacy(securityAndPrivacyNodes.stream().map(securityAndPrivacyNode -> SecurityAndPrivacyRDAMapper.toRDA(securityAndPrivacyNode, properties)).collect(Collectors.toList())); + rda.setSecurityAndPrivacy(SecurityAndPrivacyRDAMapper.toRDAList(securityAndPrivacyNodes)); } List sensitiveDataNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.sensitive_data"); if (!sensitiveDataNodes.isEmpty()) { - rda.setSensitiveData(securityAndPrivacyNodes.stream().map(sensitiveDataNode -> Dataset.SensitiveData.fromValue(properties.get(sensitiveDataNode.get("id").asText()).asText())).findFirst().get()); + rda.setSensitiveData(securityAndPrivacyNodes.stream().map(sensitiveDataNode -> Dataset.SensitiveData.fromValue(sensitiveDataNode.get("value").asText())).findFirst().get()); } List technicalResourceNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.technical_resource"); if (!technicalResourceNodes.isEmpty()) { - rda.setTechnicalResource(technicalResourceNodes.stream().map(technicalResourceNode -> TechnicalResourceRDAMapper.toRDA(technicalResourceNode, properties)).collect(Collectors.toList())); + rda.setTechnicalResource(TechnicalResourceRDAMapper.toRDAList(technicalResourceNodes)); } diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/DistributionRDAMapper.java b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/DistributionRDAMapper.java index 513affd98..98cb6960b 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/DistributionRDAMapper.java +++ b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/DistributionRDAMapper.java @@ -1,39 +1,120 @@ package eu.eudat.models.rda.mapper; import com.fasterxml.jackson.databind.JsonNode; +import eu.eudat.logic.utilities.helpers.MyStringUtils; import eu.eudat.models.rda.Distribution; import java.net.URI; -import java.util.Collections; +import java.util.*; public class DistributionRDAMapper { - public static Distribution toRDA(JsonNode structure, JsonNode properties) { + public static List toRDAList(List nodes) { + Map rdaMap = new HashMap<>(); + + for (JsonNode node: nodes) { + String rdaProperty = node.get("rdaProperty").asText(); + String rdaValue = node.get("value").asText(); + Distribution rda = getRelative(rdaMap, node.get("numbering").asText()); + if (!rdaMap.containsValue(rda)) { + rdaMap.put(node.get("numbering").asText(), rda); + } + for (PropertyName propertyName : PropertyName.values()) { + if (rdaProperty.contains(propertyName.getName())) { + switch (propertyName) { + case ACCESS_URL: + rda.setAccessUrl(rdaValue); + break; + case AVAILABLE_UTIL: + rda.setAvailableUntil(rdaValue); + break; + case DOWNLOAD_URL: + rda.setDownloadUrl(URI.create(rdaValue)); + break; + case DESCRIPTION: + rda.setDescription(rdaValue); + break; + case DATA_ACCESS: + rda.setDataAccess(Distribution.DataAccess.fromValue(rdaValue)); + break; + case BYTE_SIZE: + rda.setByteSize(Integer.parseInt(rdaValue)); + break; + case LICENSE: + rda.setLicense(Collections.singletonList(LicenseRDAMapper.toRDA(node))); + break; + case FORMAT: + rda.setFormat(Collections.singletonList(rdaValue)); + break; + case TITLE: + rda.setTitle(rdaValue); + break; + case HOST: + rda.setHost(HostRDAMapper.toRDA(nodes, node.get("numbering").asText())); + break; + } + } + } + } + + return new ArrayList<>(rdaMap.values()); + } + + public static Distribution toRDA(JsonNode node) { Distribution rda = new Distribution(); - String rdaProperty = structure.get("rdaProperty").asText(); + String rdaProperty = node.get("rdaProperty").asText(); + String rdaValue = node.get("value").asText(); if (rdaProperty.contains("access_url")) { - rda.setAccessUrl(properties.get(structure.get("id").asText()).asText()); + rda.setAccessUrl(rdaValue); } else if (rdaProperty.contains("available_util")) { - rda.setAvailableUntil(properties.get(structure.get("id").asText()).asText()); + rda.setAvailableUntil(rdaValue); } else if (rdaProperty.contains("byte_size")) { - rda.setByteSize(properties.get(structure.get("id").asText()).asInt()); + rda.setByteSize(Integer.parseInt(rdaValue)); } else if (rdaProperty.contains("data_access")) { - rda.setDataAccess(Distribution.DataAccess.fromValue(properties.get(structure.get("id").asText()).asText())); + rda.setDataAccess(Distribution.DataAccess.fromValue(rdaValue)); } else if (rdaProperty.contains("description")) { - rda.setDescription(properties.get(structure.get("id").asText()).asText()); + rda.setDescription(rdaValue); } else if (rdaProperty.contains("download_url")) { - rda.setDownloadUrl(URI.create(properties.get(structure.get("id").asText()).asText())); + rda.setDownloadUrl(URI.create(rdaValue)); } else if (rdaProperty.contains("format")) { - rda.setFormat(Collections.singletonList(properties.get(structure.get("id").asText()).asText())); + rda.setFormat(Collections.singletonList(rdaValue)); } else if (rdaProperty.contains("host")) { - rda.setHost(HostRDAMapper.toRDA(structure, properties)); +// rda.setHost(HostRDAMapper.toRDA(node)); } else if (rdaProperty.contains("license")) { - rda.setLicense(Collections.singletonList(LicenseRDAMapper.toRDA(structure, properties))); + rda.setLicense(Collections.singletonList(LicenseRDAMapper.toRDA(node))); } else if (rdaProperty.contains("title")) { - rda.setTitle(properties.get(structure.get("id").asText()).asText()); + rda.setTitle(rdaValue); } return rda; } + + private static Distribution getRelative( Map rdaMap, String numbering) { + return rdaMap.entrySet().stream().filter(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering) > 0) + .max(Comparator.comparingInt(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering))).map(Map.Entry::getValue).orElse(new Distribution()); + } + + private enum PropertyName { + ACCESS_URL("access_url"), + AVAILABLE_UTIL("available_util"), + BYTE_SIZE("byte_size"), + DATA_ACCESS("data_access"), + DESCRIPTION("description"), + DOWNLOAD_URL("download_url"), + FORMAT("format"), + HOST("host"), + LICENSE("license"), + TITLE("title"); + + private final String name; + + PropertyName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } } diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/HostRDAMapper.java b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/HostRDAMapper.java index 9a32cc697..0983f3d3b 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/HostRDAMapper.java +++ b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/HostRDAMapper.java @@ -1,41 +1,91 @@ package eu.eudat.models.rda.mapper; import com.fasterxml.jackson.databind.JsonNode; +import eu.eudat.logic.utilities.helpers.MyStringUtils; import eu.eudat.models.rda.Host; import eu.eudat.models.rda.PidSystem; import java.net.URI; import java.util.Collections; +import java.util.List; public class HostRDAMapper { - public static Host toRDA(JsonNode structure, JsonNode properties) { + public static Host toRDA(List nodes, String numbering) { Host rda = new Host(); - String rdaProperty = structure.get("rdaProperties").asText(); - if (rdaProperty.contains("availability")) { - rda.setAvailability(properties.get(structure.get("id").asText()).asText()); - } else if (rdaProperty.contains("backup_frequency")) { - rda.setBackupFrequency(properties.get(structure.get("id").asText()).asText()); - } else if (rdaProperty.contains("backup_type")) { - rda.setBackupType(properties.get(structure.get("id").asText()).asText()); - } else if (rdaProperty.contains("certified_with")) { - rda.setCertifiedWith(Host.CertifiedWith.fromValue(properties.get(structure.get("id").asText()).asText())); - } else if (rdaProperty.contains("description")) { - rda.setDescription(properties.get(structure.get("id").asText()).asText()); - } else if (rdaProperty.contains("geo_location")) { - rda.setGeoLocation(Host.GeoLocation.fromValue(properties.get(structure.get("id").asText()).asText())); - } else if (rdaProperty.contains("pid_system")) { - rda.setPidSystem(Collections.singletonList(PidSystem.fromValue(properties.get(structure.get("id").asText()).asText()))); - } else if (rdaProperty.contains("storage_type")) { - rda.setStorageType(properties.get(structure.get("id").asText()).asText()); - } else if (rdaProperty.contains("support_versioning")) { - rda.setSupportVersioning(Host.SupportVersioning.fromValue(properties.get(structure.get("id").asText()).asText())); - } else if (rdaProperty.contains("title")) { - rda.setTitle(properties.get(structure.get("id").asText()).asText()); - } else if (rdaProperty.contains("url")) { - rda.setUrl(URI.create(properties.get(structure.get("id").asText()).asText())); + for (JsonNode node: nodes) { + String rdaProperty = node.get("rdaProperties").asText(); + if (rdaProperty.contains("host")) { + int firstDiff = MyStringUtils.getFirstDifference(numbering, node.get("numbering").asText()); + if (firstDiff == -1 || firstDiff > 2) { + String rdaValue = node.get("value").asText(); + for (PropertyName propertyName: PropertyName.values()) { + if (rdaProperty.contains(propertyName.getName())) { + switch (propertyName) { + case AVAILABILITY: + rda.setAvailability(rdaValue); + break; + case BACKUP_FREQUENCY: + rda.setBackupFrequency(rdaValue); + break; + case BACKUP_TYPE: + rda.setBackupType(rdaValue); + break; + case CERTIFIED_WITH: + rda.setCertifiedWith(Host.CertifiedWith.fromValue(rdaValue)); + break; + case DESCRIPTION: + rda.setDescription(rdaValue); + break; + case GEO_LOCATION: + rda.setGeoLocation(Host.GeoLocation.fromValue(rdaValue)); + break; + case PID_SYSTEM: + rda.setPidSystem(Collections.singletonList(PidSystem.fromValue(rdaValue))); + break; + case STORAGE_TYPE: + rda.setStorageType(rdaValue); + break; + case SUPPORT_VERSIONING: + rda.setSupportVersioning(Host.SupportVersioning.fromValue(rdaValue)); + break; + case TITLE: + rda.setTitle(rdaValue); + break; + case URL: + rda.setUrl(URI.create(rdaValue)); + break; + } + } + } + } + } } return rda; } + + private enum PropertyName { + AVAILABILITY("availability"), + BACKUP_FREQUENCY("backup_frequency"), + BACKUP_TYPE("backup_type"), + CERTIFIED_WITH("certified_with"), + DESCRIPTION("description"), + GEO_LOCATION("geo_location"), + PID_SYSTEM("pid_system"), + STORAGE_TYPE("storage_type"), + SUPPORT_VERSIONING("support_versioning"), + TITLE("title"), + URL("url"); + + private final String name; + + PropertyName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } } diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/LicenseRDAMapper.java b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/LicenseRDAMapper.java index 68817bd3d..6f94a73c4 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/LicenseRDAMapper.java +++ b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/LicenseRDAMapper.java @@ -7,10 +7,10 @@ import java.net.URI; public class LicenseRDAMapper { - public static License toRDA(JsonNode structure, JsonNode properties) { + public static License toRDA(JsonNode node) { License rda = new License(); - String rdaProperty = structure.get("rdaProperty").asText(); - String value = properties.get(structure.get("id").asText()).asText(); + String rdaProperty = node.get("rdaProperty").asText(); + String value = node.get("value").asText(); if (rdaProperty.contains("license_ref")) { rda.setLicenseRef(URI.create(value)); diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/MetadataRDAMapper.java b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/MetadataRDAMapper.java index fa638d3e3..c7e3ba35f 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/MetadataRDAMapper.java +++ b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/MetadataRDAMapper.java @@ -2,43 +2,124 @@ package eu.eudat.models.rda.mapper; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.TextNode; +import eu.eudat.logic.utilities.helpers.MyStringUtils; import eu.eudat.models.rda.Metadatum; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; -import java.util.Iterator; +import java.util.*; public class MetadataRDAMapper { private static final Logger logger = LoggerFactory.getLogger(MetadataRDAMapper.class); - public static Metadatum toRDA(JsonNode structure, JsonNode properties) { - Metadatum rda = new Metadatum(); - JsonNode dataNode = null; - String rdaProperty = structure.get("rdaProperty").asText(); - if (rdaProperty.contains("metadata_standard_id")) { - try { - String jsonText = properties.get(structure.get("id").asText()).asText(); - if (jsonText != null && !jsonText.isEmpty()) { - dataNode = new ObjectMapper().readTree(jsonText); - for (Iterator it = dataNode.elements(); it.hasNext(); ) { - JsonNode data = it.next(); - if (data.get("uri") != null) { - rda.setMetadataStandardId(MetadataStandardIdRDAMapper.toRDA(data.get("uri").asText())); - } + public static List toRDAList(List nodes) { + Map rdaMap = new HashMap<>(); + List rdas = new ArrayList<>(); + for (JsonNode node: nodes) { + String rdaProperty = node.get("rdaProperty").asText(); + JsonNode rdaValue = node.get("value"); + + for (PropertyName propertyName: PropertyName.values()) { + if (rdaProperty.contains(propertyName.getName())) { + switch (propertyName) { + case METADATA_STANDARD_ID: + if (rdaValue instanceof ArrayNode) { + try { + ObjectMapper mapper = new ObjectMapper(); + for (Iterator it = rdaValue.elements(); it.hasNext(); ) { + JsonNode data = null; + data = mapper.readTree(it.next().asText()); + if (data.get("uri") != null) { + rdas.add(new Metadatum()); + rdas.get(rdas.size() - 1).setMetadataStandardId(MetadataStandardIdRDAMapper.toRDA(data.get("uri").asText())); + rdas.get(rdas.size() - 1).setDescription(data.get("label").asText()); + rdaMap.put(data.get("uri").asText(), node.get("numbering").asText()); + } + } + } catch (IOException e) { + logger.error(e.getMessage(), e); + } + } else if (rdaValue instanceof TextNode && rdaProperty.contains("identifier") && !rdaValue.asText().isEmpty()) { + rdas.add(new Metadatum()); + rdas.get(rdas.size() - 1).setMetadataStandardId(MetadataStandardIdRDAMapper.toRDA(rdaValue.asText())); + rdaMap.put(rdaValue.asText(), node.get("numbering").asText()); + } + break; + case DESCRIPTION: + if (!rdaValue.asText().isEmpty()) { + Metadatum rda = getRelative(rdas, rdaMap, node.get("numbering").asText()); + if (rda != null) { + rda.setDescription(rdaValue.asText()); + } else { + rdas.stream().filter(rda1 -> rda1.getDescription() == null || rda1.getDescription().isEmpty()).forEach(rda1 -> rda1.setDescription(rdaValue.asText())); + } + } + break; + case LANGUAGE: + String language = rdaValue.asText(); + Metadatum.Language lang = Metadatum.Language.fromValue(language); + Metadatum rda = getRelative(rdas, rdaMap, node.get("numbering").asText()); + if (rda != null) { + rda.setLanguage(lang); + } else { + rdas.forEach(rda1 -> rda1.setLanguage(lang)); + } + break; + } + } + } + + } + + return rdas; + } + + public static Metadatum toRDA(JsonNode node) { + Metadatum rda = new Metadatum(); + String rdaProperty = node.get("rdaProperty").asText(); + JsonNode rdaValue = node.get("value"); + if (rdaProperty.contains("metadata_standard_id")) { + if (rdaValue instanceof ArrayNode) { + for (Iterator it = rdaValue.elements(); it.hasNext(); ) { + JsonNode data = it.next(); + if (data.get("uri") != null) { + rda.setMetadataStandardId(MetadataStandardIdRDAMapper.toRDA(data.get("uri").asText())); } } - } catch (IOException e) { - logger.error(e.getMessage(), e); } } else if (rdaProperty.contains("description")) { - rda.setDescription(properties.get(structure.get("id").asText()).asText()); + rda.setDescription(rdaValue.asText()); } else if (rdaProperty.contains("language")) { - String language = properties.get(structure.get("id").asText()).asText(); + String language = rdaValue.asText(); Metadatum.Language lang = Metadatum.Language.fromValue(language); rda.setLanguage(lang); } return rda; } + + private static Metadatum getRelative(List rdas, Map rdaMap, String numbering) { + String target = rdaMap.entrySet().stream().filter(entry -> MyStringUtils.getFirstDifference(entry.getValue(), numbering) > 0) + .max(Comparator.comparingInt(entry -> MyStringUtils.getFirstDifference(entry.getValue(), numbering))).map(Map.Entry::getKey).orElse(""); + return rdas.stream().filter(rda -> rda.getMetadataStandardId().getIdentifier().equals(target)).distinct().findFirst().orElse(null); + } + + private enum PropertyName { + METADATA_STANDARD_ID("metadata_standard_id"), + DESCRIPTION("description"), + LANGUAGE("language"); + + private final String name; + + PropertyName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } } diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/SecurityAndPrivacyRDAMapper.java b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/SecurityAndPrivacyRDAMapper.java index f7f95633f..fc44cc182 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/SecurityAndPrivacyRDAMapper.java +++ b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/SecurityAndPrivacyRDAMapper.java @@ -1,14 +1,45 @@ package eu.eudat.models.rda.mapper; import com.fasterxml.jackson.databind.JsonNode; +import eu.eudat.logic.utilities.helpers.MyStringUtils; import eu.eudat.models.rda.SecurityAndPrivacy; +import java.util.*; + public class SecurityAndPrivacyRDAMapper { - public static SecurityAndPrivacy toRDA(JsonNode structure, JsonNode properties) { + public static List toRDAList(List nodes) { + Map rdaMap = new HashMap<>(); + + for (JsonNode node: nodes) { + String rdaProperty = node.get("rdaProperty").asText(); + String rdaValue = node.get("value").asText(); + + SecurityAndPrivacy rda = getRelative(rdaMap, node.get("numbering").asText()); + if (!rdaMap.containsValue(rda)) { + rdaMap.put(node.get("numbering").asText(), rda); + } + for (PropertyName propertyName: PropertyName.values()) { + if (rdaProperty.contains(propertyName.getName())) { + switch (propertyName) { + case TITLE: + rda.setTitle(rdaValue); + break; + case DESCRIPTION: + rda.setDescription(rdaValue); + break; + } + } + } + } + + return new ArrayList<>(rdaMap.values()); + } + + public static SecurityAndPrivacy toRDA(JsonNode node) { SecurityAndPrivacy rda = new SecurityAndPrivacy(); - String rdaProperty = structure.get("rdaProperty").asText(); - String value = properties.get(structure.get("id").asText()).asText(); + String rdaProperty = node.get("rdaProperty").asText(); + String value =node.get("value").asText(); if (rdaProperty.contains("description")) { rda.setDescription(value); @@ -18,4 +49,24 @@ public class SecurityAndPrivacyRDAMapper { return rda; } + + private static SecurityAndPrivacy getRelative(Map rdaMap, String numbering) { + return rdaMap.entrySet().stream().filter(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering) > 0) + .max(Comparator.comparingInt(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering))).map(Map.Entry::getValue).orElse(new SecurityAndPrivacy()); + } + + private enum PropertyName { + TITLE("title"), + DESCRIPTION("description"); + + private String name; + + PropertyName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } } diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/TechnicalResourceRDAMapper.java b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/TechnicalResourceRDAMapper.java index b2652901a..c48f93cae 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/TechnicalResourceRDAMapper.java +++ b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/TechnicalResourceRDAMapper.java @@ -1,14 +1,46 @@ package eu.eudat.models.rda.mapper; import com.fasterxml.jackson.databind.JsonNode; +import eu.eudat.logic.utilities.helpers.MyStringUtils; +import eu.eudat.models.rda.SecurityAndPrivacy; import eu.eudat.models.rda.TechnicalResource; +import java.util.*; + public class TechnicalResourceRDAMapper { - public static TechnicalResource toRDA(JsonNode structure, JsonNode properties) { + public static List toRDAList(List nodes) { + Map rdaMap = new HashMap<>(); + + for (JsonNode node: nodes) { + String rdaProperty = node.get("rdaProperty").asText(); + String rdaValue = node.get("value").asText(); + + TechnicalResource rda = getRelative(rdaMap, node.get("numbering").asText()); + if (!rdaMap.containsValue(rda)) { + rdaMap.put(node.get("numbering").asText(), rda); + } + for (PropertyName propertyName: PropertyName.values()) { + if (rdaProperty.contains(propertyName.getName())) { + switch (propertyName) { + case NAME: + rda.setName(rdaValue); + break; + case DESCRIPTION: + rda.setDescription(rdaValue); + break; + } + } + } + } + + return new ArrayList<>(rdaMap.values()); + } + + public static TechnicalResource toRDA(JsonNode node) { TechnicalResource rda = new TechnicalResource(); - String rdaProperty = structure.get("rdaProperty").asText(); - String value = properties.get(structure.get("id").asText()).asText(); + String rdaProperty = node.get("rdaProperty").asText(); + String value = node.get("value").asText(); if (rdaProperty.contains("description")) { rda.setDescription(value); @@ -18,4 +50,24 @@ public class TechnicalResourceRDAMapper { return rda; } + + private static TechnicalResource getRelative(Map rdaMap, String numbering) { + return rdaMap.entrySet().stream().filter(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering) > 0) + .max(Comparator.comparingInt(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering))).map(Map.Entry::getValue).orElse(new TechnicalResource()); + } + + private enum PropertyName { + NAME("name"), + DESCRIPTION("description"); + + private String name; + + PropertyName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } } diff --git a/dmp-frontend/src/app/ui/misc/dataset-description-form/dataset-description-form.model.ts b/dmp-frontend/src/app/ui/misc/dataset-description-form/dataset-description-form.model.ts index c4b8153b8..fd8824ba3 100644 --- a/dmp-frontend/src/app/ui/misc/dataset-description-form/dataset-description-form.model.ts +++ b/dmp-frontend/src/app/ui/misc/dataset-description-form/dataset-description-form.model.ts @@ -338,4 +338,4 @@ export class DatasetDescriptionDefaultValueEditorModel extends BaseFormModel { }); return formGroup; } -} \ No newline at end of file +}