package eu.eudat.models.dmp; import eu.eudat.entities.*; import eu.eudat.models.DataModel; import eu.eudat.models.project.Project; import eu.eudat.models.userinfo.UserInfo; import eu.eudat.utilities.builders.DomainModelConverter; import eu.eudat.utilities.builders.XmlBuilder; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import java.text.ParseException; import java.util.*; import java.util.stream.Collectors; public class DataManagementPlan implements DataModel { private UUID id; private String label; private UUID groupId; 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 eu.eudat.models.userinfo.UserInfo creator; private Date created; public UUID getId() { return id; } public void setId(UUID id) { this.id = id; } 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; } @Override public void fromDataModel(DMP entity) throws InstantiationException, IllegalAccessException { this.id = entity.getId(); this.organisations = new DomainModelConverter().fromDataModel(entity.getOrganisations().stream().collect(Collectors.toList()), Organisation.class); this.researchers = new DomainModelConverter().fromDataModel(entity.getResearchers().stream().collect(Collectors.toList()), Researcher.class); this.version = entity.getVersion(); this.groupId = this.groupId == null ? null : this.groupId; this.label = entity.getLabel(); this.project = new Project(); this.project.fromDataModel(entity.getProject()); this.creator = new eu.eudat.models.userinfo.UserInfo(); this.groupId = entity.getGroupId(); 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 = new DomainModelConverter().fromDataModel(entity.getUsers().stream().collect(Collectors.toList()), UserInfo.class); } @Override public DMP toDataModel() throws Exception { DMP dataManagementPlanEntity = new DMP(); dataManagementPlanEntity.setId(this.id); if (this.organisations != null && !this.organisations.isEmpty()) dataManagementPlanEntity.setOrganisations(new HashSet(new DomainModelConverter().toDataModel(this.organisations))); if (this.researchers != null && !this.researchers.isEmpty()) dataManagementPlanEntity.setResearchers(new HashSet(new DomainModelConverter().toDataModel(this.researchers))); 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.setGroupId(this.groupId != null ? this.groupId : UUID.randomUUID()); dataManagementPlanEntity.setCreated(this.created != null ? this.created : new Date()); dataManagementPlanEntity.setUsers(new HashSet<>(new DomainModelConverter().toDataModel(this.associatedUsers))); return dataManagementPlanEntity; } @Override public String getHint() { return null; } }