package eu.eudat.models.dmp; import eu.eudat.entities.DMP; import eu.eudat.models.DataModel; import eu.eudat.models.dataset.Dataset; 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 java.lang.reflect.Array; import java.util.*; /** * Created by ikalyvas on 2/5/2018. */ public class DataManagementPlanNewVersionModel implements DataModel { private UUID id; private String label; private DataManagementPlan previous; 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; private List datasets; 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 DataManagementPlan getPrevious() { return previous; } public void setPrevious(DataManagementPlan previous) { this.previous = previous; } public int getVersion() { return version; } public void setVersion(int version) { this.version = version; } public int getStatus() { return status; } public void setStatus(int status) { this.status = status; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public List getProfiles() { return profiles; } public void setProfiles(List profiles) { this.profiles = profiles; } public Project getProject() { return project; } public void setProject(Project project) { this.project = project; } public List getOrganisations() { return organisations; } public void setOrganisations(List organisations) { this.organisations = organisations; } public List getResearchers() { return researchers; } public void setResearchers(List researchers) { this.researchers = researchers; } public List getAssociatedUsers() { return associatedUsers; } public void setAssociatedUsers(List associatedUsers) { this.associatedUsers = associatedUsers; } public UserInfo getCreator() { return creator; } public void setCreator(UserInfo creator) { this.creator = creator; } public Date getCreated() { return created; } public void setCreated(Date created) { this.created = created; } public List getDatasets() { return datasets; } public void setDatasets(List datasets) { this.datasets = datasets; } @Override public void fromDataModel(DMP entity) throws InstantiationException, IllegalAccessException { } @Override public DMP toDataModel() throws Exception { DMP entity = new DMP(); entity.setId(this.id); entity.setUsers(new HashSet<>(new DomainModelConverter().toDataModel(this.associatedUsers))); entity.setDescription(this.description); entity.setStatus((short) this.status); entity.setPrevious(this.previous == null ? null : new DomainModelConverter().toDataModel(Arrays.asList(this.previous)).get(0)); entity.setCreated(new Date()); entity.setLabel(this.label); entity.setModified(new Date()); List datasets = new LinkedList<>(); if(this.datasets!=null){ for (Dataset dataset :this.datasets){ eu.eudat.entities.Dataset entityDataset = new eu.eudat.entities.Dataset(); entityDataset.setId(dataset.getId()); datasets.add(entityDataset); } entity.setDataset(new HashSet<>(datasets)); } if (this.organisations != null && !this.organisations.isEmpty()) entity.setOrganisations(new HashSet(new DomainModelConverter().toDataModel(this.organisations))); if (this.researchers != null && !this.researchers.isEmpty()) entity.setResearchers(new HashSet(new DomainModelConverter().toDataModel(this.researchers))); if (this.project != null) entity.setProject(this.project.toDataModel()); 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); entity.setAssociatedDmps(XmlBuilder.generateXml(associatedProfileDoc)); } return entity; } @Override public String getHint() { return null; } }