migration changes

This commit is contained in:
Efstratios Giannopoulos 2024-03-21 15:36:48 +02:00
parent e61f1afaf5
commit 67240affcf
4 changed files with 182 additions and 111 deletions

View File

@ -15,7 +15,6 @@ import eu.eudat.model.Dmp;
import eu.eudat.model.Reference; import eu.eudat.model.Reference;
import eu.eudat.model.ReferenceType; import eu.eudat.model.ReferenceType;
import eu.eudat.query.*; import eu.eudat.query.*;
import eu.eudat.service.reference.ReferenceService;
import eu.old.eudat.data.dao.entities.DatasetDao; import eu.old.eudat.data.dao.entities.DatasetDao;
import eu.old.eudat.data.entities.Dataset; import eu.old.eudat.data.entities.Dataset;
import eu.old.eudat.logic.services.operations.DatabaseRepository; import eu.old.eudat.logic.services.operations.DatabaseRepository;
@ -25,7 +24,6 @@ import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManager;
import jakarta.xml.bind.JAXBException; import jakarta.xml.bind.JAXBException;
import org.apache.commons.lang3.StringEscapeUtils;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -54,16 +52,16 @@ public class DatasetMigrationService {
private static final boolean TestMode = false; private static final boolean TestMode = false;
private final EntityManager entityManager; private final EntityManager entityManager;
private final ConventionService conventionService; private final ConventionService conventionService;
private final ReferenceService referenceService; private final MigrationTools migrationTools;
public DatasetMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, QueryFactory queryFactory, XmlHandlingService xmlHandlingService, EntityManager entityManager, ConventionService conventionService, ReferenceService referenceService) { public DatasetMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, QueryFactory queryFactory, XmlHandlingService xmlHandlingService, EntityManager entityManager, ConventionService conventionService, MigrationTools migrationTools) {
this.databaseRepository = databaseRepository; this.databaseRepository = databaseRepository;
this.jsonHandlingService = jsonHandlingService; this.jsonHandlingService = jsonHandlingService;
this.queryFactory = queryFactory; this.queryFactory = queryFactory;
this.xmlHandlingService = xmlHandlingService; this.xmlHandlingService = xmlHandlingService;
this.entityManager = entityManager; this.entityManager = entityManager;
this.conventionService = conventionService; this.conventionService = conventionService;
this.referenceService = referenceService; this.migrationTools = migrationTools;
} }
public void migrate() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException { public void migrate() throws IOException, JAXBException, ParserConfigurationException, InstantiationException, IllegalAccessException, SAXException {
@ -387,20 +385,20 @@ public class DatasetMigrationService {
} }
case SELECT -> { case SELECT -> {
if(!this.conventionService.isNullOrEmpty(textValue)) { if(!this.conventionService.isNullOrEmpty(textValue)) {
String[] valuesParsed = this.tryParseJsonAsObjectString(String[].class, textValue); String[] valuesParsed = migrationTools.tryParseJsonAsObjectString(String[].class, textValue);
fieldEntity.setTextListValue(valuesParsed == null ? List.of(textValue) : Arrays.stream(valuesParsed).toList()); fieldEntity.setTextListValue(valuesParsed == null ? List.of(textValue) : Arrays.stream(valuesParsed).toList());
} }
} }
case DATASET_IDENTIFIER, VALIDATION -> { case DATASET_IDENTIFIER, VALIDATION -> {
if(!this.conventionService.isNullOrEmpty(textValue)) { if(!this.conventionService.isNullOrEmpty(textValue)) {
ExternalIdentifierEntity externalIdentifierEntity = this.tryParseJsonAsObjectString(ExternalIdentifierEntity.class, textValue); ExternalIdentifierEntity externalIdentifierEntity = migrationTools.tryParseJsonAsObjectString(ExternalIdentifierEntity.class, textValue);
if (externalIdentifierEntity == null) throw new MyApplicationException("Could not parse dataset External Identifier : " + textValue); if (externalIdentifierEntity == null) throw new MyApplicationException("Could not parse dataset External Identifier : " + textValue);
fieldEntity.setExternalIdentifier(externalIdentifierEntity); fieldEntity.setExternalIdentifier(externalIdentifierEntity);
} }
} }
case UPLOAD -> { case UPLOAD -> {
if(!this.conventionService.isNullOrEmpty(textValue)) { if(!this.conventionService.isNullOrEmpty(textValue)) {
Map<String, String> valuesParsed = this.tryParseJsonAsObjectString(Map.class, textValue); Map<String, String> valuesParsed = migrationTools.tryParseJsonAsObjectString(Map.class, textValue);
if (valuesParsed == null) { if (valuesParsed == null) {
valuesParsed = (Map<String, String>) properties.get(currentField.getId()); valuesParsed = (Map<String, String>) properties.get(currentField.getId());
} }
@ -412,7 +410,7 @@ public class DatasetMigrationService {
} }
case CURRENCY -> { case CURRENCY -> {
if(!this.conventionService.isNullOrEmpty(textValue)) { if(!this.conventionService.isNullOrEmpty(textValue)) {
Currency currency = this.tryParseJsonAsObjectString(Currency.class, textValue); Currency currency = migrationTools.tryParseJsonAsObjectString(Currency.class, textValue);
if (currency == null && textValue.toUpperCase(Locale.ROOT).contains("EUR")) { if (currency == null && textValue.toUpperCase(Locale.ROOT).contains("EUR")) {
currency = new Currency(); currency = new Currency();
currency.setName("Euro"); currency.setName("Euro");
@ -431,12 +429,12 @@ public class DatasetMigrationService {
} }
case TAGS -> { case TAGS -> {
if(!this.conventionService.isNullOrEmpty(textValue)) { if(!this.conventionService.isNullOrEmpty(textValue)) {
Tag[] tags = this.tryParseJsonAsObjectString(Tag[].class, textValue); Tag[] tags = migrationTools.tryParseJsonAsObjectString(Tag[].class, textValue);
if (tags == null) { if (tags == null) {
Tag tag = this.tryParseJsonAsObjectString(Tag.class, textValue); Tag tag = migrationTools.tryParseJsonAsObjectString(Tag.class, textValue);
if (tag != null) tags = List.of(tag).toArray(Tag[]::new); if (tag != null) tags = List.of(tag).toArray(Tag[]::new);
} }
if (tags == null) tags = this.jsonHandlingService.fromJsonSafe(Tag[].class, this.cleanTagString(textValue)); if (tags == null) tags = this.jsonHandlingService.fromJsonSafe(Tag[].class, migrationTools.cleanTagString(textValue));
if (tags == null) throw new MyApplicationException("Could not parse tag : " + textValue); if (tags == null) throw new MyApplicationException("Could not parse tag : " + textValue);
fieldEntity.setTextListValue(Arrays.stream(tags).toList().stream().map(Tag::getName).toList()); fieldEntity.setTextListValue(Arrays.stream(tags).toList().stream().map(Tag::getName).toList());
} }
@ -446,13 +444,13 @@ public class DatasetMigrationService {
case INTERNAL_ENTRIES_DESCRIPTIONS -> throw new MyApplicationException("Found INTERNAL_ENTRIES_DMPS into description"); case INTERNAL_ENTRIES_DESCRIPTIONS -> throw new MyApplicationException("Found INTERNAL_ENTRIES_DMPS into description");
case REFERENCE_TYPES -> { case REFERENCE_TYPES -> {
if(!this.conventionService.isNullOrEmpty(textValue)) { //TODO if(!this.conventionService.isNullOrEmpty(textValue)) { //TODO
Map<String, Object>[] references = this.tryParseJsonAsObjectString(Map[].class, textValue); Map<String, Object>[] references = migrationTools.tryParseJsonAsObjectString(Map[].class, textValue);
ReferenceTypeDataEntity referenceTypeDataEntity = (ReferenceTypeDataEntity)currentField.getData(); ReferenceTypeDataEntity referenceTypeDataEntity = (ReferenceTypeDataEntity)currentField.getData();
if (referenceTypeDataEntity == null) throw new MyApplicationException("Could not parse description template reference : " + textValue); if (referenceTypeDataEntity == null) throw new MyApplicationException("Could not parse description template reference : " + textValue);
if (references == null) { if (references == null) {
Map<String, Object> reference = this.tryParseJsonAsObjectString(Map.class, textValue); Map<String, Object> reference = migrationTools.tryParseJsonAsObjectString(Map.class, textValue);
if (reference != null) references = List.of(reference).toArray(Map[]::new); if (reference != null) references = List.of(reference).toArray(Map[]::new);
} }
if (references == null && !textValue.contains("{") && !textValue.contains("[") ) { if (references == null && !textValue.contains("{") && !textValue.contains("[") ) {
@ -506,7 +504,7 @@ public class DatasetMigrationService {
source = referenceTypeDefinition.getSources().getFirst().getKey(); source = referenceTypeDefinition.getSources().getFirst().getKey();
String[] parts = null; String[] parts = null;
if (textValue.startsWith("[\"")){ if (textValue.startsWith("[\"")){
parts = this.tryParseJsonAsObjectString(String[].class, textValue); parts = migrationTools.tryParseJsonAsObjectString(String[].class, textValue);
} else{ } else{
if (textValue.startsWith("[")) textValue = textValue.substring(1).trim(); if (textValue.startsWith("[")) textValue = textValue.substring(1).trim();
if (textValue.endsWith("]")) textValue = textValue.substring(0, textValue.length() -1).trim(); if (textValue.endsWith("]")) textValue = textValue.substring(0, textValue.length() -1).trim();
@ -564,7 +562,7 @@ public class DatasetMigrationService {
if (textValue.equals("[\"-\"]")){ if (textValue.equals("[\"-\"]")){
break; break;
} else if (textValue.startsWith("[\"") && !textValue.startsWith("[\"{") && textValue.contains(",")){ } else if (textValue.startsWith("[\"") && !textValue.startsWith("[\"{") && textValue.contains(",")){
references = this.tryParseJsonAsObjectString(Map[].class, textValue.replace(textValue.substring(1, textValue.indexOf(",") + 1), "")); references = migrationTools.tryParseJsonAsObjectString(Map[].class, textValue.replace(textValue.substring(1, textValue.indexOf(",") + 1), ""));
} else if (textValue.contains(",") && !textValue.contains("{") && !textValue.contains("[") ){ } else if (textValue.contains(",") && !textValue.contains("{") && !textValue.contains("[") ){
String[] parts = textValue.split(","); String[] parts = textValue.split(",");
@ -601,7 +599,7 @@ public class DatasetMigrationService {
} else if (!textValue.contains("{")) { } else if (!textValue.contains("{")) {
String[] parts = null; String[] parts = null;
if (textValue.startsWith("[\"")){ if (textValue.startsWith("[\"")){
parts = this.tryParseJsonAsObjectString(String[].class, textValue); parts = migrationTools.tryParseJsonAsObjectString(String[].class, textValue);
} else{ } else{
if (textValue.startsWith("[")) textValue = textValue.substring(1).trim(); if (textValue.startsWith("[")) textValue = textValue.substring(1).trim();
if (textValue.endsWith("]")) textValue = textValue.substring(0, textValue.length() -1).trim(); if (textValue.endsWith("]")) textValue = textValue.substring(0, textValue.length() -1).trim();
@ -985,81 +983,6 @@ public class DatasetMigrationService {
} }
private <T> T tryParseJsonAsObjectString(Class<T> type, String value){
T item = this.jsonHandlingService.fromJsonSafe(type, value);
if (item == null) item = this.jsonHandlingService.fromJsonSafe(type, StringEscapeUtils.unescapeJava(value));
if (item == null) item = this.jsonHandlingService.fromJsonSafe(type, StringEscapeUtils.unescapeJson(value));
if (item == null) {
String newValue = StringEscapeUtils.unescapeJava(value);
newValue = newValue.trim().replace("[\"{", "[{");
newValue = newValue.trim().replace("}\"]", "}]");
newValue = newValue.trim().replace("}\", \"{", "}, {");
newValue = newValue.trim().replace("}\",\"{", "},{");
newValue = newValue.trim().replace(", \"{", ", {");
newValue = newValue.trim().replace(",\"{", ",{");
newValue = newValue.trim().replace("}\",", "},");
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.startsWith("\"")) newValue = newValue.substring(1);
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.endsWith("\"")) newValue = newValue.substring(0, newValue.length() - 1);
item = this.jsonHandlingService.fromJsonSafe(type, newValue);
}
if (item == null) {
String newValue = StringEscapeUtils.unescapeJson(value);
newValue = newValue.trim().replace("[\"{", "[{");
newValue = newValue.trim().replace("}\"]", "}]");
newValue = newValue.trim().replace("}\", \"{", "}, {");
newValue = newValue.trim().replace("}\",\"{", "},{");
newValue = newValue.trim().replace(", \"{", ", {");
newValue = newValue.trim().replace(",\"{", ",{");
newValue = newValue.trim().replace("}\",", "},");
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.startsWith("\"")) newValue = newValue.substring(1);
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.endsWith("\"")) newValue = newValue.substring(0, newValue.length() - 1);
item = this.jsonHandlingService.fromJsonSafe(type, newValue);
}
if (item == null) {
String newValue = value.trim().replace("\\", "");
newValue = newValue.trim().replace("[\"{", "[{");
newValue = newValue.trim().replace("}\"]", "}]");
newValue = newValue.trim().replace("}\", \"{", "}, {");
newValue = newValue.trim().replace("}\",\"{", "},{");
newValue = newValue.trim().replace(", \"{", ", {");
newValue = newValue.trim().replace(",\"{", ",{");
newValue = newValue.trim().replace("}\",", "},");
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.startsWith("\"")) newValue = newValue.substring(1);
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.endsWith("\"")) newValue = newValue.substring(0, newValue.length() - 1);
item = this.jsonHandlingService.fromJsonSafe(type, newValue);
}
if (item == null) {
String newValue = value.trim().replace("=", ":");
newValue = newValue.trim().replace("{", "{\"");
newValue = newValue.trim().replace("}", "\"}");
newValue = newValue.trim().replace(":", "\":\"");
newValue = newValue.trim().replace(", ", "\", \"");
newValue = newValue.trim().replace("}\", \"{", "}, {");
newValue = newValue.trim().replace("\"\"", "\"");
newValue = newValue.trim().replace(":\"null,", ":null,");
newValue = newValue.trim().replace(":\"null\",", ":null,");
newValue = newValue.trim().replace(":\"null\"", ":null");
newValue = newValue.trim().replace(":\"null}\"", ":null}");
newValue = newValue.trim().replace("https\":\"", "https:");
newValue = newValue.trim().replace("datarepo\":\"", "datarepo:");
item = this.jsonHandlingService.fromJsonSafe(type, newValue);
}
return item;
}
private String cleanTagString(String value){
value = value.trim().replace("=", ":");
value = value.trim().replace("{", "{\"");
value = value.trim().replace("}", "\"}");
value = value.trim().replace(":", "\":\"");
value = value.trim().replace(", ", "\", \"");
value = value.trim().replace("}\", \"{", "}, {");
return value;
}
public static class Currency{ public static class Currency{
private String name; private String name;

View File

@ -34,12 +34,16 @@ import org.w3c.dom.Document;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import javax.management.InvalidApplicationException; import javax.management.InvalidApplicationException;
import javax.ws.rs.NotSupportedException;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerException;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.time.Instant; import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -51,15 +55,17 @@ public class DescriptionTemplateXmlMigrationService {
private final ConventionService conventionService; private final ConventionService conventionService;
private final XmlHandlingService xmlHandlingService; private final XmlHandlingService xmlHandlingService;
private final QueryFactory queryFactory; private final QueryFactory queryFactory;
private final MigrationTools migrationTools;
private static final int PageSize = 500; private static final int PageSize = 500;
private static final boolean TestMode = false; private static final boolean TestMode = false;
public DescriptionTemplateXmlMigrationService(EntityManager entityManager, ConventionService conventionService, XmlHandlingService xmlHandlingService, QueryFactory queryFactory) { public DescriptionTemplateXmlMigrationService(EntityManager entityManager, ConventionService conventionService, XmlHandlingService xmlHandlingService, QueryFactory queryFactory, MigrationTools migrationTools) {
this.entityManager = entityManager; this.entityManager = entityManager;
this.conventionService = conventionService; this.conventionService = conventionService;
this.xmlHandlingService = xmlHandlingService; this.xmlHandlingService = xmlHandlingService;
this.queryFactory = queryFactory; this.queryFactory = queryFactory;
this.migrationTools = migrationTools;
} }
public void migrate() throws IOException, NoSuchFieldException, IllegalAccessException, JAXBException, ParserConfigurationException, InstantiationException, SAXException, InvalidApplicationException, TransformerException, URISyntaxException { public void migrate() throws IOException, NoSuchFieldException, IllegalAccessException, JAXBException, ParserConfigurationException, InstantiationException, SAXException, InvalidApplicationException, TransformerException, URISyntaxException {
@ -223,7 +229,7 @@ public class DescriptionTemplateXmlMigrationService {
if (persist.getVisible() != null && !this.conventionService.isListNullOrEmpty(persist.getVisible().getRules())) { if (persist.getVisible() != null && !this.conventionService.isListNullOrEmpty(persist.getVisible().getRules())) {
data.setVisibilityRules(new ArrayList<>()); data.setVisibilityRules(new ArrayList<>());
for (Rule fieldPersist : persist.getVisible().getRules()) { for (Rule fieldPersist : persist.getVisible().getRules()) {
data.getVisibilityRules().add(this.buildRuleEntity(fieldPersist)); data.getVisibilityRules().add(this.buildRuleEntity(fieldPersist, data.getData().getFieldType()));
} }
} }
return data; return data;
@ -392,13 +398,50 @@ public class DescriptionTemplateXmlMigrationService {
} }
private @NotNull RuleEntity buildRuleEntity(Rule persist) { private @NotNull RuleEntity buildRuleEntity(Rule persist, FieldType fieldType) {
RuleEntity data = new RuleEntity(); RuleEntity data = new RuleEntity();
if (persist == null) if (persist == null)
return data; return data;
String textValue = persist.getValue();
if (textValue == null || textValue.isEmpty()) return data;
data.setTarget(persist.getTarget()); data.setTarget(persist.getTarget());
data.setValue(persist.getValue()); switch (fieldType){
case FREE_TEXT, TEXT_AREA, RICH_TEXT_AREA, RADIO_BOX -> data.setTextValue(textValue.trim());
case CHECK_BOX, BOOLEAN_DECISION -> data.setTextValue(textValue.trim().toLowerCase(Locale.ROOT));
case DATE_PICKER -> {
Instant instant = null;
if(!this.conventionService.isNullOrEmpty(textValue)) {
try {
instant = Instant.parse(textValue);
} catch (DateTimeParseException ex) {
instant = LocalDate.parse(textValue).atStartOfDay().toInstant(ZoneOffset.UTC);
}
}
data.setDateValue(instant);
}
case SELECT -> {
if(!this.conventionService.isNullOrEmpty(textValue)) {
String[] valuesParsed = migrationTools.tryParseJsonAsObjectString(String[].class, textValue);
data.setTextListValue(valuesParsed == null ? List.of(textValue) : Arrays.stream(valuesParsed).toList());
}
}
case DATASET_IDENTIFIER, VALIDATION -> {
if(!this.conventionService.isNullOrEmpty(textValue)) {
eu.eudat.commons.types.descriptiontemplate.ExternalIdentifierEntity externalIdentifierEntity = migrationTools.tryParseJsonAsObjectString(eu.eudat.commons.types.descriptiontemplate.ExternalIdentifierEntity.class, textValue);
if (externalIdentifierEntity == null) throw new MyApplicationException("Could not parse dataset External Identifier : " + textValue);
data.setExternalIdentifier(externalIdentifierEntity);
}
}
case UPLOAD -> throw new NotSupportedException("Upload validator not supported");
case CURRENCY -> throw new NotSupportedException("Currency validator not supported");
case TAGS -> throw new NotSupportedException("Tags validator not supported");
case INTERNAL_ENTRIES_DMPS -> throw new NotSupportedException("INTERNAL_ENTRIES_DMPS validator not supported");
case INTERNAL_ENTRIES_DESCRIPTIONS -> throw new NotSupportedException("INTERNAL_ENTRIES_DESCRIPTIONS validator not supported");
case REFERENCE_TYPES -> throw new NotSupportedException("REFERENCE_TYPES validator not supported");
default -> throw new MyApplicationException("unrecognized type " + fieldType);
}
return data; return data;
} }
private @NotNull MultiplicityEntity buildMultiplicityEntity(Multiplicity persist) { private @NotNull MultiplicityEntity buildMultiplicityEntity(Multiplicity persist) {

View File

@ -100,7 +100,7 @@ public class DmpMigrationService {
DataManagementPlan model = new DataManagementPlan(); DataManagementPlan model = new DataManagementPlan();
model.fromDataModel(item); model.fromDataModel(item);
List<DMP> itemGroupDmps = groupDmpMap.get(item.getGroupId()); List<DMP> itemGroupDmps = groupDmpMap.get(item.getGroupId());
DMP currentVersionDmp = itemGroupDmps.stream().max(Comparator.comparing(DMP::getVersion)).orElseThrow(); DMP currentVersionDmp = itemGroupDmps.stream().filter(x-> DmpStatus.Finalized.getValue().equals(x.getStatus())).max(Comparator.comparing(DMP::getVersion)).orElse(null);
DmpPropertiesEntity dmpProperties = new DmpPropertiesEntity(); DmpPropertiesEntity dmpProperties = new DmpPropertiesEntity();
dmpProperties.setDmpBlueprintValues(new ArrayList<>()); dmpProperties.setDmpBlueprintValues(new ArrayList<>());
@ -124,7 +124,16 @@ public class DmpMigrationService {
} }
data.setGroupId(model.getGroupId()); data.setGroupId(model.getGroupId());
data.setVersion((short) model.getVersion()); data.setVersion((short) model.getVersion());
data.setVersionStatus(currentVersionDmp.getId().equals(item.getId()) ? DmpVersionStatus.Current: DmpVersionStatus.Previous); if (currentVersionDmp != null && currentVersionDmp.getId().equals(item.getId())){
data.setVersionStatus(DmpVersionStatus.Current);
} else {
if (DmpStatus.Finalized.getValue().equals(item.getStatus())){
if (currentVersionDmp == null) throw new MyApplicationException("Found Finalized Dmp out of the group ");
data.setVersionStatus(DmpVersionStatus.Previous);
} else {
data.setVersionStatus(DmpVersionStatus.NotFinalized);
}
}
if (model.getCreated() != null) if (model.getCreated() != null)
data.setCreatedAt(model.getCreated().toInstant()); data.setCreatedAt(model.getCreated().toInstant());
if (model.getModified() != null) if (model.getModified() != null)
@ -170,24 +179,25 @@ public class DmpMigrationService {
} }
if (data.getCreatorId() == null){ if (data.getCreatorId() == null){
throw new MyApplicationException("Migration skipped creator not found " + item.getId()); throw new MyApplicationException("Migration skipped creator not found " + item.getId());
} else {
boolean shouldCreateOwner = item.getUsers() == null || item.getUsers().stream().noneMatch(x-> x.getUser() != null && data.getCreatorId().equals(x.getUser().getId()) && DmpUserRole.Owner.getValue().equals(x.getRole().shortValue()));
if (shouldCreateOwner) {
DmpUserEntity dmpUserEntity = new DmpUserEntity();
dmpUserEntity.setId(item.getId());
dmpUserEntity.setDmpId(item.getId());
dmpUserEntity.setUserId(data.getCreatorId());
dmpUserEntity.setRole(DmpUserRole.Owner);
dmpUserEntity.setCreatedAt(data.getCreatedAt());
dmpUserEntity.setUpdatedAt(data.getCreatedAt());
dmpUserEntity.setIsActive(IsActive.Active);
this.entityManager.persist(dmpUserEntity);
}
} }
this.entityManager.persist(data); this.entityManager.persist(data);
this.entityManager.flush(); this.entityManager.flush();
boolean shouldCreateOwner = item.getUsers() == null || item.getUsers().stream().noneMatch(x-> x.getUser() != null && data.getCreatorId().equals(x.getUser().getId()) && DmpUserRole.Owner.getValue().equals(x.getRole().shortValue()));
if (shouldCreateOwner) {
DmpUserEntity dmpUserEntity = new DmpUserEntity();
dmpUserEntity.setId(UUID.randomUUID());
dmpUserEntity.setDmpId(data.getId());
dmpUserEntity.setUserId(data.getCreatorId());
dmpUserEntity.setRole(DmpUserRole.Owner);
dmpUserEntity.setCreatedAt(data.getCreatedAt());
dmpUserEntity.setUpdatedAt(data.getCreatedAt());
dmpUserEntity.setIsActive(IsActive.Active);
this.entityManager.persist(dmpUserEntity);
this.entityManager.flush();
}
licenseIdByName = this.migrateLicense(item, model, licenseIdByName, referenceTypeFieldsByDmpBlueprintId.getOrDefault(data.getBlueprintId(), null)); licenseIdByName = this.migrateLicense(item, model, licenseIdByName, referenceTypeFieldsByDmpBlueprintId.getOrDefault(data.getBlueprintId(), null));
this.migrateOrganizations(item, referenceTypeFieldsByDmpBlueprintId.getOrDefault(data.getBlueprintId(), null)); this.migrateOrganizations(item, referenceTypeFieldsByDmpBlueprintId.getOrDefault(data.getBlueprintId(), null));
this.migrateResearchers(item, referenceTypeFieldsByDmpBlueprintId.getOrDefault(data.getBlueprintId(), null)); this.migrateResearchers(item, referenceTypeFieldsByDmpBlueprintId.getOrDefault(data.getBlueprintId(), null));

View File

@ -0,0 +1,95 @@
package eu.old.eudat.migration;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.convention.ConventionService;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.stereotype.Service;
@Service
public class MigrationTools {
private final JsonHandlingService jsonHandlingService;
private final ConventionService conventionService;
public MigrationTools(JsonHandlingService jsonHandlingService, ConventionService conventionService) {
this.jsonHandlingService = jsonHandlingService;
this.conventionService = conventionService;
}
public <T> T tryParseJsonAsObjectString(Class<T> type, String value){
T item = this.jsonHandlingService.fromJsonSafe(type, value);
if (item == null) item = this.jsonHandlingService.fromJsonSafe(type, StringEscapeUtils.unescapeJava(value));
if (item == null) item = this.jsonHandlingService.fromJsonSafe(type, StringEscapeUtils.unescapeJson(value));
if (item == null) {
String newValue = StringEscapeUtils.unescapeJava(value);
newValue = newValue.trim().replace("[\"{", "[{");
newValue = newValue.trim().replace("}\"]", "}]");
newValue = newValue.trim().replace("}\", \"{", "}, {");
newValue = newValue.trim().replace("}\",\"{", "},{");
newValue = newValue.trim().replace(", \"{", ", {");
newValue = newValue.trim().replace(",\"{", ",{");
newValue = newValue.trim().replace("}\",", "},");
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.startsWith("\"")) newValue = newValue.substring(1);
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.endsWith("\"")) newValue = newValue.substring(0, newValue.length() - 1);
item = this.jsonHandlingService.fromJsonSafe(type, newValue);
}
if (item == null) {
String newValue = StringEscapeUtils.unescapeJson(value);
newValue = newValue.trim().replace("[\"{", "[{");
newValue = newValue.trim().replace("}\"]", "}]");
newValue = newValue.trim().replace("}\", \"{", "}, {");
newValue = newValue.trim().replace("}\",\"{", "},{");
newValue = newValue.trim().replace(", \"{", ", {");
newValue = newValue.trim().replace(",\"{", ",{");
newValue = newValue.trim().replace("}\",", "},");
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.startsWith("\"")) newValue = newValue.substring(1);
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.endsWith("\"")) newValue = newValue.substring(0, newValue.length() - 1);
item = this.jsonHandlingService.fromJsonSafe(type, newValue);
}
if (item == null) {
String newValue = value.trim().replace("\\", "");
newValue = newValue.trim().replace("[\"{", "[{");
newValue = newValue.trim().replace("}\"]", "}]");
newValue = newValue.trim().replace("}\", \"{", "}, {");
newValue = newValue.trim().replace("}\",\"{", "},{");
newValue = newValue.trim().replace(", \"{", ", {");
newValue = newValue.trim().replace(",\"{", ",{");
newValue = newValue.trim().replace("}\",", "},");
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.startsWith("\"")) newValue = newValue.substring(1);
while (!this.conventionService.isNullOrEmpty(newValue) && newValue.endsWith("\"")) newValue = newValue.substring(0, newValue.length() - 1);
item = this.jsonHandlingService.fromJsonSafe(type, newValue);
}
if (item == null) {
String newValue = value.trim().replace("=", ":");
newValue = newValue.trim().replace("{", "{\"");
newValue = newValue.trim().replace("}", "\"}");
newValue = newValue.trim().replace(":", "\":\"");
newValue = newValue.trim().replace(", ", "\", \"");
newValue = newValue.trim().replace("}\", \"{", "}, {");
newValue = newValue.trim().replace("\"\"", "\"");
newValue = newValue.trim().replace(":\"null,", ":null,");
newValue = newValue.trim().replace(":\"null\",", ":null,");
newValue = newValue.trim().replace(":\"null\"", ":null");
newValue = newValue.trim().replace(":\"null}\"", ":null}");
newValue = newValue.trim().replace("https\":\"", "https:");
newValue = newValue.trim().replace("datarepo\":\"", "datarepo:");
item = this.jsonHandlingService.fromJsonSafe(type, newValue);
}
return item;
}
public String cleanTagString(String value){
value = value.trim().replace("=", ":");
value = value.trim().replace("{", "{\"");
value = value.trim().replace("}", "\"}");
value = value.trim().replace(":", "\":\"");
value = value.trim().replace(", ", "\", \"");
value = value.trim().replace("}\", \"{", "}, {");
return value;
}
}