2023-11-22 09:24:30 +01:00
|
|
|
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;
|
2023-11-23 13:40:44 +01:00
|
|
|
import eu.old.eudat.data.dao.entities.DMPDao;
|
2023-11-22 09:24:30 +01:00
|
|
|
import eu.old.eudat.data.entities.DMP;
|
|
|
|
import eu.old.eudat.logic.services.operations.DatabaseRepository;
|
|
|
|
import eu.old.eudat.models.data.dmp.DataManagementPlan;
|
2023-11-23 17:09:17 +01:00
|
|
|
import gr.cite.tools.data.query.QueryFactory;
|
2023-11-23 13:40:44 +01:00
|
|
|
import gr.cite.tools.exception.MyApplicationException;
|
|
|
|
import gr.cite.tools.logging.LoggerService;
|
2023-11-22 09:24:30 +01:00
|
|
|
import jakarta.persistence.EntityManager;
|
2023-11-23 13:40:44 +01:00
|
|
|
import org.slf4j.LoggerFactory;
|
2023-11-22 09:24:30 +01:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
2023-11-23 13:40:44 +01:00
|
|
|
import java.util.*;
|
2023-11-22 09:24:30 +01:00
|
|
|
|
|
|
|
@Service
|
|
|
|
public class DmpMigrationService {
|
|
|
|
|
2023-11-23 13:40:44 +01:00
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(DmpMigrationService.class));
|
2023-11-22 09:24:30 +01:00
|
|
|
private final DatabaseRepository databaseRepository;
|
|
|
|
|
|
|
|
private final JsonHandlingService jsonHandlingService;
|
|
|
|
private final EntityManager entityManager;
|
2023-11-23 13:40:44 +01:00
|
|
|
private static final int PageSize = 500;
|
|
|
|
private static final boolean TestMode = false;
|
2023-11-22 09:24:30 +01:00
|
|
|
|
|
|
|
public DmpMigrationService(DatabaseRepository databaseRepository, JsonHandlingService jsonHandlingService, EntityManager entityManager) {
|
|
|
|
this.databaseRepository = databaseRepository;
|
|
|
|
this.jsonHandlingService = jsonHandlingService;
|
|
|
|
this.entityManager = entityManager;
|
|
|
|
}
|
|
|
|
|
2023-11-23 13:40:44 +01:00
|
|
|
public void migrate() throws JsonProcessingException {
|
|
|
|
DMPDao dmpDao = databaseRepository.getDmpDao();
|
|
|
|
long total = dmpDao.asQueryable().count();
|
|
|
|
logger.debug("Migrate Dmp Total : " + total);
|
|
|
|
int page = 0;
|
|
|
|
|
|
|
|
List<DMP> items;
|
|
|
|
do {
|
|
|
|
items = dmpDao.asQueryable().orderBy((builder, root) -> builder.asc(root.get("created"))).orderBy((builder, root) -> builder.asc(root.get("ID"))).skip(page * PageSize).take(PageSize).toList();
|
|
|
|
if (items != null && !items.isEmpty()) {
|
|
|
|
logger.debug("Migrate Dmp " + page * PageSize + " of " + total);
|
|
|
|
|
|
|
|
for (DMP item : items) {
|
|
|
|
//entityManager.detach(item);
|
|
|
|
|
|
|
|
DataManagementPlan model = new DataManagementPlan();
|
|
|
|
model.fromDataModel(item);
|
|
|
|
|
|
|
|
DmpPropertiesEntity dmpProperties = new DmpPropertiesEntity();
|
|
|
|
dmpProperties.setDmpBlueprintValues(new ArrayList<>());
|
|
|
|
dmpProperties.setContacts(new ArrayList<>());
|
|
|
|
|
|
|
|
DmpEntity data = new DmpEntity();
|
|
|
|
data.setId(model.getId());
|
|
|
|
data.setLabel(model.getLabel());
|
|
|
|
data.setDescription(model.getDescription());
|
2023-11-23 17:09:17 +01:00
|
|
|
if (model.getCreator() != null) {
|
2023-11-23 13:40:44 +01:00
|
|
|
data.setCreatorId(model.getCreator().getId());
|
2023-11-23 17:09:17 +01:00
|
|
|
} else {
|
|
|
|
List<DMP> groupDmps = dmpDao.asQueryable().where((builder, root) -> root.get("GroupId").in(List.of(item.getGroupId()))).toList();
|
|
|
|
for (DMP groupDmp: groupDmps) {
|
|
|
|
DataManagementPlan groupDmpModel = new DataManagementPlan();
|
|
|
|
groupDmpModel.fromDataModel(groupDmp);
|
|
|
|
if (groupDmpModel.getCreator() != null) {
|
|
|
|
data.setCreatorId(groupDmpModel.getCreator().getId());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-23 13:40:44 +01:00
|
|
|
data.setGroupId(model.getGroupId());
|
|
|
|
data.setVersion((short) model.getVersion());
|
|
|
|
data.setVersionStatus(DmpVersionStatus.of((short) model.getVersionStatus()));
|
|
|
|
if (model.getCreated() != null)
|
|
|
|
data.setCreatedAt(model.getCreated().toInstant());
|
|
|
|
if (model.getModified() != null)
|
|
|
|
data.setUpdatedAt(model.getModified().toInstant());
|
|
|
|
if (model.getFinalized() != null)
|
|
|
|
data.setFinalizedAt(model.getFinalized().toInstant());
|
|
|
|
data.setBlueprintId(model.getProfile().getId());
|
|
|
|
if (model.getExtraProperties() != null) {
|
|
|
|
if (model.getExtraProperties().containsKey("language") && model.getExtraProperties().get("language") != null)
|
|
|
|
data.setLanguage((String) model.getExtraProperties().get("language"));
|
|
|
|
if (model.getExtraProperties().containsKey("visible") && model.getExtraProperties().get("visible") != null)
|
|
|
|
data.setAccessType((boolean) model.getExtraProperties().get("visible") ? DmpAccessType.Public : DmpAccessType.Restricted);
|
|
|
|
if (model.getExtraProperties().containsKey("contact") && model.getExtraProperties().get("contact") != null) {
|
|
|
|
DmpContactEntity contactEntity = new DmpContactEntity();
|
|
|
|
contactEntity.setUserId((String) model.getExtraProperties().get("contact"));
|
|
|
|
dmpProperties.getContacts().add(contactEntity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (model.getProperties() != null) {
|
|
|
|
model.getProperties().forEach((key,val) -> {
|
|
|
|
DmpBlueprintValueEntity valueEntity = new DmpBlueprintValueEntity();
|
|
|
|
valueEntity.setFieldId(key);
|
|
|
|
valueEntity.setValue((String) val);
|
|
|
|
dmpProperties.getDmpBlueprintValues().add(valueEntity);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (model.getStatus() == 99) {
|
|
|
|
data.setIsActive(IsActive.Inactive);
|
|
|
|
data.setStatus(DmpStatus.Draft);
|
|
|
|
} else {
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
data.setStatus(DmpStatus.of((short) model.getStatus()));
|
|
|
|
}
|
|
|
|
data.setProperties(jsonHandlingService.toJson(dmpProperties));
|
|
|
|
|
2023-11-23 17:09:17 +01:00
|
|
|
if (data.getAccessType() == null && item.getStatus().equals(DmpStatus.Finalized.getValue())) {
|
2023-11-23 13:40:44 +01:00
|
|
|
logger.warn("AccessType not found set to default for dmp " + item.getId());
|
2023-11-23 17:09:17 +01:00
|
|
|
data.setAccessType(item.isPublic() ? DmpAccessType.Public : DmpAccessType.Restricted);
|
2023-11-23 13:40:44 +01:00
|
|
|
}
|
2023-11-23 17:09:17 +01:00
|
|
|
if ((data.getLanguage() == null || data.getLanguage().isBlank()) && item.getStatus().equals(DmpStatus.Finalized.getValue())) {
|
2023-11-23 13:40:44 +01:00
|
|
|
logger.warn("Language not found set to default for dmp " + item.getId());
|
2023-11-23 17:09:17 +01:00
|
|
|
data.setLanguage("en");
|
2023-11-23 13:40:44 +01:00
|
|
|
}
|
|
|
|
if (data.getCreatorId() == null){
|
|
|
|
logger.warn("Migration skipped creator not found " + item.getId());
|
2023-11-23 17:09:17 +01:00
|
|
|
throw new MyApplicationException("Migration skipped creator not found " + item.getId());
|
2023-11-23 13:40:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
this.entityManager.persist(data);
|
|
|
|
this.entityManager.flush();
|
2023-11-22 09:24:30 +01:00
|
|
|
}
|
|
|
|
|
2023-11-23 13:40:44 +01:00
|
|
|
page++;
|
|
|
|
}
|
|
|
|
} while (items != null && !items.isEmpty() && !TestMode);
|
2023-11-22 09:24:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|