diff --git a/dmp-backend/data/src/main/java/eu/eudat/data/dao/entities/ProjectDaoImpl.java b/dmp-backend/data/src/main/java/eu/eudat/data/dao/entities/ProjectDaoImpl.java index 7c86bb357..91c9054c8 100644 --- a/dmp-backend/data/src/main/java/eu/eudat/data/dao/entities/ProjectDaoImpl.java +++ b/dmp-backend/data/src/main/java/eu/eudat/data/dao/entities/ProjectDaoImpl.java @@ -74,7 +74,7 @@ public class ProjectDaoImpl extends DatabaseAccess implements ProjectDa } public QueryableList getAuthenticated(QueryableList query, UserInfo principal) { - query.where((builder, root) -> builder.or(builder.equal(root.get("creationUser"), principal), builder.equal(root.join("dmps").join("users", JoinType.LEFT).join("user", JoinType.LEFT).get("id"), principal.getId()))).distinct(); + query.where((builder, root) -> builder.or(builder.equal(root.get("creationUser"), principal), builder.equal(root.join("dmps", JoinType.LEFT).join("users", JoinType.LEFT).join("user", JoinType.LEFT).get("id"), principal.getId()))).distinct(); return query; } 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 5aff42182..94daaf4b4 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,8 +142,8 @@ 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.DataManagementPlan dataManagementPlan, Principal principal) throws Exception { - this.dataManagementPlanManager.createOrUpdate(this.getApiContext(), dataManagementPlan, principal); + 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")); } 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 480130112..f8e252804 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 @@ -417,11 +417,10 @@ public class DataManagementPlanManager { return result; } - public void createOrUpdate(ApiContext apiContext, DataManagementPlan dataManagementPlan, Principal principal) throws Exception { + public void createOrUpdate(ApiContext apiContext, DataManagementPlanEditorModel dataManagementPlan, Principal principal) throws Exception { DMP newDmp = dataManagementPlan.toDataModel(); UserInfo user = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(principal.getId()); - //newDmp.setCreator(user); createOrganisationsIfTheyDontExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getOrganisationDao()); createResearchersIfTheyDontExist(newDmp, apiContext.getOperationsContext().getDatabaseRepository().getResearcherDao()); @@ -878,7 +877,7 @@ public class DataManagementPlanManager { dm.setDynamicFields(dynamicFields); // Sets dynamicFields property. dm.setDefinition(dmpProfile); - createOrUpdate(apiContext, dm, principal); + //createOrUpdate(apiContext, dm, principal); System.out.println(dm); } diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/data/dmp/DataManagementPlanEditorModel.java b/dmp-backend/web/src/main/java/eu/eudat/models/data/dmp/DataManagementPlanEditorModel.java new file mode 100644 index 000000000..c2672ad5c --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/models/data/dmp/DataManagementPlanEditorModel.java @@ -0,0 +1,291 @@ +package eu.eudat.models.data.dmp; + +import eu.eudat.data.entities.*; +import eu.eudat.logic.utilities.builders.XmlBuilder; +import eu.eudat.models.DataModel; +import eu.eudat.models.data.dynamicfields.DynamicFieldWithValue; +import eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.DataManagementPlanProfile; +import eu.eudat.models.data.helpermodels.Tuple; +import eu.eudat.models.data.listingmodels.DatasetListingModel; +import eu.eudat.models.data.listingmodels.UserInfoListingModel; +import eu.eudat.models.data.project.ProjectDMPEditorModel; +import eu.eudat.models.data.userinfo.UserListingModel; +import net.minidev.json.JSONObject; + +import java.util.*; +import java.util.stream.Collectors; + +public class DataManagementPlanEditorModel implements DataModel { + private UUID id; + private String label; + private UUID groupId; + private Tuple profile; + private int version; + private int status; + private boolean lockable; + private String description; + private List datasets; + private List profiles; + private eu.eudat.models.data.project.ProjectDMPEditorModel project; + private List organisations; + private List researchers; + private List associatedUsers; + private DataManagementPlanProfile definition; + private eu.eudat.models.data.userinfo.UserInfo creator; + private Date modified; + private Date created; + private List dynamicFields; + private Map properties; + private List users; + + public UUID getId() { + return id; + } + public void setId(UUID id) { + this.id = id; + } + + public Tuple getProfile() { + return profile; + } + public void setProfile(Tuple profile) { + this.profile = profile; + } + + public String getLabel() { + return label; + } + public void setLabel(String label) { + this.label = label; + } + + public UUID getGroupId() { + return groupId; + } + public void setGroupId(UUID groupId) { + this.groupId = groupId; + } + + public List getAssociatedUsers() { + return associatedUsers; + } + public void setAssociatedUsers(List associatedUsers) { + this.associatedUsers = associatedUsers; + } + + public int getVersion() { + return version; + } + public void setVersion(int version) { + this.version = version; + } + + public List getOrganisations() { + return organisations; + } + public void setOrganisations(List organizations) { + this.organisations = organizations; + } + + public List getResearchers() { + return researchers; + } + public void setResearchers(List researchers) { + this.researchers = researchers; + } + + public int getStatus() { + return status; + } + public void setStatus(int status) { + this.status = status; + } + + public ProjectDMPEditorModel getProject() { + return project; + } + public void setProject(ProjectDMPEditorModel project) { + this.project = project; + } + + public eu.eudat.models.data.userinfo.UserInfo getCreator() { + return creator; + } + public void setCreator(eu.eudat.models.data.userinfo.UserInfo creator) { + this.creator = creator; + } + + public List getProfiles() { + return profiles; + } + public void setProfiles(List profiles) { + this.profiles = profiles; + } + + public Date getModified() { + return modified; + } + public void setModified(Date modified) { + this.modified = modified; + } + + public Date getCreated() { + return created; + } + public void setCreated(Date created) { + this.created = created; + } + + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } + + public DataManagementPlanProfile getDefinition() { + return definition; + } + public void setDefinition(DataManagementPlanProfile definition) { + this.definition = definition; + } + + public Map getProperties() { + return properties; + } + public void setProperties(Map properties) { + this.properties = properties; + } + + public List getDynamicFields() { + return dynamicFields; + } + public void setDynamicFields(List dynamicFields) { + this.dynamicFields = dynamicFields; + } + + public boolean getLockable() { + return lockable; + } + public void setLockable(boolean lockable) { + this.lockable = lockable; + } + + public List getDatasets() { + return datasets; + } + public void setDatasets(List datasets) { + this.datasets = datasets; + } + + public List getUsers() { + return users; + } + public void setUsers(List users) { + this.users = users; + } + + @Override + public DataManagementPlanEditorModel fromDataModel(DMP entity) { + this.id = entity.getId(); + this.profile = entity.getProfile() != null ? new Tuple(entity.getProfile().getId(), entity.getProfile().getLabel()) : null; + this.organisations = entity.getOrganisations().stream().map(item -> new Organisation().fromDataModel(item)).collect(Collectors.toList()); + this.researchers = entity.getResearchers().stream().map(item -> new Researcher().fromDataModel(item)).collect(Collectors.toList()); + this.version = entity.getVersion(); + this.groupId = this.groupId == null ? null : this.groupId; + this.label = entity.getLabel(); + this.project = new ProjectDMPEditorModel(); + this.properties = entity.getProperties() != null ? new org.json.JSONObject(entity.getProperties()).toMap() : null; + this.project.getExistProject().fromDataModel(entity.getProject()); + this.creator = new eu.eudat.models.data.userinfo.UserInfo(); + this.groupId = entity.getGroupId(); + this.lockable = entity.getDataset().stream().findAny().isPresent(); + this.definition = entity.getProfile() == null ? null : new DataManagementPlanProfile().fromXml(XmlBuilder.fromXml(entity.getProfile().getDefinition()).getDocumentElement()); + if (this.definition != null && this.definition.getFields() != null && !this.definition.getFields().isEmpty() && this.properties != null) { + this.definition.getFields().forEach(item -> { + Optional> fieldOptional = ((List>) this.properties.get("fields")).stream().filter(field -> field.get("id").equals(item.getId().toString())).findFirst(); + if (fieldOptional.isPresent()) item.setValue(fieldOptional.get().get("value")); + }); + } + if (entity.getUsers().stream().anyMatch(userDMP -> userDMP.getRole().equals(UserDMP.UserDMPRoles.OWNER.getValue()))) + this.creator.fromDataModel(entity.getUsers().stream().filter(user -> user.getRole().equals(UserDMP.UserDMPRoles.OWNER.getValue())).findFirst().get().getUser()); + + if (entity.getAssociatedDmps() != null && !entity.getAssociatedDmps().isEmpty()) { + this.profiles = new LinkedList<>(); + for (DatasetProfile datasetProfile: entity.getAssociatedDmps()) { + AssociatedProfile associatedProfile = new AssociatedProfile().fromData(datasetProfile); + this.profiles.add(associatedProfile); + } + } + this.datasets = entity.getDataset().stream().map(item -> new DatasetListingModel().fromDataModel(item)).collect(Collectors.toList()); + this.modified = entity.getModified(); + this.created = entity.getCreated(); + this.description = entity.getDescription(); + this.status = entity.getStatus(); + this.associatedUsers = entity.getUsers().stream().map(item -> new UserListingModel().fromDataModel(item.getUser())).collect(Collectors.toList()); + this.users = entity.getUsers().stream().map(item -> new UserInfoListingModel().fromDataModel(item)).collect(Collectors.toList()); + return this; + } + + @Override + public DMP toDataModel() throws Exception { + DMP dataManagementPlanEntity = new DMP(); + if (this.profile != null) { + DMPProfile dmpProfile = new DMPProfile(); + dmpProfile.setId(this.profile.getId()); + dataManagementPlanEntity.setProfile(dmpProfile); + } + dataManagementPlanEntity.setId(this.id); + if (this.organisations != null && !this.organisations.isEmpty()) + dataManagementPlanEntity.setOrganisations(new HashSet<>(this.organisations.stream().map(item -> item.toDataModel()).collect(Collectors.toList()))); + if (this.researchers != null && !this.researchers.isEmpty()) + dataManagementPlanEntity.setResearchers(new HashSet<>(this.researchers.stream().map(item -> item.toDataModel()).collect(Collectors.toList()))); + dataManagementPlanEntity.setVersion(this.version); + dataManagementPlanEntity.setLabel(this.label); + if (this.project != null) { + if (this.project.getExistProject() != null && this.project.getLabel() == null && this.project.getDescription() == null) + dataManagementPlanEntity.setProject(this.project.getExistProject().toDataModel()); + else { + Project project = new Project(); + project.setAbbreviation(""); + project.setLabel(this.project.getLabel()); + project.setReference("dmp:" + this.project.getLabel()); + project.setUri(""); + project.setDefinition(""); + project.setType(Project.ProjectType.INTERNAL.getValue()); + project.setDescription(this.project.getDescription()); + project.setStatus(Project.Status.ACTIVE.getValue()); + project.setCreated(new Date()); + + UserInfo userInfo = new UserInfo(); + userInfo.setId(this.users.stream().filter(userInfoListingModel -> ((Integer) userInfoListingModel.getRole()).equals(UserDMP.UserDMPRoles.OWNER.getValue())).findFirst().get().getId()); + project.setCreationUser(userInfo); + + dataManagementPlanEntity.setProject(project); + } + } + + dataManagementPlanEntity.setStatus((short) this.status); + dataManagementPlanEntity.setDescription(this.description); + if (this.profiles != null) { + Set datasetProfiles = new HashSet<>(); + for (AssociatedProfile profile : this.profiles) { + datasetProfiles.add(profile.toData()); + } + dataManagementPlanEntity.setAssociatedDmps(datasetProfiles); + } + dataManagementPlanEntity.setProperties(this.properties != null ? JSONObject.toJSONString(this.properties) : null); + dataManagementPlanEntity.setGroupId(this.groupId != null ? this.groupId : UUID.randomUUID()); + dataManagementPlanEntity.setModified(this.modified != null ? this.modified : new Date()); + dataManagementPlanEntity.setCreated(this.created != null ? this.created : new Date()); + if (this.dynamicFields != null) + dataManagementPlanEntity.setDmpProperties(JSONObject.toJSONString(this.dynamicFields.stream().filter(item -> item.getValue() != null).collect(Collectors.toMap(DynamicFieldWithValue::getId, DynamicFieldWithValue::getValue)))); + + return dataManagementPlanEntity; + } + + @Override + public String getHint() { + return "fullyDetailed"; + } +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/data/project/ProjectDMPEditorModel.java b/dmp-backend/web/src/main/java/eu/eudat/models/data/project/ProjectDMPEditorModel.java new file mode 100644 index 000000000..7421a511d --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/models/data/project/ProjectDMPEditorModel.java @@ -0,0 +1,28 @@ +package eu.eudat.models.data.project; + +public class ProjectDMPEditorModel { + private Project existProject; + private String label; + private String description; + + public Project getExistProject() { + return existProject; + } + public void setExistProject(Project existProject) { + this.existProject = existProject; + } + + public String getLabel() { + return label; + } + public void setLabel(String label) { + this.label = label; + } + + public String getDescription() { + return description; + } + public void setDescription(String description) { + this.description = description; + } +}