95 lines
3.2 KiB
Java
95 lines
3.2 KiB
Java
package eu.eudat.commons.types.descriptiontemplate;
|
|
|
|
import eu.eudat.commons.types.xml.XmlBuilder;
|
|
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.ArrayList;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
|
|
public class DefinitionEntity implements XmlSerializable<DefinitionEntity> {
|
|
private List<SectionEntity> sections;
|
|
private List<PageEntity> pages;
|
|
|
|
public List<SectionEntity> getSections() {
|
|
return sections;
|
|
}
|
|
|
|
public void setSections(List<SectionEntity> sections) {
|
|
this.sections = sections;
|
|
}
|
|
|
|
public List<PageEntity> getPages() {
|
|
return pages;
|
|
}
|
|
|
|
public void setPages(List<PageEntity> pageEntities) {
|
|
this.pages = pageEntities;
|
|
}
|
|
|
|
@Override
|
|
public Element toXml(Document doc) {
|
|
Element root = doc.createElement("root");
|
|
Element sections = doc.createElement("sections");
|
|
Element pages = doc.createElement("pages");
|
|
for (SectionEntity sectionEntity : this.sections) {
|
|
sectionEntity.setNumbering("" + (this.sections.indexOf(sectionEntity) + 1));
|
|
sections.appendChild(sectionEntity.toXml(doc));
|
|
}
|
|
|
|
for (PageEntity pageEntity : this.pages) {
|
|
pages.appendChild(pageEntity.toXml(doc));
|
|
}
|
|
|
|
root.appendChild(pages);
|
|
root.appendChild(sections);
|
|
return root;
|
|
}
|
|
|
|
@Override
|
|
public DefinitionEntity fromXml(Element element) {
|
|
|
|
this.sections = new LinkedList();
|
|
Element sections = (Element) XmlBuilder.getNodeFromListByTagName(element.getChildNodes(), "sections");
|
|
if (sections != null) {
|
|
NodeList sectionElements = sections.getChildNodes();
|
|
for (int temp = 0; temp < sectionElements.getLength(); temp++) {
|
|
Node sectionElement = sectionElements.item(temp);
|
|
if (sectionElement.getNodeType() == Node.ELEMENT_NODE) {
|
|
this.sections.add(new SectionEntity().fromXml((Element) sectionElement));
|
|
}
|
|
}
|
|
}
|
|
this.pages = new LinkedList<>();
|
|
Element pages = (Element) XmlBuilder.getNodeFromListByTagName(element.getChildNodes(), "pages");
|
|
if (pages != null) {
|
|
NodeList pagesElements = pages.getChildNodes();
|
|
for (int temp = 0; temp < pagesElements.getLength(); temp++) {
|
|
Node pageElement = pagesElements.item(temp);
|
|
if (pageElement.getNodeType() == Node.ELEMENT_NODE) {
|
|
this.pages.add(new PageEntity().fromXml((Element) pageElement));
|
|
}
|
|
}
|
|
}
|
|
return this;
|
|
}
|
|
public List<FieldEntity> getAllField(){
|
|
List<FieldEntity> fieldEntities = new ArrayList<>();
|
|
if (this.getSections() != null){
|
|
for (SectionEntity sectionEntity: this.getSections()) {
|
|
fieldEntities.addAll(sectionEntity.getAllField());
|
|
}
|
|
}
|
|
return fieldEntities;
|
|
}
|
|
|
|
public List<FieldEntity> getFieldById(String id) {
|
|
return this.getAllField().stream().filter(x-> id.equals(x.getId())).toList();
|
|
}
|
|
|
|
}
|