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.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   * Importer of historical agents described in WP2.
48   * before importing the file: delete the columns with the reordering of the first and last name
49   * add a column 'id' with a unique identifier, prefixed with EHRI-Personalities or some such.
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          //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     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         //create all otherFormsOfName
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      * @param itemData the item data map
125      * @return returns a List with Maps with DatePeriod.DATE_PERIOD_START_DATE and DatePeriod.DATE_PERIOD_END_DATE values
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 }