Dmp migration refactored on DmpMigrationService

This commit is contained in:
Thomas Georgios Giannos 2023-11-22 10:24:30 +02:00
parent db6cb44ff8
commit 17b0f9bc06
3 changed files with 156 additions and 129 deletions

View File

@ -0,0 +1,138 @@
package eu.old.eudat.migration;
import com.fasterxml.jackson.core.JsonProcessingException;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.enums.DmpAccessType;
import eu.eudat.commons.enums.DmpStatus;
import eu.eudat.commons.enums.DmpVersionStatus;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.types.dmp.DmpBlueprintValueEntity;
import eu.eudat.commons.types.dmp.DmpContactEntity;
import eu.eudat.commons.types.dmp.DmpPropertiesEntity;
import eu.eudat.data.DmpEntity;
import eu.old.eudat.data.entities.DMP;
import eu.old.eudat.logic.services.operations.DatabaseRepository;
import eu.old.eudat.models.data.dmp.DataManagementPlan;
import eu.old.eudat.publicapi.migration.MigrationController;
import eu.old.eudat.queryable.QueryableList;
import jakarta.persistence.EntityManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Service
public class DmpMigrationService {
private final DatabaseRepository databaseRepository;
private final JsonHandlingService jsonHandlingService;
private final EntityManager entityManager;
public DmpMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, EntityManager entityManager) {
this.databaseRepository = databaseRepository;
this.jsonHandlingService = jsonHandlingService;
this.entityManager = entityManager;
}
public QueryableList<DMP> dmpQueryableList() {
return databaseRepository.getDmpDao().asQueryable();
}
public List<DataManagementPlan> getDmps(@PathVariable("skip") Integer skip, @PathVariable("take") Integer take) {
List<DMP> dmps = dmpQueryableList()
.orderBy((builder, root) -> builder.desc(root.get("created")))
.skip(skip)
.take(take)
.toList();
return dmps.stream().map(x -> {
DataManagementPlan dmp = new DataManagementPlan();
dmp.fromDataModel(x);
return dmp;
}).toList();
}
public List<DmpMigrationCollectedInfo> collectDmpsInfo() throws JsonProcessingException {
List<DmpMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
List<DataManagementPlan> dmps = getDmps(0, 4);
for (DataManagementPlan dataManagementPlan : dmps) {
DmpMigrationCollectedInfo dmpMigrationCollectedInfo = new DmpMigrationCollectedInfo();
DmpPropertiesEntity dmpProperties = new DmpPropertiesEntity();
dmpProperties.setDmpBlueprintValues(new ArrayList<>());
dmpProperties.setContacts(new ArrayList<>());
//Collect basic dmp information
DmpEntity dmpEntity = new DmpEntity();
dmpEntity.setId(UUID.randomUUID());
dmpEntity.setLabel(dataManagementPlan.getLabel());
dmpEntity.setDescription(dataManagementPlan.getDescription());
if (dataManagementPlan.getCreator() != null)
dmpEntity.setCreatorId(dataManagementPlan.getCreator().getId());
dmpEntity.setGroupId(dataManagementPlan.getGroupId());
dmpEntity.setVersion((short) dataManagementPlan.getVersion());
dmpEntity.setVersionStatus(DmpVersionStatus.of((short) dataManagementPlan.getVersionStatus()));
if (dataManagementPlan.getCreated() != null)
dmpEntity.setCreatedAt(dataManagementPlan.getCreated().toInstant());
if (dataManagementPlan.getModified() != null)
dmpEntity.setUpdatedAt(dataManagementPlan.getModified().toInstant());
if (dataManagementPlan.getFinalized() != null)
dmpEntity.setFinalizedAt(dataManagementPlan.getFinalized().toInstant());
dmpEntity.setBlueprintId(dataManagementPlan.getProfile().getId());
if (dataManagementPlan.getExtraProperties() != null) {
if (dataManagementPlan.getExtraProperties().containsKey("language") && dataManagementPlan.getExtraProperties().get("language") != null)
dmpEntity.setLanguage((String) dataManagementPlan.getExtraProperties().get("language"));
if (dataManagementPlan.getExtraProperties().containsKey("visible") && dataManagementPlan.getExtraProperties().get("visible") != null)
dmpEntity.setAccessType((boolean) dataManagementPlan.getExtraProperties().get("visible") ? DmpAccessType.Public : DmpAccessType.Restricted);
if (dataManagementPlan.getExtraProperties().containsKey("contact") && dataManagementPlan.getExtraProperties().get("contact") != null) {
DmpContactEntity contactEntity = new DmpContactEntity();
contactEntity.setUserId((String) dataManagementPlan.getExtraProperties().get("contact"));
dmpProperties.getContacts().add(contactEntity);
}
}
if (dataManagementPlan.getProperties() != null) {
dataManagementPlan.getProperties().forEach((key,val) -> {
DmpBlueprintValueEntity valueEntity = new DmpBlueprintValueEntity();
valueEntity.setFieldId(key);
valueEntity.setValue((String) val);
dmpProperties.getDmpBlueprintValues().add(valueEntity);
});
}
if (dataManagementPlan.getStatus() == 99) {
dmpEntity.setIsActive(IsActive.Inactive);
} else {
dmpEntity.setIsActive(IsActive.Active);
dmpEntity.setStatus(DmpStatus.of((short) dataManagementPlan.getStatus()));
}
dmpEntity.setProperties(jsonHandlingService.toJson(dmpProperties));
dmpMigrationCollectedInfo.dmpEntity = dmpEntity;
//Collect dmp Organization info
collectedInfoList.add(dmpMigrationCollectedInfo);
}
return collectedInfoList;
}
@Transactional
public String migrate() throws JsonProcessingException {
for (DmpMigrationCollectedInfo collectedInfo : collectDmpsInfo()) {
this.entityManager.persist(collectedInfo.dmpEntity);
}
this.entityManager.flush();
return "Migrated dmps";
}
public static class DmpMigrationCollectedInfo {
public DmpEntity dmpEntity;
}
}

