1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package eu.ehri.project.importers.eag;
21
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.Lists;
24 import eu.ehri.project.definitions.Entities;
25 import eu.ehri.project.definitions.Ontology;
26 import eu.ehri.project.exceptions.ValidationError;
27 import eu.ehri.project.importers.base.ItemImporter;
28 import eu.ehri.project.importers.base.SaxXmlHandler;
29 import eu.ehri.project.importers.properties.XmlImportProperties;
30 import eu.ehri.project.importers.util.ImportHelpers;
31 import eu.ehri.project.models.MaintenanceEvent;
32 import eu.ehri.project.models.base.Entity;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.xml.sax.SAXException;
36
37 import java.util.Map;
38
39
40
41
42
43 public class EagHandler extends SaxXmlHandler {
44
45 private final ImmutableMap<String, Class<? extends Entity>> possibleSubNodes = ImmutableMap.of(
46 Entities.MAINTENANCE_EVENT, MaintenanceEvent.class
47 );
48 private static final Logger logger = LoggerFactory.getLogger(EagHandler.class);
49
50 public EagHandler(ItemImporter<Map<String, Object>, ?> importer) {
51 super(importer, new XmlImportProperties("eag.properties"));
52 }
53
54 @Override
55 protected boolean needToCreateSubNode(String qName) {
56 return possibleSubNodes.containsKey(getMappedProperty(currentPath));
57 }
58
59 @Override
60 public void endElement(String uri, String localName, String qName) throws SAXException {
61
62 super.endElement(uri, localName, qName);
63
64 if (needToCreateSubNode(qName)) {
65 logger.debug("endElement: {}", qName);
66
67 logger.debug("just before popping: {} - {} - {}", depth, getMappedProperty(currentPath), qName);
68 Map<String, Object> currentGraph = currentGraphPath.pop();
69 putSubGraphInCurrentGraph(getMappedProperty(currentPath), currentGraph);
70 depth--;
71 }
72
73 currentPath.pop();
74
75 if (currentPath.isEmpty()) {
76 try {
77 logger.debug("depth close {} {}", depth, qName);
78
79 if (!currentGraphPath.peek().containsKey(ImportHelpers.OBJECT_IDENTIFIER)) {
80 logger.warn("no objectIdentifier found");
81 putPropertyInCurrentGraph(ImportHelpers.OBJECT_IDENTIFIER, "id");
82 }
83 if (!currentGraphPath.peek().containsKey("typeOfEntity")) {
84 putPropertyInCurrentGraph("typeOfEntity", "organisation");
85 }
86 if (!currentGraphPath.peek().containsKey(Ontology.NAME_KEY)) {
87 logger.debug("no {} found", Ontology.NAME_KEY);
88 putPropertyInCurrentGraph(Ontology.NAME_KEY, "title");
89 }
90 if (!currentGraphPath.peek().containsKey(Ontology.LANGUAGE_OF_DESCRIPTION)) {
91 logger.debug("no {} found", Ontology.LANGUAGE_OF_DESCRIPTION);
92 putPropertyInCurrentGraph(Ontology.LANGUAGE_OF_DESCRIPTION, "en");
93 }
94 if (!currentGraphPath.peek().containsKey("rulesAndConventions")) {
95 logger.debug("no rulesAndConventions found");
96 putPropertyInCurrentGraph("rulesAndConventions", "ISDIAH");
97 }
98 importer.importItem(currentGraphPath.pop(), Lists.<String>newArrayList());
99
100 } catch (ValidationError ex) {
101 logger.error(ex.getMessage());
102 }
103 }
104 }
105 }