argos/dmp-backend/core/src/main/java/eu/eudat/commons/types/descriptiontemplate/fielddata/UploadDataEntity.java

111 lines
3.1 KiB
Java

package eu.eudat.commons.types.descriptiontemplate.fielddata;
import eu.eudat.commons.enums.FieldType;
import eu.eudat.commons.types.xml.XmlSerializable;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class UploadDataEntity extends BaseFieldDataEntity<UploadDataEntity> {
public UploadDataEntity() {
super(FieldType.UPLOAD);
}
public static class Option implements XmlSerializable<UploadDataEntity.Option> {
private String label;
private String value;
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public Element toXml(Document doc) {
Element option = doc.createElement("option");
option.setAttribute("label", this.label);
option.setAttribute("value", this.value);
return option;
}
@Override
public UploadDataEntity.Option fromXml(Element item) {
this.label = item.getAttribute("label");
this.value = item.getAttribute("value");
return this;
}
}
private List<UploadDataEntity.Option> types;
public List<UploadDataEntity.Option> getTypes() {
return types;
}
public void setTypes(List<UploadDataEntity.Option> types) {
this.types = types;
}
private Integer maxFileSizeInMB;
public Integer getMaxFileSizeInMB() {
return maxFileSizeInMB;
}
public void setMaxFileSizeInMB(Integer maxFileSizeInMB) {
this.maxFileSizeInMB = maxFileSizeInMB;
}
@Override
public Element toXml(Document doc) {
Element root = super.toXml(doc);
Element element = doc.createElement("types");
for (UploadDataEntity.Option type : this.types) {
element.appendChild(type.toXml(doc));
}
root.setAttribute("maxFileSizeInMB", this.getMaxFileSizeInMB().toString());
root.appendChild(element);
return root;
}
@Override
public UploadDataEntity fromXml(Element item) {
this.types = new LinkedList<>();
super.fromXml(item);
Element optionsElement = (Element) item.getElementsByTagName("types").item(0);
if(item.getAttribute("maxFileSizeInMB") != null) {
this.setMaxFileSizeInMB(Integer.parseInt(item.getAttribute("maxFileSizeInMB")));
}
if (optionsElement != null) {
NodeList optionElements = optionsElement.getChildNodes();
for (int temp = 0; temp < optionElements.getLength(); temp++) {
Node optionElement = optionElements.item(temp);
if (optionElement.getNodeType() == Node.ELEMENT_NODE) {
this.types.add(new Option().fromXml((Element) optionElement));
}
}
}
return this;
}
}