1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package eu.ehri.project.importers.csv;
25
26 import com.google.common.collect.Maps;
27 import com.tinkerpop.frames.FramedGraph;
28 import eu.ehri.project.acl.SystemScope;
29 import eu.ehri.project.definitions.Ontology;
30 import eu.ehri.project.exceptions.ValidationError;
31 import eu.ehri.project.importers.ImportLog;
32 import eu.ehri.project.importers.base.AbstractImporter;
33 import eu.ehri.project.importers.util.ImportHelpers;
34 import eu.ehri.project.models.EntityClass;
35 import eu.ehri.project.models.base.Actioner;
36 import eu.ehri.project.models.base.Description;
37 import eu.ehri.project.models.base.PermissionScope;
38 import eu.ehri.project.models.cvoc.AuthoritativeItem;
39 import eu.ehri.project.models.cvoc.AuthoritativeSet;
40 import eu.ehri.project.persistence.Bundle;
41 import eu.ehri.project.persistence.BundleManager;
42 import eu.ehri.project.persistence.Mutation;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import java.util.List;
47 import java.util.Map;
48
49
50
51
52
53 public class CsvAuthoritativeItemImporter extends AbstractImporter<Map<String, Object>, AuthoritativeItem> {
54
55 private static final Logger logger = LoggerFactory.getLogger(CsvAuthoritativeItemImporter.class);
56
57 public CsvAuthoritativeItemImporter(FramedGraph<?> framedGraph, PermissionScope permissionScope,
58 Actioner actioner, ImportLog log) {
59 super(framedGraph, permissionScope, actioner, log);
60 }
61
62 @Override
63 public AuthoritativeItem importItem(Map<String, Object> itemData) throws ValidationError {
64
65 BundleManager persister = getPersister();
66 Bundle descBundle = Bundle.of(EntityClass.HISTORICAL_AGENT_DESCRIPTION,
67 extractUnitDescription(itemData, EntityClass.HISTORICAL_AGENT_DESCRIPTION));
68 Bundle unit = Bundle.of(EntityClass.HISTORICAL_AGENT, extractUnit(itemData))
69 .withRelation(Ontology.DESCRIPTION_FOR_ENTITY, descBundle);
70
71 Mutation<AuthoritativeItem> mutation = persister.createOrUpdate(unit, AuthoritativeItem.class);
72 AuthoritativeItem frame = mutation.getNode();
73
74 if (!permissionScope.equals(SystemScope.getInstance()) && mutation.created()) {
75 permissionScope.as(AuthoritativeSet.class).addItem(frame);
76 frame.setPermissionScope(permissionScope);
77 }
78
79 handleCallbacks(mutation);
80 return frame;
81 }
82
83 @Override
84 public AuthoritativeItem importItem(Map<String, Object> itemData, List<String> idPath) throws
85 ValidationError {
86 throw new UnsupportedOperationException("Not supported ever.");
87 }
88
89 protected 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 protected Map<String, Object> extractUnitDescription(Map<String, Object> itemData, EntityClass entityClass) {
101 Map<String, Object> item = Maps.newHashMap();
102 item.put(Ontology.CREATION_PROCESS, Description.CreationProcess.IMPORT.toString());
103
104 ImportHelpers.putPropertyInGraph(item, Ontology.NAME_KEY, itemData.get("name").toString());
105 for (String key : itemData.keySet()) {
106 if (!key.equals("id") && !key.equals("name")) {
107 ImportHelpers.putPropertyInGraph(item, key, itemData.get(key).toString());
108 }
109 }
110 if (!item.containsKey("typeOfEntity")) {
111 ImportHelpers.putPropertyInGraph(item, "typeOfEntity", "subject");
112 }
113 if (!item.containsKey(Ontology.LANGUAGE_OF_DESCRIPTION)) {
114 ImportHelpers.putPropertyInGraph(item, Ontology.LANGUAGE_OF_DESCRIPTION, "en");
115 }
116 return item;
117 }
118 }