From 02a42d22bad8430f53fd8689fe03115c94386e65 Mon Sep 17 00:00:00 2001 From: dtziotzios Date: Tue, 26 Mar 2019 16:52:19 +0200 Subject: [PATCH] Fixes Dmp Versioning functionality letting user editing and saving over an old version. --- .../main/java/eu/eudat/controllers/DMPs.java | 9 ++- .../DMPNewVersionException.java | 8 +++ .../managers/DataManagementPlanManager.java | 57 ++++++++++++++----- .../app/ui/dmp/wizard/dmp-wizard.component.ts | 7 ++- 4 files changed, 64 insertions(+), 17 deletions(-) create mode 100644 dmp-backend/web/src/main/java/eu/eudat/exceptions/datamanagementplan/DMPNewVersionException.java 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 6bf522700..d3841eff2 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 @@ -8,6 +8,7 @@ import eu.eudat.data.dao.entities.DMPDao; import eu.eudat.data.entities.DMP; import eu.eudat.data.query.items.item.dmp.DataManagementPlanCriteriaRequest; import eu.eudat.data.query.items.table.dmp.DataManagementPlanTableRequest; +import eu.eudat.exceptions.datamanagementplan.DMPNewVersionException; import eu.eudat.exceptions.datamanagementplan.DMPWithDatasetsDeleteException; import eu.eudat.logic.managers.DataManagementPlanManager; import eu.eudat.logic.managers.DatasetManager; @@ -105,8 +106,12 @@ public class DMPs extends BaseController { @RequestMapping(method = RequestMethod.POST, value = {"/new/{id}"}, consumes = "application/json", produces = "application/json") public @ResponseBody ResponseEntity> newVersion(@PathVariable UUID id, @Valid @RequestBody eu.eudat.models.data.dmp.DataManagementPlanNewVersionModel dataManagementPlan, Principal principal) throws Exception { - this.dataManagementPlanManager.newVersion(id, dataManagementPlan, principal); - return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE)); + try { + this.dataManagementPlanManager.newVersion(id, dataManagementPlan, principal); + return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem().status(ApiMessageCode.NO_MESSAGE)); + } catch (DMPNewVersionException exception) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem().status(ApiMessageCode.ERROR_MESSAGE).message(exception.getMessage())); + } } @RequestMapping(method = RequestMethod.POST, value = {"/clone/{id}"}, consumes = "application/json", produces = "application/json") diff --git a/dmp-backend/web/src/main/java/eu/eudat/exceptions/datamanagementplan/DMPNewVersionException.java b/dmp-backend/web/src/main/java/eu/eudat/exceptions/datamanagementplan/DMPNewVersionException.java new file mode 100644 index 000000000..8c789c41d --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/exceptions/datamanagementplan/DMPNewVersionException.java @@ -0,0 +1,8 @@ +package eu.eudat.exceptions.datamanagementplan; + +public class DMPNewVersionException extends RuntimeException { + + public DMPNewVersionException(String message) { + super(message); + } +} 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 e70689d92..52671a89c 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 @@ -9,6 +9,7 @@ import eu.eudat.data.entities.Organisation; import eu.eudat.data.entities.Researcher; import eu.eudat.data.query.items.item.dmp.DataManagementPlanCriteriaRequest; import eu.eudat.data.query.items.table.dmp.DataManagementPlanTableRequest; +import eu.eudat.exceptions.datamanagementplan.DMPNewVersionException; import eu.eudat.exceptions.datamanagementplan.DMPWithDatasetsDeleteException; import eu.eudat.exceptions.security.UnauthorisedException; import eu.eudat.logic.builders.entity.UserInfoBuilder; @@ -25,6 +26,8 @@ import eu.eudat.models.HintedModelFactory; import eu.eudat.models.data.datasetwizard.DatasetWizardModel; import eu.eudat.models.data.dmp.*; import eu.eudat.models.data.dynamicfields.DynamicFieldWithValue; +import eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.DataManagementPlanProfile; +import eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.Field; import eu.eudat.models.data.helpermodels.Tuple; import eu.eudat.models.data.helpers.common.DataTableData; import eu.eudat.models.data.listingmodels.DataManagementPlanListingModel; @@ -283,17 +286,30 @@ public class DataManagementPlanManager { public void newVersion(UUID uuid, DataManagementPlanNewVersionModel dataManagementPlan, Principal principal) throws Exception { DMP oldDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(uuid); - DMP newDmp = dataManagementPlan.toDataModel(); - createOrganisationsIfTheyDontExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getOrganisationDao()); - createResearchersIfTheyDontExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getResearcherDao()); - UserInfo user = apiContext.getOperationsContext().getBuilderFactory().getBuilder(UserInfoBuilder.class).id(principal.getId()).build(); - createProjectIfItDoesntExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getProjectDao(), user); - newDmp.setCreator(user); - newDmp.setGroupId(oldDmp.getGroupId()); - newDmp.setVersion(oldDmp.getVersion() + 1); - newDmp.setId(null); - newDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(newDmp); - copyDatasets(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao()); + + DataManagementPlanCriteria criteria = new DataManagementPlanCriteria(); + LinkedList list = new LinkedList<>(); + list.push(oldDmp.getGroupId()); + criteria.setGroupIds(list); + criteria.setAllVersions(false); + QueryableList dataManagementPlanQueryableList = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().getWithCriteria(criteria); + List latestVersionDMP = dataManagementPlanQueryableList.toList(); + + if(latestVersionDMP.get(0).getVersion().equals(oldDmp.getVersion())){ + DMP newDmp = dataManagementPlan.toDataModel(); + createOrganisationsIfTheyDontExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getOrganisationDao()); + createResearchersIfTheyDontExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getResearcherDao()); + UserInfo user = apiContext.getOperationsContext().getBuilderFactory().getBuilder(UserInfoBuilder.class).id(principal.getId()).build(); + createProjectIfItDoesntExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getProjectDao(), user); + newDmp.setCreator(user); + newDmp.setGroupId(oldDmp.getGroupId()); + newDmp.setVersion(oldDmp.getVersion() + 1); + newDmp.setId(null); + newDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(newDmp); + copyDatasets(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao()); + } else { + throw new DMPNewVersionException("Version to update not the latest."); + } } public void clone(UUID uuid, DataManagementPlanNewVersionModel dataManagementPlan, Principal principal) throws Exception { @@ -446,6 +462,9 @@ public class DataManagementPlanManager { Element dmpProfileId = xmlDoc.createElement("dmpProfileId"); dmpProfileId.setTextContent(dmpProfile.getId().toString()); dmpProfileElement.appendChild(dmpProfileId); + Element values = xmlDoc.createElement("values"); + values.setTextContent(dmpProfile.getDefinition()); + dmpProfileElement.appendChild(values); } dmpElement.appendChild(dmpProfileElement); @@ -589,9 +608,19 @@ public class DataManagementPlanManager { // Creates new dataManagmentPlan to fill it with the data model that was parsed from the xml. // Creates properties. DataManagementPlan dm = new DataManagementPlan(); - Tuple tuple = new Tuple(); + DataManagementPlanProfile dmpProfile = new DataManagementPlanProfile(); + + List fieldList = new LinkedList<>(); + Field field = new Field(); + field.setLabel(dataManagementPlans.get(0).getDmpProfile().getDmpProfileName()); + field.setId(dataManagementPlans.get(0).getDmpProfile().getDmpProfileId()); + + fieldList.add(field); + dmpProfile.setFields(fieldList); + + /*Tuple tuple = new Tuple(); tuple.setId(dataManagementPlans.get(0).getDmpProfile().getDmpProfileId()); - tuple.setLabel(dataManagementPlans.get(0).getDmpProfile().getDmpProfileName()); + tuple.setLabel(dataManagementPlans.get(0).getDmpProfile().getDmpProfileName());*/ eu.eudat.models.data.project.Project project = new eu.eudat.models.data.project.Project(); ProjectImportModels projectImport = dataManagementPlans.get(0).getProjectImport(); project.setId(projectImport.getId()); @@ -632,7 +661,7 @@ public class DataManagementPlanManager { dm.setResearchers(researchers); // Sets researchers property. dm.setAssociatedUsers(associatedUsers); // Sets associatedUsers property. dm.setDynamicFields(dynamicFields); // Sets dynamicFields property. - dm.setProfile(tuple); + dm.setDefinition(dmpProfile); createOrUpdate(apiContext, dm, principal); diff --git a/dmp-frontend/src/app/ui/dmp/wizard/dmp-wizard.component.ts b/dmp-frontend/src/app/ui/dmp/wizard/dmp-wizard.component.ts index f57e65a3c..bccef459c 100644 --- a/dmp-frontend/src/app/ui/dmp/wizard/dmp-wizard.component.ts +++ b/dmp-frontend/src/app/ui/dmp/wizard/dmp-wizard.component.ts @@ -13,6 +13,7 @@ import { BreadcrumbItem } from '../../misc/breadcrumb/definition/breadcrumb-item import { IBreadCrumbComponent } from '../../misc/breadcrumb/definition/IBreadCrumbComponent'; import { DmpWizardEditorModel } from './dmp-wizard-editor.model'; import { SnackBarNotificationLevel, UiNotificationService } from '../../../core/services/notification/ui-notification-service'; +import { HttpErrorResponse } from '@angular/common/http'; @Component({ selector: 'app-dmp-wizard-component', @@ -75,7 +76,7 @@ export class DmpWizardComponent extends BaseComponent implements OnInit, IBreadC .pipe(takeUntil(this._destroyed)) .subscribe( complete => this.onCallbackSuccess(), - error => this.onCallbackError(error) + error => this.onCallbackErrorNewVersion(error) ); } } @@ -96,5 +97,9 @@ export class DmpWizardComponent extends BaseComponent implements OnInit, IBreadC }); } + onCallbackErrorNewVersion(errorResponse: HttpErrorResponse) { + this.uiNotificationService.snackBarNotification(errorResponse.error.message, SnackBarNotificationLevel.Error); + } + }