package eu.eudat.models.data.dmp; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import eu.eudat.data.entities.*; import eu.eudat.logic.utilities.builders.XmlBuilder; import eu.eudat.models.DataModel; import eu.eudat.models.data.datasetwizard.DatasetWizardModel; import eu.eudat.models.data.dynamicfields.DynamicFieldWithValue; import eu.eudat.models.data.entities.xmlmodels.dmpprofiledefinition.DataManagementPlanProfile; import eu.eudat.models.data.funder.FunderDMPEditorModel; 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.grant.GrantDMPEditorModel; import eu.eudat.models.data.project.ProjectDMPEditorModel; import eu.eudat.models.data.userinfo.UserListingModel; import net.minidev.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.*; import java.util.stream.Collectors; public class DataManagementPlanEditorModel implements DataModel { private static final ObjectMapper objectMapper = new ObjectMapper(); private static final Logger logger = LoggerFactory.getLogger(DataManagementPlanEditorModel.class); 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.grant.GrantDMPEditorModel grant; 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; private List datasetsToBeFinalized; private ProjectDMPEditorModel project; private FunderDMPEditorModel funder; private Map extraProperties; 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 GrantDMPEditorModel getGrant() { return grant; } public void setGrant(GrantDMPEditorModel grant) { this.grant = grant; } 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; } public List getDatasetsToBeFinalized() { return datasetsToBeFinalized; } public void setDatasetsToBeFinalized(List datasetsToBeFinalized) { this.datasetsToBeFinalized = datasetsToBeFinalized; } 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; } public Map getExtraProperties() { return extraProperties; } public void setExtraProperties(Map extraProperties) { this.extraProperties = extraProperties; } @Override public DataManagementPlanEditorModel fromDataModel(DMP entity) { try { 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 : entity.getGroupId(); this.label = entity.getLabel(); this.grant = new GrantDMPEditorModel(); this.properties = entity.getProperties() != null ? objectMapper.readValue(entity.getProperties(), LinkedHashMap.class) : null; this.grant.getExistGrant().fromDataModel(entity.getGrant()); this.grant.getExistGrant().setSource(""); 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 DatasetWizardModel().fromDataModelNoDmp(item)).collect(Collectors.toList()); this.modified = entity.getModified(); this.created = entity.getCreated(); this.description = entity.getDescription(); this.status = entity.getStatus(); this.project = new ProjectDMPEditorModel(); this.project.getExistProject().fromDataModel(entity.getProject()); 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()); this.funder = new FunderDMPEditorModel(); this.funder.getExistFunder().fromDataModel(entity.getGrant().getFunder()); this.extraProperties = entity.getExtraProperties() != null ? objectMapper.readValue(entity.getExtraProperties(), LinkedHashMap.class) : null; } catch (JsonProcessingException e) { logger.error(e.getLocalizedMessage(), e); } 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(Organisation::toDataModel).collect(Collectors.toList()))); if (this.researchers != null && !this.researchers.isEmpty()) dataManagementPlanEntity.setResearchers(new HashSet<>(this.researchers.stream().map(Researcher::toDataModel).collect(Collectors.toList()))); dataManagementPlanEntity.setVersion(this.version); dataManagementPlanEntity.setLabel(this.label); if (this.grant != null) { if (this.grant.getExistGrant() != null && this.grant.getLabel() == null && this.grant.getDescription() == null) dataManagementPlanEntity.setGrant(this.grant.getExistGrant().toDataModel()); else { Grant grant = new Grant(); grant.setId(UUID.randomUUID()); grant.setAbbreviation(""); grant.setLabel(this.grant.getLabel()); grant.setType(Grant.GrantType.INTERNAL.getValue()); grant.setReference("dmp:" + (this.grant.getReference() != null ? this.grant.getReference() : grant.getId())); grant.setUri(""); grant.setDefinition(""); grant.setCreated(new Date()); grant.setStatus(Grant.Status.ACTIVE.getValue()); grant.setModified(new Date()); grant.setDescription(this.grant.getDescription()); dataManagementPlanEntity.setGrant(grant); } } if (this.funder == null) { // dataManagementPlanEntity.getGrant().setFunder(null); throw new Exception("Funder is a mandatory entity"); } else if (this.funder.getExistFunder() == null && this.funder.getLabel() == null) { // dataManagementPlanEntity.getGrant().setFunder(null); throw new Exception("Funder is a mandatory entity"); } else { if (this.funder.getLabel() != null) { Funder funder = new Funder(); funder.setId(UUID.randomUUID()); funder.setLabel(this.funder.getLabel()); funder.setType(Funder.FunderType.INTERNAL.getValue()); funder.setReference("dmp:" + (this.funder.getReference() != null ? this.funder.getReference() : funder.getId())); funder.setDefinition(""); funder.setCreated(new Date()); funder.setStatus(Funder.Status.ACTIVE.getValue()); funder.setModified(new Date()); dataManagementPlanEntity.getGrant().setFunder(funder); } else if (this.funder.getExistFunder() != null && this.funder.getLabel() == null){ dataManagementPlanEntity.getGrant().setFunder(this.funder.getExistFunder().toDataModel()); dataManagementPlanEntity.getGrant().getFunder().setType(Funder.FunderType.EXTERNAL.getValue()); } } 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.setId(UUID.randomUUID()); project.setAbbreviation(""); project.setLabel(this.project.getLabel()); project.setType(Project.ProjectType.INTERNAL.getValue()); project.setReference("dmp:" + (this.project.getReference() != null ? this.project.getReference() : project.getId())); project.setUri(""); project.setDefinition(""); project.setCreated(new Date()); project.setStatus(Project.Status.ACTIVE.getValue()); project.setModified(new Date()); project.setDescription(this.project.getDescription()); dataManagementPlanEntity.setProject(project); } } dataManagementPlanEntity.setStatus((short) this.status); if (this.status == (int) DMP.DMPStatus.FINALISED.getValue()) { dataManagementPlanEntity.setFinalizedAt(new Date()); } 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)))); dataManagementPlanEntity.setExtraProperties(this.extraProperties != null ? JSONObject.toJSONString(this.extraProperties) : null); return dataManagementPlanEntity; } @Override public String getHint() { return "fullyDetailed"; } }