Refactors DMP "clone" and "new version" functionality to support DMP's new entities "Funder" and "Project".
This commit is contained in:
parent
2d36450e55
commit
8ebe2961b7
|
@ -161,8 +161,8 @@ public class DMPs extends BaseController {
|
|||
@RequestMapping(method = RequestMethod.POST, value = {"/clone/{id}"}, consumes = "application/json", produces = "application/json")
|
||||
public @ResponseBody
|
||||
ResponseEntity<ResponseItem<DMP>> clone(@PathVariable UUID id, @RequestBody eu.eudat.models.data.dmp.DataManagementPlanNewVersionModel dataManagementPlan, Principal principal) throws Exception {
|
||||
this.dataManagementPlanManager.clone(id, dataManagementPlan, principal);
|
||||
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DMP>().status(ApiMessageCode.NO_MESSAGE));
|
||||
this.dataManagementPlanManager.clone(dataManagementPlan, principal);
|
||||
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<DMP>().status(ApiMessageCode.SUCCESS_MESSAGE));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
|
|
@ -455,18 +455,9 @@ public class DataManagementPlanManager {
|
|||
}
|
||||
}
|
||||
|
||||
if (newDmp.getGrant().getId() != null) {
|
||||
Grant grant = apiContext.getOperationsContext().getDatabaseRepository().getGrantDao().find(newDmp.getGrant().getId());
|
||||
if (grant.getFunder() != null && newDmp.getGrant().getFunder() != null
|
||||
&& !grant.getFunder().getId().equals(newDmp.getGrant().getFunder().getId())
|
||||
&& !grant.getCreationUser().getId().equals(user.getId())){
|
||||
throw new Exception("User is not the owner of the Grant, therefore, cannot edit it");
|
||||
}
|
||||
}
|
||||
checkIfUserCanEditGrant(newDmp, user);
|
||||
checkIfGrandHasCreationUser(newDmp, user);
|
||||
|
||||
if (newDmp.getGrant().getCreationUser() == null) {
|
||||
newDmp.getGrant().setCreationUser(user);
|
||||
}
|
||||
apiContext.getOperationsContext().getDatabaseRepository().getGrantDao().createOrUpdate(newDmp.getGrant());
|
||||
newDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(newDmp);
|
||||
|
||||
|
@ -502,77 +493,88 @@ public class DataManagementPlanManager {
|
|||
}
|
||||
|
||||
if (dataManagementPlan.getAssociatedUsers().size() == 0)
|
||||
assignUser(newDmp, user, apiContext);
|
||||
assignUser(newDmp, user);
|
||||
|
||||
return newDmp;
|
||||
}
|
||||
|
||||
|
||||
public void assignUser(DMP dmp, UserInfo userInfo, ApiContext apiContext) {
|
||||
public void assignUser(DMP dmp, UserInfo userInfo) {
|
||||
UserDMP userDMP = new UserDMP();
|
||||
userDMP.setDmp(dmp);
|
||||
userDMP.setUser(userInfo);
|
||||
userDMP.setRole(UserDMP.UserDMPRoles.OWNER.getValue());
|
||||
apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().createOrUpdate(userDMP);
|
||||
databaseRepository.getUserDmpDao().createOrUpdate(userDMP);
|
||||
}
|
||||
|
||||
public void newVersion(UUID uuid, DataManagementPlanNewVersionModel dataManagementPlan, Principal principal) throws Exception {
|
||||
DMP oldDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().find(uuid);
|
||||
DMP oldDmp = databaseRepository.getDmpDao().find(uuid);
|
||||
|
||||
DataManagementPlanCriteria criteria = new DataManagementPlanCriteria();
|
||||
LinkedList<UUID> list = new LinkedList<>();
|
||||
list.push(oldDmp.getGroupId());
|
||||
criteria.setGroupIds(list);
|
||||
criteria.setAllVersions(false);
|
||||
QueryableList<DMP> dataManagementPlanQueryableList = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().getWithCriteria(criteria);
|
||||
QueryableList<DMP> dataManagementPlanQueryableList = databaseRepository.getDmpDao().getWithCriteria(criteria);
|
||||
List<DMP> 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());
|
||||
|
||||
createOrganisationsIfTheyDontExist(newDmp, databaseRepository.getOrganisationDao());
|
||||
createResearchersIfTheyDontExist(newDmp, databaseRepository.getResearcherDao());
|
||||
UserInfo user = apiContext.getOperationsContext().getBuilderFactory().getBuilder(UserInfoBuilder.class).id(principal.getId()).build();
|
||||
createGrantIfItDoesntExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getGrantDao());
|
||||
createProjectIfItDoesntExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getProjectDao());
|
||||
|
||||
createFunderIfItDoesntExist(newDmp, databaseRepository.getFunderDao());
|
||||
createGrantIfItDoesntExist(newDmp, databaseRepository.getGrantDao());
|
||||
if (newDmp.getProject().getLabel() == null || newDmp.getProject().getLabel().trim().isEmpty()) {
|
||||
newDmp.setProject(newDmp.getProject().projectFromGrant(newDmp.getGrant()));
|
||||
}
|
||||
createProjectIfItDoesntExist(newDmp, databaseRepository.getProjectDao());
|
||||
|
||||
newDmp.setGroupId(oldDmp.getGroupId());
|
||||
newDmp.setVersion(oldDmp.getVersion() + 1);
|
||||
newDmp.setId(null);
|
||||
newDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(newDmp);
|
||||
|
||||
checkIfUserCanEditGrant(newDmp, user);
|
||||
checkIfGrandHasCreationUser(newDmp, user);
|
||||
databaseRepository.getGrantDao().createOrUpdate(newDmp.getGrant());
|
||||
newDmp = databaseRepository.getDmpDao().createOrUpdate(newDmp);
|
||||
|
||||
// Assign creator.
|
||||
UserDMP userDMP = new UserDMP();
|
||||
userDMP.setDmp(newDmp);
|
||||
userDMP.setUser(user);
|
||||
userDMP.setRole((UserDMP.UserDMPRoles.OWNER.getValue()));
|
||||
apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().createOrUpdate(userDMP);
|
||||
assignUser(newDmp, user);
|
||||
|
||||
copyDatasets(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao());
|
||||
copyDatasets(newDmp, databaseRepository.getDatasetDao());
|
||||
} else {
|
||||
throw new DMPNewVersionException("Version to update not the latest.");
|
||||
}
|
||||
}
|
||||
|
||||
public void clone(UUID uuid, DataManagementPlanNewVersionModel dataManagementPlan, Principal principal) throws Exception {
|
||||
public void clone(DataManagementPlanNewVersionModel dataManagementPlan, Principal principal) throws Exception {
|
||||
DMP newDmp = dataManagementPlan.toDataModel();
|
||||
createOrganisationsIfTheyDontExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getOrganisationDao());
|
||||
createResearchersIfTheyDontExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getResearcherDao());
|
||||
createOrganisationsIfTheyDontExist(newDmp, databaseRepository.getOrganisationDao());
|
||||
createResearchersIfTheyDontExist(newDmp, databaseRepository.getResearcherDao());
|
||||
UserInfo user = apiContext.getOperationsContext().getBuilderFactory().getBuilder(UserInfoBuilder.class).id(principal.getId()).build();
|
||||
createGrantIfItDoesntExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getGrantDao());
|
||||
createProjectIfItDoesntExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getProjectDao());
|
||||
|
||||
createFunderIfItDoesntExist(newDmp, databaseRepository.getFunderDao());
|
||||
createGrantIfItDoesntExist(newDmp, databaseRepository.getGrantDao());
|
||||
if (newDmp.getProject().getLabel() == null || newDmp.getProject().getLabel().trim().isEmpty()) {
|
||||
newDmp.setProject(newDmp.getProject().projectFromGrant(newDmp.getGrant()));
|
||||
}
|
||||
createProjectIfItDoesntExist(newDmp, databaseRepository.getProjectDao());
|
||||
|
||||
newDmp.setGroupId(UUID.randomUUID());
|
||||
newDmp.setVersion(0);
|
||||
newDmp.setId(null);
|
||||
newDmp = apiContext.getOperationsContext().getDatabaseRepository().getDmpDao().createOrUpdate(newDmp);
|
||||
|
||||
checkIfUserCanEditGrant(newDmp, user);
|
||||
checkIfGrandHasCreationUser(newDmp, user);
|
||||
databaseRepository.getGrantDao().createOrUpdate(newDmp.getGrant());
|
||||
newDmp = databaseRepository.getDmpDao().createOrUpdate(newDmp);
|
||||
|
||||
// Assign creator.
|
||||
UserDMP userDMP = new UserDMP();
|
||||
userDMP.setDmp(newDmp);
|
||||
userDMP.setUser(user);
|
||||
userDMP.setRole((UserDMP.UserDMPRoles.OWNER.getValue()));
|
||||
apiContext.getOperationsContext().getDatabaseRepository().getUserDmpDao().createOrUpdate(userDMP);
|
||||
copyDatasets(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getDatasetDao());
|
||||
assignUser(newDmp, user);
|
||||
copyDatasets(newDmp, databaseRepository.getDatasetDao());
|
||||
}
|
||||
|
||||
public void delete(UUID uuid) throws DMPWithDatasetsDeleteException {
|
||||
|
@ -652,6 +654,21 @@ public class DataManagementPlanManager {
|
|||
}
|
||||
}
|
||||
|
||||
private void checkIfUserCanEditGrant(DMP dmp, UserInfo user) throws Exception{
|
||||
if (dmp.getGrant().getId() != null) {
|
||||
Grant grant = apiContext.getOperationsContext().getDatabaseRepository().getGrantDao().find(dmp.getGrant().getId());
|
||||
if (grant.getFunder() != null && dmp.getGrant().getFunder() != null
|
||||
&& !grant.getFunder().getId().equals(dmp.getGrant().getFunder().getId())) {
|
||||
if (grant.getCreationUser() == null) {
|
||||
throw new Exception("Grant has no user, therefore, cannot be edited.");
|
||||
}
|
||||
if (!grant.getCreationUser().getId().equals(user.getId())) {
|
||||
throw new Exception("User is not the owner of the Grant, therefore, cannot edit it.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkDmpValidationRules(DMP dmp) throws Exception {
|
||||
if (dmp.getLabel() == null || dmp.getLabel().trim().isEmpty()) {
|
||||
throw new Exception("DMP has no label.");
|
||||
|
@ -664,6 +681,12 @@ public class DataManagementPlanManager {
|
|||
}
|
||||
}
|
||||
|
||||
private void checkIfGrandHasCreationUser(DMP dmp, UserInfo user) {
|
||||
if (dmp.getGrant().getCreationUser() == null) {
|
||||
dmp.getGrant().setCreationUser(user);
|
||||
}
|
||||
}
|
||||
|
||||
private void copyDatasets(DMP newDmp, DatasetDao datasetDao) {
|
||||
List<CompletableFuture<Dataset>> futures = new LinkedList<>();
|
||||
for (Dataset dataset : newDmp.getDataset()) {
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
package eu.eudat.models.data.dmp;
|
||||
|
||||
import eu.eudat.data.entities.DMP;
|
||||
import eu.eudat.data.entities.UserDMP;
|
||||
import eu.eudat.data.entities.*;
|
||||
import eu.eudat.models.DataModel;
|
||||
import eu.eudat.models.data.dataset.Dataset;
|
||||
import eu.eudat.models.data.grant.Grant;
|
||||
import eu.eudat.models.data.funder.FunderDMPEditorModel;
|
||||
import eu.eudat.models.data.grant.GrantDMPEditorModel;
|
||||
import eu.eudat.models.data.project.ProjectDMPEditorModel;
|
||||
import eu.eudat.models.data.userinfo.UserInfo;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -21,18 +22,19 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
private int status;
|
||||
private String description;
|
||||
private List<AssociatedProfile> profiles;
|
||||
private eu.eudat.models.data.grant.Grant grant;
|
||||
private eu.eudat.models.data.grant.GrantDMPEditorModel grant;
|
||||
private List<Organisation> organisations;
|
||||
private List<Researcher> researchers;
|
||||
private List<UserDMP> associatedUsers;
|
||||
private eu.eudat.models.data.userinfo.UserInfo creator;
|
||||
private Date created;
|
||||
private List<Dataset> datasets;
|
||||
private ProjectDMPEditorModel project;
|
||||
private FunderDMPEditorModel funder;
|
||||
|
||||
public UUID getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(UUID id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
@ -40,7 +42,6 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
public String getLabel() {
|
||||
return label;
|
||||
}
|
||||
|
||||
public void setLabel(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
@ -48,7 +49,6 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
public UUID getGroupId() {
|
||||
return groupId;
|
||||
}
|
||||
|
||||
public void setGroupId(UUID groupId) {
|
||||
this.groupId = groupId;
|
||||
}
|
||||
|
@ -56,7 +56,6 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
public int getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(int version) {
|
||||
this.version = version;
|
||||
}
|
||||
|
@ -64,7 +63,6 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(int status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
@ -72,7 +70,6 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
@ -80,23 +77,20 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
public List<AssociatedProfile> getProfiles() {
|
||||
return profiles;
|
||||
}
|
||||
|
||||
public void setProfiles(List<AssociatedProfile> profiles) {
|
||||
this.profiles = profiles;
|
||||
}
|
||||
|
||||
public Grant getGrant() {
|
||||
public GrantDMPEditorModel getGrant() {
|
||||
return grant;
|
||||
}
|
||||
|
||||
public void setGrant(Grant grant) {
|
||||
public void setGrant(GrantDMPEditorModel grant) {
|
||||
this.grant = grant;
|
||||
}
|
||||
|
||||
public List<Organisation> getOrganisations() {
|
||||
return organisations;
|
||||
}
|
||||
|
||||
public void setOrganisations(List<Organisation> organisations) {
|
||||
this.organisations = organisations;
|
||||
}
|
||||
|
@ -104,7 +98,6 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
public List<Researcher> getResearchers() {
|
||||
return researchers;
|
||||
}
|
||||
|
||||
public void setResearchers(List<Researcher> researchers) {
|
||||
this.researchers = researchers;
|
||||
}
|
||||
|
@ -112,7 +105,6 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
public List<UserDMP> getAssociatedUsers() {
|
||||
return associatedUsers;
|
||||
}
|
||||
|
||||
public void setAssociatedUsers(List<UserDMP> associatedUsers) {
|
||||
this.associatedUsers = associatedUsers;
|
||||
}
|
||||
|
@ -120,7 +112,6 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
public UserInfo getCreator() {
|
||||
return creator;
|
||||
}
|
||||
|
||||
public void setCreator(UserInfo creator) {
|
||||
this.creator = creator;
|
||||
}
|
||||
|
@ -128,7 +119,6 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
public Date getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
public void setCreated(Date created) {
|
||||
this.created = created;
|
||||
}
|
||||
|
@ -136,11 +126,24 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
public List<Dataset> getDatasets() {
|
||||
return datasets;
|
||||
}
|
||||
|
||||
public void setDatasets(List<Dataset> datasets) {
|
||||
this.datasets = datasets;
|
||||
}
|
||||
|
||||
public ProjectDMPEditorModel getProject() {
|
||||
return project;
|
||||
}
|
||||
public void setProject(ProjectDMPEditorModel project) {
|
||||
this.project = project;
|
||||
}
|
||||
|
||||
public FunderDMPEditorModel getFunder() {
|
||||
return funder;
|
||||
}
|
||||
public void setFunder(FunderDMPEditorModel funder) {
|
||||
this.funder = funder;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataManagementPlanNewVersionModel fromDataModel(DMP entity) {
|
||||
return null;
|
||||
|
@ -170,16 +173,76 @@ public class DataManagementPlanNewVersionModel implements DataModel<DMP, DataMan
|
|||
entity.setOrganisations(new HashSet<>(this.organisations.stream().map(item -> item.toDataModel()).collect(Collectors.toList())));
|
||||
if (this.researchers != null && !this.researchers.isEmpty())
|
||||
entity.setResearchers(new HashSet<>(this.researchers.stream().map(item -> item.toDataModel()).collect(Collectors.toList())));
|
||||
if (this.grant != null) entity.setGrant(this.grant.toDataModel());
|
||||
/*if (this.grant != null) {
|
||||
entity.setGrant(this.grant.toDataModel());
|
||||
}*/
|
||||
|
||||
if (this.grant != null) {
|
||||
if (this.grant.getExistGrant() != null && this.grant.getLabel() == null && this.grant.getDescription() == null)
|
||||
entity.setGrant(this.grant.getExistGrant().toDataModel());
|
||||
else {
|
||||
eu.eudat.data.entities.Grant grant = new eu.eudat.data.entities.Grant();
|
||||
grant.setAbbreviation("");
|
||||
grant.setLabel(this.grant.getLabel());
|
||||
grant.setType(eu.eudat.data.entities.Grant.GrantType.INTERNAL.getValue());
|
||||
grant.setReference("dmp:" + this.grant.getLabel());
|
||||
grant.setUri("");
|
||||
grant.setDefinition("");
|
||||
grant.setCreated(new Date());
|
||||
grant.setStatus(Grant.Status.ACTIVE.getValue());
|
||||
grant.setModified(new Date());
|
||||
grant.setDescription(this.grant.getDescription());
|
||||
|
||||
entity.setGrant(grant);
|
||||
}
|
||||
}
|
||||
|
||||
if(this.funder == null && this.funder.getExistFunder() == null && this.funder.getLabel() == null) {
|
||||
entity.getGrant().setFunder(null);
|
||||
}
|
||||
else if (this.funder != null && this.funder.getExistFunder() == null && this.funder.getLabel() == null) {
|
||||
entity.getGrant().setFunder(null);
|
||||
}
|
||||
else {
|
||||
if (this.funder.getLabel() != null) {
|
||||
Funder funder = new Funder();
|
||||
funder.setLabel(this.funder.getLabel());
|
||||
funder.setType(Funder.FunderType.INTERNAL.getValue());
|
||||
funder.setReference("dmp:" + this.funder.getLabel());
|
||||
funder.setDefinition("");
|
||||
funder.setCreated(new Date());
|
||||
funder.setStatus(Funder.Status.ACTIVE.getValue());
|
||||
funder.setModified(new Date());
|
||||
|
||||
entity.getGrant().setFunder(funder);
|
||||
}
|
||||
else if (this.funder.getExistFunder() != null && this.funder.getLabel() == null){
|
||||
entity.getGrant().setFunder(this.funder.getExistFunder().toDataModel());
|
||||
}
|
||||
}
|
||||
|
||||
if (this.project != null) {
|
||||
if (this.project.getExistProject() != null && this.project.getLabel() == null && this.project.getDescription() == null)
|
||||
entity.setProject(this.project.getExistProject().toDataModel());
|
||||
else {
|
||||
Project project = new Project();
|
||||
project.setAbbreviation("");
|
||||
project.setLabel(this.project.getLabel());
|
||||
project.setType(Project.ProjectType.INTERNAL.getValue());
|
||||
project.setReference("dmp:" + this.project.getLabel());
|
||||
project.setUri("");
|
||||
project.setDefinition("");
|
||||
project.setCreated(new Date());
|
||||
project.setStatus(Project.Status.ACTIVE.getValue());
|
||||
project.setModified(new Date());
|
||||
project.setDescription(this.project.getDescription());
|
||||
|
||||
entity.setProject(project);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.profiles != null)
|
||||
entity.setAssociatedDmps(this.profiles.stream().map(x -> x.toData()).collect(Collectors.toSet()));
|
||||
/*Document associatedProfileDoc = XmlBuilder.getDocument();
|
||||
Element associatedProfilesElement = associatedProfileDoc.createElement("profiles");
|
||||
for (AssociatedProfile associatedProfile : this.profiles) {
|
||||
associatedProfilesElement.appendChild(associatedProfile.toXml(associatedProfileDoc));
|
||||
}
|
||||
associatedProfileDoc.appendChild(associatedProfilesElement);
|
||||
entity.setAssociatedDmps(XmlBuilder.generateXml(associatedProfileDoc));*/
|
||||
return entity;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue