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

261 lines
9.8 KiB
Java

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<DMP, DataManagementPlan> {
private UUID id;
private String label;
private UUID groupId;
private Tuple<UUID, String> profile;
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 List<UserInfo> associatedUsers;
private DataManagementPlanProfile definition;
private eu.eudat.models.userinfo.UserInfo creator;
private Date created;
private List<DynamicFieldWithValue> dynamicFields;
private Map<String, Object> properties;
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public Tuple<UUID, String> getProfile() {
return profile;
}
public void setProfile(Tuple<UUID, String> 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<UserInfo> getAssociatedUsers() {
return associatedUsers;
}
public void setAssociatedUsers(List<UserInfo> associatedUsers) {
this.associatedUsers = associatedUsers;
}
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;
}
public DataManagementPlanProfile getDefinition() {
return definition;
}
public void setDefinition(DataManagementPlanProfile definition) {
this.definition = definition;
}
public Map<String, Object> getProperties() {
return properties;
}
public void setProperties(Map<String, Object> properties) {
this.properties = properties;
}
public List<DynamicFieldWithValue> getDynamicFields() {
return dynamicFields;
}
public void setDynamicFields(List<DynamicFieldWithValue> dynamicFields) {
this.dynamicFields = dynamicFields;
}
@Override
public DataManagementPlan fromDataModel(DMP entity) {
this.id = entity.getId();
this.profile = entity.getProfile() != null ? new Tuple<UUID, String>(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<Map<String, Object>> fieldOptional = ((List<Map<String, Object>>) 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;
}
}