View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
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       * Call all registered ImportCallbacks for the given mutation.
51       *
52       * @param mutation the Mutation to handle callbacks for
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       * Constructor.
79       *
80       * @param graph    the framed graph
81       * @param scope    the permission scope
82       * @param actioner the user performing the import
83       * @param log      the log object
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 }