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.Description;
36  import eu.ehri.project.models.base.PermissionScope;
37  import eu.ehri.project.models.cvoc.AuthoritativeSet;
38  import eu.ehri.project.persistence.Bundle;
39  import eu.ehri.project.persistence.BundleManager;
40  import eu.ehri.project.persistence.Mutation;
41  import org.slf4j.Logger;
42  import org.slf4j.LoggerFactory;
43  
44  import java.util.List;
45  import java.util.Map;
46  
47  /**
48   * Importer of historical agents encoded in {@link Map}s.
49   * <p>
50   * Before importing the file: delete the columns with the reordering of the first and last name
51   * add a column 'id' with a unique identifier, prefixed with EHRI-Personalities or some such.
52   */
53  public class PersonalitiesImporter extends AbstractImporter<Map<String, Object>, HistoricalAgent> {
54  
55      private final XmlImportProperties p = new XmlImportProperties("personalities.properties");
56  
57      private static final Logger logger = LoggerFactory.getLogger(PersonalitiesImporter.class);
58  
59      public PersonalitiesImporter(FramedGraph<?> framedGraph, PermissionScope permissionScope, Actioner actioner,
60              ImportLog log) {
61          super(framedGraph, permissionScope, actioner, log);
62      }
63  
64      @Override
65      public HistoricalAgent importItem(Map<String, Object> itemData) throws ValidationError {
66  
67          BundleManager persister = getPersister();
68          Bundle descBundle = Bundle.of(EntityClass.HISTORICAL_AGENT_DESCRIPTION,
69                  extractUnitDescription(itemData, EntityClass.HISTORICAL_AGENT_DESCRIPTION));
70          for (Map<String, Object> dpb : extractDates(itemData)) {
71              descBundle = descBundle.withRelation(Ontology.ENTITY_HAS_DATE, Bundle.of(EntityClass.DATE_PERIOD, dpb));
72          }
73  
74          Bundle unit = Bundle.of(EntityClass.HISTORICAL_AGENT, extractUnit(itemData))
75              .withRelation(Ontology.DESCRIPTION_FOR_ENTITY, descBundle);
76  
77          Mutation<HistoricalAgent> mutation = persister.createOrUpdate(unit, HistoricalAgent.class);
78          HistoricalAgent frame = mutation.getNode();
79  
80          if (!permissionScope.equals(SystemScope.getInstance()) && mutation.created()) {
81              permissionScope.as(AuthoritativeSet.class).addItem(frame);
82              frame.setPermissionScope(permissionScope);
83          }
84  
85          handleCallbacks(mutation);
86          return frame;
87      }
88  
89      @Override
90      public HistoricalAgent importItem(Map<String, Object> itemData, List<String> idPath) throws
91              ValidationError {
92          throw new UnsupportedOperationException("Not supported ever.");
93      }
94  
95      private Map<String, Object> extractUnit(Map<String, Object> itemData) {
96          //unit needs at least IDENTIFIER_KEY
97          Map<String, Object> item = Maps.newHashMap();
98          if (itemData.containsKey("id")) {
99              item.put(Ontology.IDENTIFIER_KEY, itemData.get("id"));
100         } else {
101             logger.error("missing objectIdentifier");
102         }
103         return item;
104     }
105 
106     private String getName(Map<String, Object> itemData) {
107         // FIXME: This all sucks
108         String firstName = (String) itemData.get("Firstname");
109         String lastName = (String) itemData.get("Lastname");
110         if (firstName == null && lastName == null) {
111             return null;
112         }
113         String name = "";
114         if (lastName != null) {
115             name = lastName;
116         }
117         if (firstName != null) {
118             name = firstName + " " + name;
119         }
120         return name;
121     }
122 
123     private Map<String, Object> extractUnitDescription(Map<String, Object> itemData, EntityClass entityClass) {
124         Map<String, Object> item = Maps.newHashMap();
125         item.put(Ontology.CREATION_PROCESS, Description.CreationProcess.IMPORT.toString());
126 
127 
128         ImportHelpers.putPropertyInGraph(item, Ontology.NAME_KEY, getName(itemData));
129         for (String key : itemData.keySet()) {
130             if (!key.equals("id")) {
131                 if (!p.containsProperty(key)) {
132                     ImportHelpers.putPropertyInGraph(item, ImportHelpers.UNKNOWN_PREFIX + key, itemData.get(key).toString());
133                 } else {
134                     ImportHelpers.putPropertyInGraph(item, p.getProperty(key), itemData.get(key).toString());
135                 }
136             }
137 
138         }
139         //create all otherFormsOfName
140         if (!item.containsKey("typeOfEntity")) {
141             ImportHelpers.putPropertyInGraph(item, "typeOfEntity", "person");
142         }
143         if (!item.containsKey(Ontology.LANGUAGE_OF_DESCRIPTION)) {
144             ImportHelpers.putPropertyInGraph(item, Ontology.LANGUAGE_OF_DESCRIPTION, "en");
145         }
146         return item;
147     }
148 
149     /**
150      * @param itemData the item data map
151      * @return returns a List with Maps with DatePeriod.DATE_PERIOD_START_DATE and DatePeriod.DATE_PERIOD_END_DATE values
152      */
153     public List<Map<String, Object>> extractDates(Map<String, Object> itemData) {
154 
155         List<Map<String, Object>> l = Lists.newArrayList();
156         Map<String, Object> items = Maps.newHashMap();
157 
158         String end = (String) itemData.get("DateofdeathYYYY-MM-DD");
159         String start = (String) itemData.get("DateofbirthYYYY-MM-DD");
160 
161         if (start != null && start.endsWith("00-00")) {
162             start = start.substring(0, 4);
163         }
164         if (end != null && end.endsWith("00-00")) {
165             end = end.substring(0, 4);
166         }
167         if (end != null || start != null) {
168             if (start != null)
169                 items.put(Ontology.DATE_PERIOD_START_DATE, start);
170             if (end != null)
171                 items.put(Ontology.DATE_PERIOD_END_DATE, end);
172             l.add(items);
173         }
174         return l;
175     }
176 }