argos/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DmpMigrationService.java

139 lines
6.4 KiB
Java

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;
}
}