argos/dmp-backend/src/main/java/eu/eudat/models/dmp/DataManagementPlan.java

181 lines
6.3 KiB
Java

package eu.eudat.models.dmp;
import eu.eudat.entities.*;
import eu.eudat.models.DataModel;
import eu.eudat.models.project.Project;
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.util.*;
import java.util.stream.Collectors;
public class DataManagementPlan implements DataModel<DMP>{
private UUID id;
private String label;
private UUID previous;
private int version;
private int status;
private String description;
private List<AssociatedProfile> profiles;
private eu.eudat.models.project.Project project;
private List<Organisation> organisations;
private List<Researcher> researchers;
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 getPrevious() {
return previous;
}
public void setPrevious(UUID previous) {
this.previous = previous;
}
public int getVersion() {
return version;
}
public void setVersion(int version) {
this.version = version;
}
public List<Organisation> getOrganisations() {
return organisations;
}
public void setOrganisations(List<Organisation> organizations) {
this.organisations = organizations;
}
public List<Researcher> getResearchers() {
return researchers;
}
public void setResearchers(List<Researcher> 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<AssociatedProfile> getProfiles() {
return profiles;
}
public void setProfiles(List<AssociatedProfile> 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<eu.eudat.entities.Organisation,Organisation>().fromDataModel(entity.getOrganisations().stream().collect(Collectors.toList()),Organisation.class);
this.researchers =new DomainModelConverter<eu.eudat.entities.Researcher,Researcher>().fromDataModel(entity.getResearchers().stream().collect(Collectors.toList()),Researcher.class);
this.version = entity.getVersion();
this.previous = entity.getPrevious();
this.label = entity.getLabel();
this.project = new Project();
this.project.fromDataModel(entity.getProject());
this.creator = new eu.eudat.models.userinfo.UserInfo();
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();
}
@Override
public DMP toDataModel() {
DMP dataManagementPlanEntity = new DMP();
dataManagementPlanEntity.setId(this.id);
if(this.organisations!=null&&!this.organisations.isEmpty())dataManagementPlanEntity.setOrganisations(new HashSet<eu.eudat.entities.Organisation>(new DomainModelConverter<eu.eudat.entities.Organisation,Organisation>().toDataModel(this.organisations)));
if(this.researchers!=null&&!this.researchers.isEmpty())dataManagementPlanEntity.setResearchers(new HashSet<eu.eudat.entities.Researcher>(new DomainModelConverter<eu.eudat.entities.Researcher,Researcher>().toDataModel(this.researchers)));
dataManagementPlanEntity.setVersion(this.version);
dataManagementPlanEntity.setPrevious(this.previous);
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.setCreated(this.created!=null?this.created:new Date());
return dataManagementPlanEntity;
}
}