129 lines
5.0 KiB
Java
129 lines
5.0 KiB
Java
package eu.old.eudat.publicapi.migration;
|
|
|
|
import eu.eudat.commons.enums.*;
|
|
import eu.eudat.data.DmpEntity;
|
|
import eu.eudat.data.ReferenceEntity;
|
|
import eu.old.eudat.data.dao.criteria.DataManagementPlanCriteria;
|
|
import eu.old.eudat.data.dao.criteria.OrganisationCriteria;
|
|
import eu.old.eudat.data.dao.entities.DMPDao;
|
|
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.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;
|
|
|
|
@PersistenceContext
|
|
private final EntityManager entityManager;
|
|
|
|
public MigrationController(DatabaseRepository databaseRepository, EntityManager entityManager) {
|
|
this.databaseRepository = databaseRepository;
|
|
this.entityManager = entityManager;
|
|
}
|
|
|
|
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() {
|
|
List<DmpMigrationCollectedInfo> collectedInfoList = new ArrayList<>();
|
|
List<DataManagementPlan> dmps = getDmps(0, 4);
|
|
for (DataManagementPlan dataManagementPlan : dmps) {
|
|
DmpMigrationCollectedInfo dmpMigrationCollectedInfo = new DmpMigrationCollectedInfo();
|
|
|
|
//Collect basic dmp information
|
|
DmpEntity dmpEntity = new DmpEntity();
|
|
dmpEntity.setId(UUID.randomUUID());
|
|
dmpEntity.setLabel(dataManagementPlan.getLabel());
|
|
dmpEntity.setDescription(dataManagementPlan.getDescription());
|
|
dmpEntity.setAccessType(dataManagementPlan.getPublic() ? DmpAccessType.Public : DmpAccessType.Restricted);
|
|
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().containsKey("language") && dataManagementPlan.getExtraProperties().get("language") != null)
|
|
dmpEntity.setLanguage((String) dataManagementPlan.getExtraProperties().get("language"));
|
|
if (dataManagementPlan.getStatus() == 99) {
|
|
dmpEntity.setIsActive(IsActive.Inactive);
|
|
} else {
|
|
dmpEntity.setIsActive(IsActive.Active);
|
|
dmpEntity.setStatus(DmpStatus.of((short) dataManagementPlan.getStatus()));
|
|
}
|
|
dmpMigrationCollectedInfo.dmpEntity = dmpEntity;
|
|
|
|
//Collect dmp Organization info
|
|
|
|
|
|
|
|
collectedInfoList.add(dmpMigrationCollectedInfo);
|
|
}
|
|
return collectedInfoList;
|
|
}
|
|
|
|
@GetMapping("dmps/migrate")
|
|
@Transactional
|
|
public String migrate() {
|
|
for (DmpMigrationCollectedInfo collectedInfo : collectDmpsInfo()) {
|
|
this.entityManager.persist(collectedInfo.dmpEntity);
|
|
}
|
|
this.entityManager.flush();
|
|
return "Migrated dmps";
|
|
}
|
|
|
|
public static class DmpMigrationCollectedInfo {
|
|
|
|
public DmpEntity dmpEntity;
|
|
|
|
}
|
|
|
|
}
|