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  /*
21   * To change this template, choose Tools | Templates
22   * and open the template in the editor.
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   * Importer of authoritative items such as historical agents and concepts, loaded
51   * from a CSV file.
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          //unit needs at least IDENTIFIER_KEY
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 }