From 6596b3f9533cf3a31655acfa932e9177ebe03436 Mon Sep 17 00:00:00 2001 From: gkolokythas Date: Tue, 2 Jul 2019 12:36:25 +0300 Subject: [PATCH] Refactors DMP create or update and adds the dmp id on payload response. --- .../main/java/eu/eudat/controllers/DMPs.java | 6 +- .../managers/DataManagementPlanManager.java | 110 +++++++----------- 2 files changed, 45 insertions(+), 71 deletions(-) diff --git a/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPs.java b/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPs.java index 22cb1c489..fe1454ecf 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPs.java +++ b/dmp-backend/web/src/main/java/eu/eudat/controllers/DMPs.java @@ -142,9 +142,9 @@ public class DMPs extends BaseController { @Transactional @RequestMapping(method = RequestMethod.POST, consumes = "application/json", produces = "application/json") public @ResponseBody - ResponseEntity> 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().status(ApiMessageCode.SUCCESS_MESSAGE).message("Created")); + ResponseEntity> 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().status(ApiMessageCode.SUCCESS_MESSAGE).message("Created").payload(dmp.getId())); } @RequestMapping(method = RequestMethod.POST, value = {"/new/{id}"}, consumes = "application/json", produces = "application/json") diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java index 4011a9206..d76ad4cd1 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/DataManagementPlanManager.java @@ -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 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 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.get("status"), Dataset.Status.FINALISED.getValue()); + } + newDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(newDmp); - List 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.get("status"), Dataset.Status.FINALISED.getValue()); + + List 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.get("status"), Dataset.Status.CANCELED.getValue()); - } else { - List 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.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.get("status"), Dataset.Status.FINALISED.getValue()); - - List 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.get("status"), Dataset.Status.CANCELED.getValue()); + } else { + List 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.get("status"), Dataset.Status.CANCELED.getValue()); - } else { - List 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.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.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);