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.base;
21
22 import com.google.common.collect.Iterables;
23 import com.google.common.collect.Lists;
24 import com.tinkerpop.frames.FramedGraph;
25 import eu.ehri.project.core.GraphManager;
26 import eu.ehri.project.core.GraphManagerFactory;
27 import eu.ehri.project.exceptions.ValidationError;
28 import eu.ehri.project.importers.ErrorCallback;
29 import eu.ehri.project.importers.ImportCallback;
30 import eu.ehri.project.importers.ImportLog;
31 import eu.ehri.project.models.base.Accessible;
32 import eu.ehri.project.models.base.Actioner;
33 import eu.ehri.project.models.base.PermissionScope;
34 import eu.ehri.project.persistence.BundleManager;
35 import eu.ehri.project.persistence.Mutation;
36
37 import java.util.List;
38
39 public abstract class AbstractImporter<I, T extends Accessible> implements ItemImporter<I, T> {
40
41 protected final PermissionScope permissionScope;
42 protected final Actioner actioner;
43 protected final FramedGraph<?> framedGraph;
44 protected final GraphManager manager;
45 protected final ImportLog log;
46 private final List<ImportCallback> callbacks = Lists.newArrayList();
47 private final List<ErrorCallback> errorCallbacks = Lists.newArrayList();
48
49
50
51
52
53
54 protected void handleCallbacks(Mutation<? extends Accessible> mutation) {
55 for (ImportCallback callback : callbacks) {
56 callback.itemImported(mutation);
57 }
58 }
59
60 public PermissionScope getPermissionScope() {
61 return permissionScope;
62 }
63
64 public Actioner getActioner() {
65 return actioner;
66 }
67
68 protected BundleManager getPersister(List<String> scopeIds) {
69 return new BundleManager(framedGraph,
70 Lists.newArrayList(Iterables.concat(permissionScope.idPath(), scopeIds)));
71 }
72
73 public BundleManager getPersister() {
74 return new BundleManager(framedGraph, permissionScope.idPath());
75 }
76
77
78
79
80
81
82
83
84
85 public AbstractImporter(FramedGraph<?> graph, PermissionScope scope, Actioner actioner, ImportLog log) {
86 this.permissionScope = scope;
87 this.framedGraph = graph;
88 this.actioner = actioner;
89 this.log = log;
90 manager = GraphManagerFactory.getInstance(graph);
91 }
92
93 @Override
94 public void addCallback(ImportCallback callback) {
95 callbacks.add(callback);
96 }
97
98 @Override
99 public void addErrorCallback(ErrorCallback errorCallback) {
100 errorCallbacks.add(errorCallback);
101 }
102
103 @Override
104 public void handleError(Exception ex) {
105 for (ErrorCallback errorCallback: errorCallbacks) {
106 errorCallback.itemError(ex);
107 }
108 }
109 }