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.Maps;
23  import com.tinkerpop.frames.FramedGraph;
24  import eu.ehri.project.acl.SystemScope;
25  import eu.ehri.project.definitions.Ontology;
26  import eu.ehri.project.exceptions.ValidationError;
27  import eu.ehri.project.importers.ImportLog;
28  import eu.ehri.project.importers.base.AbstractImporter;
29  import eu.ehri.project.importers.properties.XmlImportProperties;
30  import eu.ehri.project.importers.util.ImportHelpers;
31  import eu.ehri.project.models.DocumentaryUnit;
32  import eu.ehri.project.models.EntityClass;
33  import eu.ehri.project.models.Repository;
34  import eu.ehri.project.models.base.AbstractUnit;
35  import eu.ehri.project.models.base.Actioner;
36  import eu.ehri.project.models.base.PermissionScope;
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 Ukrainian units, which are encoded in a slightly different way
48   * than other documentary units.
49   */
50  public class UkrainianUnitImporter extends AbstractImporter<Map<String, Object>, AbstractUnit> {
51  
52      private static final String MULTIVALUE_SEP = ",,";
53      private final XmlImportProperties p;
54      private static final Logger logger = LoggerFactory.getLogger(UkrainianUnitImporter.class);
55  
56      public UkrainianUnitImporter(FramedGraph<?> framedGraph, PermissionScope permissionScope,
57              Actioner actioner, ImportLog log) {
58          super(framedGraph, permissionScope, actioner, log);
59          p = new XmlImportProperties("ukraine.properties");
60      }
61  
62      @Override
63      public AbstractUnit importItem(Map<String, Object> itemData) throws ValidationError {
64  
65          BundleManager persister = new BundleManager(framedGraph, permissionScope.idPath());
66  
67          logger.debug("-----------------------------------");
68          Bundle unit = Bundle.of(EntityClass.DOCUMENTARY_UNIT, extractUnit(itemData));
69          Map<String, Object> unknowns = extractUnknownProperties(itemData);
70  
71          String lang = itemData.get("language_of_description").toString();
72          if (lang.indexOf(", ") > 0) {
73              String[] langs = lang.split(", ");
74              for (String l : langs) {
75                  Bundle descBundle = Bundle.of(EntityClass.DOCUMENTARY_UNIT_DESCRIPTION, extractUnitDescription(itemData, l));
76                  descBundle = descBundle.withRelation(Ontology.ENTITY_HAS_DATE, Bundle.of(EntityClass.DATE_PERIOD, constructDateMap(itemData)));
77                  if (!unknowns.isEmpty()) {
78                      descBundle = descBundle.withRelation(Ontology.HAS_UNKNOWN_PROPERTY, Bundle.of(EntityClass.UNKNOWN_PROPERTY, unknowns));
79                  }
80                  unit = unit.withRelation(Ontology.DESCRIPTION_FOR_ENTITY, descBundle);
81              }
82          } else {
83              Bundle descBundle = Bundle.of(EntityClass.DOCUMENTARY_UNIT_DESCRIPTION, extractUnitDescription(itemData, lang));
84              descBundle = descBundle.withRelation(Ontology.ENTITY_HAS_DATE, Bundle.of(EntityClass.DATE_PERIOD, constructDateMap(itemData)));
85              if (!unknowns.isEmpty()) {
86                  descBundle = descBundle.withRelation(Ontology.HAS_UNKNOWN_PROPERTY, Bundle.of(EntityClass.UNKNOWN_PROPERTY, unknowns));
87              }
88  
89              unit = unit.withRelation(Ontology.DESCRIPTION_FOR_ENTITY, descBundle);
90          }
91  
92          Mutation<DocumentaryUnit> mutation = persister
93                  .createOrUpdate(unit, DocumentaryUnit.class);
94          DocumentaryUnit frame = mutation.getNode();
95          if (!permissionScope.equals(SystemScope.getInstance())
96                  && mutation.created()) {
97              frame.setRepository(framedGraph.frame(permissionScope.asVertex(), Repository.class));
98              frame.setPermissionScope(permissionScope);
99          }
100 
101         handleCallbacks(mutation);
102         return frame;
103 
104     }
105 
106     private Map<String, Object> extractUnknownProperties(Map<String, Object> itemData) throws ValidationError {
107         Map<String, Object> unknowns = Maps.newHashMap();
108         for (String key : itemData.keySet()) {
109             if (p.getProperty(key).equals("UNKNOWN")) {
110                 unknowns.put(key, itemData.get(key));
111             }
112         }
113         return unknowns;
114     }
115 
116 
117     @Override
118     public AbstractUnit importItem(Map<String, Object> itemData, List<String> scopeIds) throws
119             ValidationError {
120         throw new UnsupportedOperationException("Not supported ever.");
121     }
122 
123     private Map<String, Object> extractUnit(Map<String, Object> itemData) {
124         //unit needs at least IDENTIFIER_KEY
125         Map<String, Object> item = Maps.newHashMap();
126         putIfNotEmpty(item, "scope", itemData.get(p.getFirstPropertyWithValue("scope")));
127         putIfNotEmpty(item, "priority", itemData.get(p.getFirstPropertyWithValue("priority")));
128         putIfNotEmpty(item, "copyrightStatus", itemData.get(p.getFirstPropertyWithValue("copyrightStatus")));
129         if (itemData.containsKey("identifier")) {
130             item.put(Ontology.IDENTIFIER_KEY, itemData.get("identifier"));
131         } else {
132             logger.error("missing identifier");
133         }
134         return item;
135     }
136 
137     private void putIfNotEmpty(Map<String, Object> item, String key, Object value) {
138         if (value != null && !value.toString().isEmpty()) {
139             item.put(key, value);
140         }
141     }
142 
143     public Map<String, Object> constructDateMap(Map<String, Object> itemData) {
144         Map<String, Object> item = Maps.newHashMap();
145         String origDate = itemData.get("dates").toString();
146         if (origDate.indexOf(MULTIVALUE_SEP) > 0) {
147             String[] dates = itemData.get("dates").toString().split(MULTIVALUE_SEP);
148             item.put(Ontology.DATE_PERIOD_START_DATE, dates[0]);
149             item.put(Ontology.DATE_PERIOD_END_DATE, dates[1]);
150         } else {
151             item.put(Ontology.DATE_PERIOD_START_DATE, origDate);
152             item.put(Ontology.DATE_PERIOD_END_DATE, origDate);
153         }
154         return item;
155     }
156 
157     private Map<String, Object> extractUnitDescription(Map<String, Object> itemData, String language) {
158         Map<String, Object> item = Maps.newHashMap();
159 
160         for (String key : itemData.keySet()) {
161             if ((!key.equals("identifier")) &&
162                     !(p.getProperty(key).equals("IGNORE")) &&
163                     !(p.getProperty(key).equals("UNKNOWN")) &&
164                     (!key.equals("dates")) &&
165                     (!p.getProperty(key).equals("scope")) && //on the unit
166                     (!p.getProperty(key).equals("copyrightStatus")) && // on the unit
167                     (!p.getProperty(key).equals("priority")) && // on the unit
168                     (!key.equals("language_of_description"))  //dealt with in importItem
169                     ) {
170                 if (!p.containsProperty(key)) {
171                     ImportHelpers.putPropertyInGraph(item, ImportHelpers.UNKNOWN_PREFIX + key, itemData.get(key).toString());
172                 } else {
173                     Object value = itemData.get(key);
174                     // TODO: Check if the property is an allowedMultivalue one...
175                     if (value.toString().contains(MULTIVALUE_SEP)) {
176                         for (String v : value.toString().split(MULTIVALUE_SEP)) {
177                             ImportHelpers.putPropertyInGraph(item, p.getProperty(key), v);
178                         }
179                     } else {
180                         ImportHelpers.putPropertyInGraph(item, p.getProperty(key), value.toString());
181                     }
182                 }
183             }
184 
185         }
186         //replace the language from the itemData with the one specified in the param
187         ImportHelpers.putPropertyInGraph(item, Ontology.LANGUAGE_OF_DESCRIPTION, language);
188         return item;
189     }
190 }