View File

@ -14,7 +14,7 @@ import eu.old.eudat.models.data.grant.Grant;
import eu.old.eudat.models.data.project.Project;
import eu.old.eudat.models.data.userinfo.UserListingModel;
import eu.old.eudat.models.data.userinfo.UserInfo;
import net.minidev.json.JSONObject;
import org.json.JSONObject;
import java.util.*;
import java.util.stream.Collectors;
@ -257,7 +257,15 @@ public class DataManagementPlan implements DataModel<DMP, DataManagementPlan> {
this.version = entity.getVersion();
this.groupId = this.groupId == null ? null : entity.getGroupId();
this.label = entity.getLabel();
this.properties = entity.getProperties() != null ? new org.json.JSONObject(entity.getProperties()).toMap() : null;
if (entity.getProperties() != null) {
JSONObject propertiesJson = new JSONObject(entity.getProperties());
if (propertiesJson.has("fields")) {
//Ignore, the format won't be on the real database, only on our dev database
this.properties = new HashMap<>();
} else {
this.properties = propertiesJson.toMap();
}
}
if(entity.getGrant() != null) {
this.grant = new eu.old.eudat.models.data.grant.Grant();
this.grant.fromDataModel(entity.getGrant());
@ -368,18 +376,18 @@ public class DataManagementPlan implements DataModel<DMP, DataManagementPlan> {
if(this.extraFields != null) {
this.properties = this.extraFields.stream().collect(Collectors.toMap(ExtraFieldModel::getId, ExtraFieldModel::getValue));
}
dataManagementPlanEntity.setProperties(this.properties != null ? JSONObject.toJSONString(this.properties) : null);
dataManagementPlanEntity.setProperties(this.properties != null ? JSONObject.valueToString(this.properties) : null);
dataManagementPlanEntity.setGroupId(this.groupId != null ? this.groupId : UUID.randomUUID());
dataManagementPlanEntity.setModified(this.modified != null ? this.modified : new Date());
dataManagementPlanEntity.setCreated(this.created != null ? this.created : new Date());
if (this.dynamicFields != null)
dataManagementPlanEntity.setDmpProperties(JSONObject.toJSONString(this.dynamicFields.stream().filter(item -> item.getValue() != null).collect(Collectors.toMap(DynamicFieldWithValue::getId, DynamicFieldWithValue::getValue))));
dataManagementPlanEntity.setDmpProperties(JSONObject.valueToString(this.dynamicFields.stream().filter(item -> item.getValue() != null).collect(Collectors.toMap(DynamicFieldWithValue::getId, DynamicFieldWithValue::getValue))));
if (this.isPublic != null) {
dataManagementPlanEntity.setPublic(this.isPublic);
}
dataManagementPlanEntity.setExtraProperties(this.extraProperties != null ? JSONObject.toJSONString(this.extraProperties) : null);
dataManagementPlanEntity.setExtraProperties(this.extraProperties != null ? JSONObject.valueToString(this.extraProperties) : null);
return dataManagementPlanEntity;
}

View File

@ -2,148 +2,35 @@ package eu.old.eudat.publicapi.migration;
import com.fasterxml.jackson.core.JsonProcessingException;
import eu.eudat.commons.JsonHandlingService;
import eu.eudat.commons.enums.*;
import eu.eudat.commons.types.dmp.DmpBlueprintValueEntity;
import eu.eudat.commons.types.dmp.DmpContactEntity;
import eu.eudat.commons.types.dmp.DmpPropertiesEntity;
import eu.eudat.data.DmpEntity;
import eu.old.eudat.data.entities.DMP;
import eu.old.eudat.data.entities.Organisation;
import eu.old.eudat.logic.services.operations.DatabaseRepository;
import eu.old.eudat.migration.DmpMigrationService;
import eu.old.eudat.migration.OrganizationMigrationService;
import eu.old.eudat.models.data.dmp.DataManagementPlan;
import eu.old.eudat.queryable.QueryableList;
import io.swagger.annotations.Api;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Api(tags = "Migration")
@RestController
@CrossOrigin
@RequestMapping(value = {"/api/public/migration"})
public class MigrationController {
private final DatabaseRepository databaseRepository;
private final JsonHandlingService jsonHandlingService;
@PersistenceContext
private final EntityManager entityManager;
private final DmpMigrationService dmpMigrationService;
private final OrganizationMigrationService organizationMigrationService;
public MigrationController(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, EntityManager entityManager, OrganizationMigrationService organizationMigrationService) {
this.databaseRepository = databaseRepository;
this.jsonHandlingService = jsonHandlingService;
this.entityManager = entityManager;
public MigrationController(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, EntityManager entityManager, DmpMigrationService dmpMigrationService, OrganizationMigrationService organizationMigrationService) {
this.dmpMigrationService = dmpMigrationService;
this.organizationMigrationService = organizationMigrationService;
}
public QueryableList<DMP> dmpQueryableList() {
return databaseRepository.getDmpDao().asQueryable();
}
public QueryableList<Organisation> organizationQueryableList() {
return databaseRepository.getOrganisationDao().asQueryable();
}
@GetMapping("dmps/all/count")
public Long count() {
return dmpQueryableList().count();
}
@GetMapping("dmps/all/{skip}/{take}")
public List<DataManagementPlan> getDmps(@PathVariable("skip") Integer skip, @PathVariable("take") Integer take) {
List<DMP> dmps = dmpQueryableList()
.orderBy((builder, root) -> builder.desc(root.get("created")))
.skip(skip)
.take(take)
.toList();
return dmps.stream().map(x -> {
DataManagementPlan dmp = new DataManagementPlan();
dmp.fromDataModel(x);
return dmp;
}).toList();
}
@GetMapping("dmps/collectInfo")
public List<DmpMigrationCollectedInfo> collectDmpsInfo() throws JsonProcessingException {
List<DmpMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
List<DataManagementPlan> dmps = getDmps(0, 4);
for (DataManagementPlan dataManagementPlan : dmps) {
DmpMigrationCollectedInfo dmpMigrationCollectedInfo = new DmpMigrationCollectedInfo();
DmpPropertiesEntity dmpProperties = new DmpPropertiesEntity();
dmpProperties.setDmpBlueprintValues(new ArrayList<>());
dmpProperties.setContacts(new ArrayList<>());
//Collect basic dmp information
DmpEntity dmpEntity = new DmpEntity();
dmpEntity.setId(UUID.randomUUID());
dmpEntity.setLabel(dataManagementPlan.getLabel());
dmpEntity.setDescription(dataManagementPlan.getDescription());
if (dataManagementPlan.getCreator() != null)
dmpEntity.setCreatorId(dataManagementPlan.getCreator().getId());
dmpEntity.setGroupId(dataManagementPlan.getGroupId());
dmpEntity.setVersion((short) dataManagementPlan.getVersion());
dmpEntity.setVersionStatus(DmpVersionStatus.of((short) dataManagementPlan.getVersionStatus()));
if (dataManagementPlan.getCreated() != null)
dmpEntity.setCreatedAt(dataManagementPlan.getCreated().toInstant());
if (dataManagementPlan.getModified() != null)
dmpEntity.setUpdatedAt(dataManagementPlan.getModified().toInstant());
if (dataManagementPlan.getFinalized() != null)
dmpEntity.setFinalizedAt(dataManagementPlan.getFinalized().toInstant());
dmpEntity.setBlueprintId(dataManagementPlan.getProfile().getId());
if (dataManagementPlan.getExtraProperties() != null) {
if (dataManagementPlan.getExtraProperties().containsKey("language") && dataManagementPlan.getExtraProperties().get("language") != null)
dmpEntity.setLanguage((String) dataManagementPlan.getExtraProperties().get("language"));
if (dataManagementPlan.getExtraProperties().containsKey("visible") && dataManagementPlan.getExtraProperties().get("visible") != null)
dmpEntity.setAccessType((boolean) dataManagementPlan.getExtraProperties().get("visible") ? DmpAccessType.Public : DmpAccessType.Restricted);
if (dataManagementPlan.getExtraProperties().containsKey("contact") && dataManagementPlan.getExtraProperties().get("contact") != null) {
DmpContactEntity contactEntity = new DmpContactEntity();
contactEntity.setUserId((String) dataManagementPlan.getExtraProperties().get("contact"));
dmpProperties.getContacts().add(contactEntity);
}
}
if (dataManagementPlan.getProperties() != null) {
dataManagementPlan.getProperties().forEach((key,val) -> {
DmpBlueprintValueEntity valueEntity = new DmpBlueprintValueEntity();
valueEntity.setFieldId(key);
valueEntity.setValue((String) val);
dmpProperties.getDmpBlueprintValues().add(valueEntity);
});
}
if (dataManagementPlan.getStatus() == 99) {
dmpEntity.setIsActive(IsActive.Inactive);
} else {
dmpEntity.setIsActive(IsActive.Active);
dmpEntity.setStatus(DmpStatus.of((short) dataManagementPlan.getStatus()));
}
dmpEntity.setProperties(jsonHandlingService.toJson(dmpProperties));
dmpMigrationCollectedInfo.dmpEntity = dmpEntity;
//Collect dmp Organization info
collectedInfoList.add(dmpMigrationCollectedInfo);
}
return collectedInfoList;
}
@GetMapping("dmps/migrate")
@Transactional
public String migrate() throws JsonProcessingException {
for (DmpMigrationCollectedInfo collectedInfo : collectDmpsInfo()) {
this.entityManager.persist(collectedInfo.dmpEntity);
}
this.entityManager.flush();
return "Migrated dmps";
return this.dmpMigrationService.migrate();
}
@GetMapping("organizations/migrate")
@ -153,10 +40,4 @@ public class MigrationController {
return true;
}
public static class DmpMigrationCollectedInfo {
public DmpEntity dmpEntity;
}
}