Refactors DMP create or update and adds the dmp id on payload response.

This commit is contained in:
gkolokythas 2019-07-02 12:36:25 +03:00
parent d9429a0de6
commit 6596b3f953
2 changed files with 45 additions and 71 deletions

View File

@ -142,9 +142,9 @@ public class DMPs extends BaseController {
@Transactional
@RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<DMP>> createOrUpdate(@RequestBody eu.eudat.models.data.dmp.DataManagementPlanEditorModel dataManagementPlanEditorModel, Principal principal) throws Exception {
this.dataManagementPlanManager.createOrUpdate(this.getApiContext(), dataManagementPlanEditorModel, principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DMP>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Created"));
ResponseEntity<ResponseItem<UUID>> createOrUpdate(@RequestBody eu.eudat.models.data.dmp.DataManagementPlanEditorModel dataManagementPlanEditorModel, Principal principal) throws Exception {
DMP dmp = this.dataManagementPlanManager.createOrUpdate(this.getApiContext(), dataManagementPlanEditorModel, principal);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<UUID>().status(ApiMessageCode.SUCCESS_MESSAGE).message("Created").payload(dmp.getId()));
}
@RequestMapping(method = RequestMethod.POST, value = {"/new/{id}"}, consumes = "application/json", produces = "application/json")

View File

@ -396,7 +396,7 @@ public class DataManagementPlanManager {
return result;
}
public void createOrUpdate(ApiContext apiContext, DataManagementPlanEditorModel dataManagementPlan, Principal principal) throws Exception {
public DMP createOrUpdate(ApiContext apiContext, DataManagementPlanEditorModel dataManagementPlan, Principal principal) throws Exception {
if (dataManagementPlan.getStatus() == (int) DMP.DMPStatus.FINALISED.getValue() && dataManagementPlan.getId() != null) {
DMP dmp1 = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(dataManagementPlan.getId());
@ -411,89 +411,63 @@ public class DataManagementPlanManager {
createResearchersIfTheyDontExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getResearcherDao());
createProjectIfItDoesntExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getProjectDao(), user);
try {
DMP dmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(dataManagementPlan.getId());
newDmp.setCreated(dmp.getCreated());
if (dmp.getUsers().stream().filter(userInfo -> userInfo.getUser().getId() == principal.getId())
.collect(Collectors.toList()).size() == 0) {
List<UserDMP> userDMPList = dmp.getUsers().stream().collect(Collectors.toList());
for (UserInfoListingModel userInfoListingModel : dataManagementPlan.getUsers()) {
for (UserDMP userDMP : userDMPList) {
if (!(userDMP.getUser().getId().equals(userInfoListingModel.getId()))) {
apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().delete(userDMP);
}
DMP dmp;
if (dataManagementPlan.getId() != null) {
dmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(dataManagementPlan.getId());
} else dmp = new DMP();
newDmp.setCreated(dmp.getCreated() == null ? new Date() : dmp.getCreated());
if (newDmp.getUsers()!= null && newDmp.getUsers().stream().filter(userInfo -> userInfo.getUser().getId() == principal.getId())
.collect(Collectors.toList()).size() == 0) {
List<UserDMP> userDMPList = newDmp.getUsers().stream().collect(Collectors.toList());
for (UserInfoListingModel userInfoListingModel : dataManagementPlan.getUsers()) {
for (UserDMP userDMP : userDMPList) {
if (!(userDMP.getUser().getId().equals(userInfoListingModel.getId()))) {
apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().delete(userDMP);
}
}
}
newDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(newDmp);
if (dataManagementPlan.getStatus() == DMP.DMPStatus.FINALISED.getValue()) {
if (dataManagementPlan.getDatasetsToBeFinalized() != null && !dataManagementPlan.getDatasetsToBeFinalized().isEmpty()) {
apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao()
.asQueryable().where((builder, root) -> root.get("id").in(dataManagementPlan.getDatasetsToBeFinalized()))
.update(root -> root.<Integer>get("status"), Dataset.Status.FINALISED.getValue());
}
newDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(newDmp);
List<UUID> datasetsToBeCanceled = new LinkedList<>();
for (Dataset dataset : dmp.getDataset()) {
if (!dataset.getStatus().equals(Dataset.Status.FINALISED.getValue()) && !dataManagementPlan.getDatasetsToBeFinalized().contains(dataset.getId())) {
datasetsToBeCanceled.add(dataset.getId());
}
// Dataset manipulation for when the DMP is set to be finalized.
if (dataManagementPlan.getStatus() == DMP.DMPStatus.FINALISED.getValue()) {
if (dataManagementPlan.getDatasetsToBeFinalized() != null && !dataManagementPlan.getDatasetsToBeFinalized().isEmpty()) {
apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao()
.asQueryable().where((builder, root) -> root.get("id").in(dataManagementPlan.getDatasetsToBeFinalized()))
.update(root -> root.<Integer>get("status"), Dataset.Status.FINALISED.getValue());
List<UUID> datasetsToBeCanceled = new LinkedList<>();
for (DatasetListingModel dataset : dataManagementPlan.getDatasets()) {
if (!(dataset.getStatus() == (int) Dataset.Status.FINALISED.getValue()) && !dataManagementPlan.getDatasetsToBeFinalized().contains(UUID.fromString(dataset.getId()))) {
datasetsToBeCanceled.add(UUID.fromString(dataset.getId()));
}
if (!datasetsToBeCanceled.isEmpty())
apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao()
.asQueryable().where((builder, root) -> root.get("id").in(datasetsToBeCanceled))
.update(root -> root.<Integer>get("status"), Dataset.Status.CANCELED.getValue());
} else {
List<UUID> datasetsToBeCanceled = new LinkedList<>();
for (Dataset dataset : dmp.getDataset()) {
if (!dataset.getStatus().equals(Dataset.Status.FINALISED.getValue())) {
datasetsToBeCanceled.add(dataset.getId());
}
}
if (!datasetsToBeCanceled.isEmpty())
apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao()
.asQueryable().where((builder, root) -> root.get("id").in(datasetsToBeCanceled))
.update(root -> root.<Integer>get("status"), Dataset.Status.CANCELED.getValue());
}
}
} catch (Exception e) {
newDmp.setCreated(new Date());
newDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(newDmp);
if (dataManagementPlan.getStatus() == DMP.DMPStatus.FINALISED.getValue()) {
if (dataManagementPlan.getDatasetsToBeFinalized() != null && !dataManagementPlan.getDatasetsToBeFinalized().isEmpty()) {
if (!datasetsToBeCanceled.isEmpty())
apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao()
.asQueryable().where((builder, root) -> root.get("id").in(dataManagementPlan.getDatasetsToBeFinalized()))
.update(root -> root.<Integer>get("status"), Dataset.Status.FINALISED.getValue());
List<UUID> datasetsToBeCanceled = new LinkedList<>();
for (DatasetListingModel dataset : dataManagementPlan.getDatasets()) {
if (!(dataset.getStatus() == (int) Dataset.Status.FINALISED.getValue()) && !dataManagementPlan.getDatasetsToBeFinalized().contains(UUID.fromString(dataset.getId()))) {
datasetsToBeCanceled.add(UUID.fromString(dataset.getId()));
}
.asQueryable().where((builder, root) -> root.get("id").in(datasetsToBeCanceled))
.update(root -> root.<Integer>get("status"), Dataset.Status.CANCELED.getValue());
} else {
List<UUID> datasetsToBeCanceled = new LinkedList<>();
for (DatasetListingModel dataset : dataManagementPlan.getDatasets()) {
if (!(dataset.getStatus() == (int) Dataset.Status.FINALISED.getValue())) {
datasetsToBeCanceled.add(UUID.fromString(dataset.getId()));
}
if (!datasetsToBeCanceled.isEmpty())
apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao()
.asQueryable().where((builder, root) -> root.get("id").in(datasetsToBeCanceled))
.update(root -> root.<Integer>get("status"), Dataset.Status.CANCELED.getValue());
} else {
List<UUID> datasetsToBeCanceled = new LinkedList<>();
for (DatasetListingModel dataset : dataManagementPlan.getDatasets()) {
if (!(dataset.getStatus() == (int) Dataset.Status.FINALISED.getValue())) {
datasetsToBeCanceled.add(UUID.fromString(dataset.getId()));
}
}
if (!datasetsToBeCanceled.isEmpty())
apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao()
.asQueryable().where((builder, root) -> root.get("id").in(datasetsToBeCanceled))
.update(root -> root.<Integer>get("status"), Dataset.Status.CANCELED.getValue());
}
if (!datasetsToBeCanceled.isEmpty())
apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao()
.asQueryable().where((builder, root) -> root.get("id").in(datasetsToBeCanceled))
.update(root -> root.<Integer>get("status"), Dataset.Status.CANCELED.getValue());
}
}
if (dataManagementPlan.getAssociatedUsers().size() == 0)
assignUser(newDmp, user, apiContext);
return newDmp;
}
public void assignUser(DMP dmp, UserInfo userInfo, ApiContext apiContext) {
UserDMP userDMP = new UserDMP();
userDMP.setDmp(dmp);