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.csv;
21
22 import com.google.common.collect.Lists;
23 import com.google.common.collect.Maps;
24 import com.tinkerpop.frames.FramedGraph;
25 import eu.ehri.project.acl.SystemScope;
26 import eu.ehri.project.definitions.Ontology;
27 import eu.ehri.project.exceptions.ValidationError;
28 import eu.ehri.project.importers.ImportLog;
29 import eu.ehri.project.importers.base.AbstractImporter;
30 import eu.ehri.project.importers.properties.XmlImportProperties;
31 import eu.ehri.project.importers.util.ImportHelpers;
32 import eu.ehri.project.models.EntityClass;
33 import eu.ehri.project.models.HistoricalAgent;
34 import eu.ehri.project.models.base.Actioner;
35 import eu.ehri.project.models.base.PermissionScope;
36 import eu.ehri.project.models.cvoc.AuthoritativeSet;
37 import eu.ehri.project.persistence.Bundle;
38 import eu.ehri.project.persistence.BundleManager;
39 import eu.ehri.project.persistence.Mutation;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import java.util.List;
44 import java.util.Map;
45
46
47
48
49
50
51 public abstract class Wp2PersonalitiesImporter extends AbstractImporter<Map<String, Object>, HistoricalAgent> {
52
53 private final XmlImportProperties p = new XmlImportProperties("wp2personalities.properties");
54
55 private static final Logger logger = LoggerFactory.getLogger(Wp2PersonalitiesImporter.class);
56
57 public Wp2PersonalitiesImporter(FramedGraph<?> framedGraph, Actioner actioner, PermissionScope permissionScope,
58 ImportLog log) {
59 super(framedGraph, permissionScope, actioner, log);
60 }
61
62 @Override
63 public HistoricalAgent importItem(Map<String, Object> itemData) throws ValidationError {
64 Bundle descBundle = Bundle.of(EntityClass.HISTORICAL_AGENT_DESCRIPTION,
65 extractUnitDescription(itemData, EntityClass.HISTORICAL_AGENT_DESCRIPTION));
66 for (Map<String, Object> dpb : extractDates(itemData)) {
67 descBundle = descBundle.withRelation(Ontology.ENTITY_HAS_DATE, Bundle.of(EntityClass.DATE_PERIOD, dpb));
68 }
69 Bundle unit = Bundle.of(EntityClass.HISTORICAL_AGENT, extractUnit(itemData))
70 .withRelation(Ontology.DESCRIPTION_FOR_ENTITY, descBundle);
71
72 BundleManager persister = getPersister();
73 Mutation<HistoricalAgent> mutation = persister.createOrUpdate(unit, HistoricalAgent.class);
74 HistoricalAgent frame = mutation.getNode();
75
76 if (!permissionScope.equals(SystemScope.getInstance()) && mutation.created()) {
77 permissionScope.as(AuthoritativeSet.class).addItem(frame);
78 frame.setPermissionScope(permissionScope);
79 }
80
81 handleCallbacks(mutation);
82 return frame;
83 }
84
85 public HistoricalAgent importItem(Map<String, Object> itemData, int depth) throws ValidationError {
86 throw new UnsupportedOperationException("Not supported ever.");
87 }
88
89 private Map<String, Object> extractUnit(Map<String, Object> itemData) {
90
91 Map<String, Object> item = Maps.newHashMap();
92 if (itemData.containsKey("id")) {
93 item.put(Ontology.IDENTIFIER_KEY, itemData.get("id"));
94 } else {
95 logger.error("missing objectIdentifier");
96 }
97 return item;
98 }
99
100 private Map<String, Object> extractUnitDescription(Map<String, Object> itemData, EntityClass entityClass) {
101 Map<String, Object> item = Maps.newHashMap();
102
103 for (String key : itemData.keySet()) {
104 if (!key.equals("id")) {
105 if (!p.containsProperty(key)) {
106 ImportHelpers.putPropertyInGraph(item, ImportHelpers.UNKNOWN_PREFIX + key, itemData.get(key).toString());
107 } else {
108 ImportHelpers.putPropertyInGraph(item, p.getProperty(key), itemData.get(key).toString());
109 }
110 }
111
112 }
113
114 if (!item.containsKey("typeOfEntity")) {
115 ImportHelpers.putPropertyInGraph(item, "typeOfEntity", "person");
116 }
117 if (!item.containsKey(Ontology.LANGUAGE_OF_DESCRIPTION)) {
118 ImportHelpers.putPropertyInGraph(item, Ontology.LANGUAGE_OF_DESCRIPTION, "en");
119 }
120 return item;
121 }
122
123
124
125
126
127 public List<Map<String, Object>> extractDates(Map<String, Object> itemData) {
128
129 List<Map<String, Object>> l = Lists.newArrayList();
130 Map<String, Object> items = Maps.newHashMap();
131
132 String start = (String) itemData.get("dateOfBirth");
133
134 if (start != null && start.endsWith("00-00")) {
135 start = start.substring(0, 4);
136 }
137 if (start != null) {
138 items.put(Ontology.DATE_PERIOD_START_DATE, start);
139 l.add(items);
140 }
141 return l;
142 }
143 }