0
|
1 package de.mpiwg.anteater.xml.impl;
|
|
2
|
|
3 import java.io.File;
|
|
4 import java.io.InputStream;
|
|
5 import java.util.List;
|
|
6
|
|
7 import org.jdom2.Attribute;
|
|
8 import org.jdom2.Element;
|
|
9
|
|
10 import de.mpiwg.anteater.events.Applicant;
|
|
11 import de.mpiwg.anteater.events.Location;
|
|
12 import de.mpiwg.anteater.events.ResearchEvent;
|
|
13 import de.mpiwg.anteater.events.Species;
|
|
14 import de.mpiwg.anteater.places.Place;
|
|
15
|
|
16 public class EventXMLManager extends JDOMParser {
|
|
17
|
|
18 private String filepath;
|
|
19
|
|
20 public EventXMLManager(String filepath) {
|
|
21 super();
|
|
22 this.filepath = filepath;
|
|
23
|
|
24 File resultFile = new File(filepath);
|
|
25 if (resultFile.exists())
|
|
26 init(filepath);
|
|
27 else {
|
|
28 InputStream stream = getClass().getResourceAsStream(
|
|
29 "templates/eventsFile.xml");
|
|
30 init(stream);
|
|
31 }
|
|
32 }
|
|
33
|
|
34 public void addEvent(ResearchEvent event) {
|
|
35
|
|
36 Element eventNode = new Element("event");
|
|
37 File file = new File(event.getTextInformation().getFilepath());
|
|
38 eventNode.setAttribute(new Attribute("text", file.getName()));
|
|
39 eventNode.setAttribute(new Attribute("date_filed", event.getDate()));
|
|
40
|
|
41 Element applicantsNode = new Element("applicants");
|
|
42 eventNode.addContent(applicantsNode);
|
|
43
|
|
44 // add applicants
|
|
45 for (Applicant applicant : event.getApplicants()) {
|
|
46 Element applicantNode = new Element("applicant");
|
|
47 applicantsNode.addContent(applicantNode);
|
|
48
|
|
49 // name
|
|
50 Element name = new Element("name");
|
|
51 applicantNode.addContent(name);
|
|
52 name.setText(applicant.getApplicant().getReferenceInText());
|
|
53
|
|
54 // appliant institution
|
|
55 Element appInstNode = new Element("applicant_institutions");
|
|
56 applicantNode.addContent(appInstNode);
|
|
57
|
|
58 for (Location location : applicant.getApplicantInstitution()) {
|
|
59 Element appInst = new Element("applicant_institution");
|
|
60 appInstNode.addContent(appInst);
|
|
61
|
|
62 createPlace(location, appInst);
|
|
63 }
|
|
64
|
|
65 Element appLocs = new Element("applicant_locations");
|
|
66 applicantNode.addContent(appLocs);
|
|
67
|
|
68 for (Location location : applicant.getLocation()) {
|
|
69 Element appLoc = new Element("applicant_locations");
|
|
70 appLocs.addContent(appLoc);
|
|
71
|
|
72 createPlace(location, appLoc);
|
|
73 }
|
|
74 }
|
|
75
|
|
76 Element researchLocsNode = new Element("research_locations");
|
|
77 eventNode.addContent(researchLocsNode);
|
|
78
|
|
79 // add research location
|
|
80 for (Location location : event.getResearchLocations()) {
|
|
81 Element researchLoc = new Element("research_location");
|
|
82 researchLocsNode.addContent(researchLoc);
|
|
83
|
|
84 createPlace(location, researchLoc);
|
|
85 }
|
|
86
|
|
87 Element speciesNode = new Element("researched_species");
|
|
88 eventNode.addContent(speciesNode);
|
|
89 // add species
|
|
90 for (Species species : event.getResearchedSpecies()) {
|
|
91 Element spNode = new Element("species");
|
|
92 speciesNode.addContent(spNode);
|
|
93
|
|
94 spNode.setText(species.getSpeciesName().getReferenceInText());
|
|
95 spNode.setAttribute(new Attribute("identified_name", species.getSpeciesName().getIdentifiedName()));
|
|
96 }
|
|
97
|
|
98 this.addElementToDoc(eventNode, "/events");
|
|
99 }
|
|
100
|
|
101 public void createPlace(Location location, Element parent) {
|
|
102 Element instname = new Element("name");
|
|
103 instname.setText(location.getLocation().getReferenceInText());
|
|
104 parent.addContent(instname);
|
|
105
|
|
106 for (Place place : location.getLocation().getPlaces()) {
|
|
107 Element placeInfo = new Element("place_information");
|
|
108 parent.addContent(placeInfo);
|
|
109
|
|
110 placeInfo.setAttribute(new Attribute("type", place.getType()));
|
|
111 placeInfo.setText(place.getName());
|
|
112 placeInfo.setAttribute(new Attribute("woeId", place.getWoeId()));
|
|
113 placeInfo.setAttribute(new Attribute("latitude", place.getLatitude() + ""));
|
|
114 placeInfo.setAttribute(new Attribute("longitude", place.getLongitude() + ""));
|
|
115
|
|
116 }
|
|
117 }
|
|
118
|
|
119 private void addElementToDoc(Element element, String xpath) {
|
|
120
|
|
121 List<Element> nodes = executeXPath(xpath, null);
|
|
122
|
|
123 if (nodes != null && nodes.size() > 0) {
|
|
124 nodes.get(0).addContent(element);
|
|
125 }
|
|
126
|
|
127 save(filepath, false);
|
|
128 }
|
|
129 }
|