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.tinkerpop.frames.FramedGraph;
23 import eu.ehri.project.definitions.Entities;
24 import eu.ehri.project.definitions.Ontology;
25 import eu.ehri.project.exceptions.ValidationError;
26 import eu.ehri.project.importers.ImportLog;
27 import eu.ehri.project.importers.base.AbstractImporter;
28 import eu.ehri.project.importers.eac.EacImporter;
29 import eu.ehri.project.importers.util.ImportHelpers;
30 import eu.ehri.project.models.Country;
31 import eu.ehri.project.models.EntityClass;
32 import eu.ehri.project.models.Repository;
33 import eu.ehri.project.models.base.Actioner;
34 import eu.ehri.project.models.base.PermissionScope;
35 import eu.ehri.project.persistence.Bundle;
36 import eu.ehri.project.persistence.BundleManager;
37 import eu.ehri.project.persistence.Mutation;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import java.util.ArrayList;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.regex.Matcher;
45 import java.util.regex.Pattern;
46
47
48
49
50 public class EagImporter extends AbstractImporter<Map<String, Object>, Repository> {
51
52 private static final Logger logger = LoggerFactory.getLogger(EacImporter.class);
53 private final Pattern priorityPattern = Pattern.compile("Priority: (-?\\d+)");
54 public static final String MAINTENANCE_NOTES = "maintenanceNotes";
55 public static final String PRIORITY = "priority";
56
57
58
59
60
61
62
63
64 public EagImporter(FramedGraph<?> framedGraph, PermissionScope permissionScope, Actioner actioner, ImportLog log) {
65 super(framedGraph, permissionScope, actioner, log);
66 }
67
68 @Override
69 public Repository importItem(Map<String, Object> itemData, List<String> idPath) throws
70 ValidationError {
71 return importItem(itemData);
72 }
73
74 public Map<String, Object> extractUnit(Map<String, Object> itemData) throws ValidationError {
75 Map<String, Object> data = ImportHelpers.extractIdentifiers(itemData);
76 data.put("typeOfEntity", itemData.get("typeOfEntity"));
77
78
79 Object notes = itemData.get(MAINTENANCE_NOTES);
80 if (notes != null) {
81 if (notes instanceof ArrayList<?>) {
82 for (Object n : (ArrayList<?>) notes) {
83 if (n instanceof String) {
84 Matcher m = priorityPattern.matcher((String) n);
85 if (m.find()) {
86 data.put(PRIORITY, Integer.parseInt(m.group(1)));
87 }
88 }
89 }
90 }
91 }
92
93 return data;
94 }
95
96
97
98
99 @Override
100 public Repository importItem(Map<String, Object> itemData) throws ValidationError {
101
102 BundleManager persister = new BundleManager(framedGraph, permissionScope.idPath());
103
104 Map<String, Object> descmap = ImportHelpers.extractDescription(itemData, EntityClass.REPOSITORY_DESCRIPTION);
105 descmap.put(Ontology.IDENTIFIER_KEY, descmap.get(Ontology.IDENTIFIER_KEY) + "#desc");
106 Bundle descBundle = Bundle.of(EntityClass.REPOSITORY_DESCRIPTION, descmap);
107
108
109
110 for (Map<String, Object> dpb : ImportHelpers.extractDates(itemData)) {
111 descBundle = descBundle.withRelation(Ontology.ENTITY_HAS_DATE, Bundle.of(EntityClass.DATE_PERIOD, dpb));
112 }
113
114
115 Map<String, Object> address = ImportHelpers.extractAddress(itemData);
116 if (!address.isEmpty()) {
117 descBundle = descBundle.withRelation(Ontology.ENTITY_HAS_ADDRESS, Bundle.of(EntityClass.ADDRESS, address));
118 }
119 Map<String, Object> unknowns = ImportHelpers.extractUnknownProperties(itemData);
120 if (!unknowns.isEmpty()) {
121 logger.debug("Unknown Properties found");
122 descBundle = descBundle.withRelation(Ontology.HAS_UNKNOWN_PROPERTY, Bundle.of(EntityClass.UNKNOWN_PROPERTY, unknowns));
123 }
124 for (Map<String, Object> dpb : ImportHelpers.extractSubNodes(Entities.MAINTENANCE_EVENT, itemData)) {
125 logger.debug("maintenance event found");
126
127 descBundle = descBundle.withRelation(Ontology.HAS_MAINTENANCE_EVENT, Bundle.of(EntityClass.MAINTENANCE_EVENT, dpb));
128 }
129
130 Bundle unit = Bundle.of(EntityClass.REPOSITORY, extractUnit(itemData))
131 .withRelation(Ontology.DESCRIPTION_FOR_ENTITY, descBundle);
132
133 Mutation<Repository> mutation = persister.createOrUpdate(unit, Repository.class);
134 handleCallbacks(mutation);
135
136 if (mutation.created()) {
137 mutation.getNode().setCountry(framedGraph.frame(permissionScope.asVertex(), Country.class));
138 mutation.getNode().setPermissionScope(permissionScope);
139 }
140 return mutation.getNode();
141 }
142 }