package eu.eudat.models.dmp; import eu.eudat.data.entities.DMP; import eu.eudat.data.entities.DMPProfile; import eu.eudat.models.DataModel; import eu.eudat.models.dynamicfields.DynamicFieldWithValue; import eu.eudat.models.entities.xmlmodels.dmpprofiledefinition.DataManagementPlanProfile; import eu.eudat.models.helpermodels.Tuple; import eu.eudat.models.project.Project; import eu.eudat.models.userinfo.UserInfo; import eu.eudat.utilities.builders.XmlBuilder; import net.minidev.json.JSONObject; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.util.*; import java.util.stream.Collectors; public class DataManagementPlan implements DataModel { private UUID id; private String label; private UUID groupId; private Tuple profile; private int version; private int status; private String description; private List profiles; private eu.eudat.models.project.Project project; private List organisations; private List researchers; private List associatedUsers; private DataManagementPlanProfile definition; private eu.eudat.models.userinfo.UserInfo creator; private Date created; private List dynamicFields; private Map properties; 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 Project getProject() { return project; } public void setProject(Project project) { this.project = project; } public eu.eudat.models.userinfo.UserInfo getCreator() { return creator; } public void setCreator(eu.eudat.models.userinfo.UserInfo creator) { this.creator = creator; } public List getProfiles() { return profiles; } public void setProfiles(List profiles) { this.profiles = profiles; } 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; } @Override public DataManagementPlan 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 Project(); this.properties = entity.getProperties() != null ? new org.json.JSONObject(entity.getProperties()).toMap() : null; this.project.fromDataModel(entity.getProject()); this.creator = new eu.eudat.models.userinfo.UserInfo(); this.groupId = entity.getGroupId(); 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(); item.setValue(fieldOptional.orElse(null).get("value")); }); } if (entity.getCreator() != null) this.creator.fromDataModel(entity.getCreator()); if (entity.getAssociatedDmps() != null && !entity.getAssociatedDmps().isEmpty()) { Document viewStyleDoc = XmlBuilder.fromXml(entity.getAssociatedDmps()); Element item = (Element) viewStyleDoc.getElementsByTagName("profiles").item(0); this.profiles = new LinkedList<>(); if (item != null) { NodeList associatedProfilesElement = item.getChildNodes(); for (int temp = 0; temp < associatedProfilesElement.getLength(); temp++) { Node associatedProfileElement = associatedProfilesElement.item(temp); if (associatedProfileElement.getNodeType() == Node.ELEMENT_NODE) { this.profiles.add(new AssociatedProfile().fromXml((Element) associatedProfileElement)); } } } } this.created = entity.getCreated(); this.description = entity.getDescription(); this.associatedUsers = entity.getUsers().stream().map(item -> new UserInfo().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) dataManagementPlanEntity.setProject(this.project.toDataModel()); dataManagementPlanEntity.setStatus((short) this.status); dataManagementPlanEntity.setDescription(this.description); if (this.profiles != null) { Document associatedProfileDoc = XmlBuilder.getDocument(); Element associatedProfilesElement = associatedProfileDoc.createElement("profiles"); for (AssociatedProfile associatedProfile : this.profiles) { associatedProfilesElement.appendChild(associatedProfile.toXml(associatedProfileDoc)); } associatedProfileDoc.appendChild(associatedProfilesElement); dataManagementPlanEntity.setAssociatedDmps(XmlBuilder.generateXml(associatedProfileDoc)); } dataManagementPlanEntity.setProperties(this.properties != null ? JSONObject.toJSONString(this.properties) : null); dataManagementPlanEntity.setGroupId(this.groupId != null ? this.groupId : UUID.randomUUID()); dataManagementPlanEntity.setCreated(this.created != null ? this.created : new Date()); dataManagementPlanEntity.setDmpProperties(JSONObject.toJSONString(this.dynamicFields.stream().filter(item -> item.getValue() != null).collect(Collectors.toMap(DynamicFieldWithValue::getId, DynamicFieldWithValue::getValue)))); dataManagementPlanEntity.setDmpProperties(JSONObject.toJSONString(this.dynamicFields.stream().filter(item -> item.getValue() != null).collect(Collectors.toMap(DynamicFieldWithValue::getId, DynamicFieldWithValue::getValue)))); dataManagementPlanEntity.setUsers(new HashSet<>(this.associatedUsers.stream().map(item -> item.toDataModel()).collect(Collectors.toList()))); return dataManagementPlanEntity; } @Override public String getHint() { return null; } }