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.eag;
21  
22  import com.tinkerpop.frames.FramedGraph;
23  import eu.ehri.project.definitions.Entities;
24  import eu.ehri.project.definitions.Ontology;
25  import eu.ehri.project.exceptions.ValidationError;
26  import eu.ehri.project.importers.ImportLog;
27  import eu.ehri.project.importers.base.AbstractImporter;
28  import eu.ehri.project.importers.eac.EacImporter;
29  import eu.ehri.project.importers.util.ImportHelpers;
30  import eu.ehri.project.models.Country;
31  import eu.ehri.project.models.EntityClass;
32  import eu.ehri.project.models.Repository;
33  import eu.ehri.project.models.base.Actioner;
34  import eu.ehri.project.models.base.PermissionScope;
35  import eu.ehri.project.persistence.Bundle;
36  import eu.ehri.project.persistence.BundleManager;
37  import eu.ehri.project.persistence.Mutation;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  import java.util.Map;
44  import java.util.regex.Matcher;
45  import java.util.regex.Pattern;
46  
47  /**
48   * Importer of EAG-based descriptions.
49   */
50  public class EagImporter extends AbstractImporter<Map<String, Object>, Repository> {
51  
52      private static final Logger logger = LoggerFactory.getLogger(EacImporter.class);
53      private final Pattern priorityPattern = Pattern.compile("Priority: (-?\\d+)");
54      public static final String MAINTENANCE_NOTES = "maintenanceNotes";
55      public static final String PRIORITY = "priority";
56  
57      /**
58       * Construct an EagImporter object.
59       *
60       * @param framedGraph     The graph instance
61       * @param permissionScope A permission scope, e.g. a country
62       * @param log             An import log instance
63       */
64      public EagImporter(FramedGraph<?> framedGraph, PermissionScope permissionScope, Actioner actioner, ImportLog log) {
65          super(framedGraph, permissionScope, actioner, log);
66      }
67  
68      @Override
69      public Repository importItem(Map<String, Object> itemData, List<String> idPath) throws
70              ValidationError {
71          return importItem(itemData);
72      }
73  
74      public Map<String, Object> extractUnit(Map<String, Object> itemData) throws ValidationError {
75          Map<String, Object> data = ImportHelpers.extractIdentifiers(itemData);
76          data.put("typeOfEntity", itemData.get("typeOfEntity"));
77          // MB: Hack hack hack - extract EHRI-specific 'priority' field out of the
78          // pattern "Priority: <digit>" in the maintenanceNotes field.
79          Object notes = itemData.get(MAINTENANCE_NOTES);
80          if (notes != null) {
81              if (notes instanceof ArrayList<?>) {
82                  for (Object n : (ArrayList<?>) notes) {
83                      if (n instanceof String) {
84                          Matcher m = priorityPattern.matcher((String) n);
85                          if (m.find()) {
86                              data.put(PRIORITY, Integer.parseInt(m.group(1)));
87                          }
88                      }
89                  }
90              }
91          }
92  
93          return data;
94      }
95  
96      /**
97       * @param itemData A data tree
98       */
99      @Override
100     public Repository importItem(Map<String, Object> itemData) throws ValidationError {
101 
102         BundleManager persister = new BundleManager(framedGraph, permissionScope.idPath());
103 
104         Map<String, Object> descmap = ImportHelpers.extractDescription(itemData, EntityClass.REPOSITORY_DESCRIPTION);
105         descmap.put(Ontology.IDENTIFIER_KEY, descmap.get(Ontology.IDENTIFIER_KEY) + "#desc");
106         Bundle descBundle = Bundle.of(EntityClass.REPOSITORY_DESCRIPTION, descmap);
107 
108         // Add dates and descriptions to the bundle since they're @Dependent
109         // relations.
110         for (Map<String, Object> dpb : ImportHelpers.extractDates(itemData)) {
111             descBundle = descBundle.withRelation(Ontology.ENTITY_HAS_DATE, Bundle.of(EntityClass.DATE_PERIOD, dpb));
112         }
113 
114         //add the address to the description bundle
115         Map<String, Object> address = ImportHelpers.extractAddress(itemData);
116         if (!address.isEmpty()) {
117             descBundle = descBundle.withRelation(Ontology.ENTITY_HAS_ADDRESS, Bundle.of(EntityClass.ADDRESS, address));
118         }
119         Map<String, Object> unknowns = ImportHelpers.extractUnknownProperties(itemData);
120         if (!unknowns.isEmpty()) {
121             logger.debug("Unknown Properties found");
122             descBundle = descBundle.withRelation(Ontology.HAS_UNKNOWN_PROPERTY, Bundle.of(EntityClass.UNKNOWN_PROPERTY, unknowns));
123         }
124         for (Map<String, Object> dpb : ImportHelpers.extractSubNodes(Entities.MAINTENANCE_EVENT, itemData)) {
125             logger.debug("maintenance event found");
126             //dates in maintenanceEvents are no DatePeriods, they are not something to search on
127             descBundle = descBundle.withRelation(Ontology.HAS_MAINTENANCE_EVENT, Bundle.of(EntityClass.MAINTENANCE_EVENT, dpb));
128         }
129 
130         Bundle unit = Bundle.of(EntityClass.REPOSITORY, extractUnit(itemData))
131                 .withRelation(Ontology.DESCRIPTION_FOR_ENTITY, descBundle);
132 
133         Mutation<Repository> mutation = persister.createOrUpdate(unit, Repository.class);
134         handleCallbacks(mutation);
135 
136         if (mutation.created()) {
137             mutation.getNode().setCountry(framedGraph.frame(permissionScope.asVertex(), Country.class));
138             mutation.getNode().setPermissionScope(permissionScope);
139         }
140         return mutation.getNode();
141     }
142 }