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.properties;
21  
22  import com.google.common.collect.Lists;
23  import com.google.common.collect.Maps;
24  import com.google.common.collect.Sets;
25  
26  import java.util.Arrays;
27  import java.util.List;
28  import java.util.Map;
29  import java.util.Set;
30  
31  
32  public class NodeProperties {
33  
34      public final static String NODE = "node";
35      public final static String PROPERTY = "property";
36      public final static String MULTIVALUED = "multivalued?";
37      public final static String REQUIRED = "required?";
38      public final static String HANDLERNAME = "handlerTempName";
39      private final List<String> titles;
40      public static final String SEP = ",";
41      private final Map<String, List<PropertiesRow>> p;
42      private final Set<String> allKnownNodes;
43  
44      public NodeProperties() {
45          titles = Lists.newArrayList();
46          p = Maps.newHashMap();
47          allKnownNodes = Sets.newHashSet();
48      }
49  
50      public void setTitles(String titles) {
51          this.titles.addAll(Arrays.asList(titles.split(SEP)));
52          assert (titles.contains(NODE));
53          assert (titles.contains(PROPERTY));
54          assert (titles.contains(MULTIVALUED));
55          assert (titles.contains(REQUIRED));
56          assert (titles.contains(HANDLERNAME));
57      }
58  
59      private PropertiesRow createPropertiesCheck(String row) {
60          int i = 0;
61          PropertiesRow c = new PropertiesRow();
62          for (String r : row.split(SEP)) {
63              c.add(titles.get(i), r);
64              i++;
65          }
66          return c;
67      }
68  
69      public void addRow(String row) {
70          String nodetype = row.split(SEP)[0];
71          allKnownNodes.add(nodetype);
72          if (!p.containsKey(nodetype)) {
73              p.put(nodetype, Lists.<PropertiesRow>newArrayList());
74          }
75          p.get(nodetype).add(createPropertiesCheck(row));
76      }
77  
78      private PropertiesRow getProperty(String nodetype, String property) {
79          if (!p.containsKey(nodetype)) {
80              return null;
81          }
82          List<PropertiesRow> l = p.get(nodetype);
83          for (PropertiesRow pr : l) {
84              if (property.equals(pr.get(PROPERTY))) {
85                  return pr;
86              }
87          }
88          return null;
89      }
90  
91      public boolean hasProperty(String nodetype, String property) {
92          PropertiesRow pr = getProperty(nodetype, property);
93          return pr != null;
94      }
95  
96      protected String getHandlerName(String nodetype, String property) {
97          return getProperty(nodetype, property).get(HANDLERNAME);
98      }
99  
100     public boolean isMultivaluedProperty(String nodetype, String property) {
101         return isBooleanFieldTrue(nodetype, property, MULTIVALUED);
102     }
103 
104     protected boolean isRequiredProperty(String nodetype, String property) {
105         return isBooleanFieldTrue(nodetype, property, REQUIRED);
106     }
107 
108     private boolean isBooleanFieldTrue(String nodetype, String property, String booleanField) {
109         PropertiesRow pr = getProperty(nodetype, property);
110         if (pr == null) {
111             return false;
112         }
113         return "1".equals(pr.get(booleanField));
114     }
115 
116     protected Set<String> getRequiredProperties(String nodetype) {
117         List<PropertiesRow> parentrows = getReferencedProperties(nodetype);
118         if (parentrows == null || p.get(nodetype) == null)
119             return null;
120         parentrows.addAll(p.get(nodetype));
121 
122         Set<String> required = Sets.newHashSet();
123         for (PropertiesRow r : parentrows) {
124             if ("1".equals(r.get(REQUIRED))) {
125 
126                 if (r.get(HANDLERNAME) == null) {
127                     required.add(r.get(PROPERTY));
128                 } else {
129                     required.add(r.get(HANDLERNAME));
130                 }
131             }
132         }
133         return required;
134     }
135 
136     /**
137      * @param nodetype the type of node
138      * @return returns a list of properties that were referenced from this node-type, like
139      * address or description
140      */
141     private List<PropertiesRow> getReferencedProperties(String nodetype) {
142         List<PropertiesRow> referencedrows = Lists.newArrayList();
143         for (PropertiesRow prop : p.get(nodetype)) {
144             if (allKnownNodes.contains(prop.get(PROPERTY))) {
145                 for (PropertiesRow pr : p.get(prop.get(PROPERTY))) {
146                     referencedrows.add(pr.clone().add(PROPERTY, prop.get(PROPERTY) + "/" + pr.get(PROPERTY)));
147                 }
148                 referencedrows.addAll(p.get(prop.get(PROPERTY)));
149             }
150         }
151         if (nodetype.endsWith("Description")) {
152             referencedrows.addAll(p.get("Unit"));
153             referencedrows.addAll(p.get("Description"));
154         }
155         return referencedrows;
156     }
157 
158     protected Set<String> getHandlerProperties(String node) {
159         List<PropertiesRow> parentrows = getReferencedProperties(node);
160         if (parentrows == null || p.get(node) == null)
161             return null;
162         parentrows.addAll(p.get(node));
163 
164         Set<String> required = Sets.newHashSet();
165         for (PropertiesRow r : parentrows) {
166             if (r.get(HANDLERNAME) == null) {
167                 required.add(r.get(PROPERTY));
168             } else {
169                 required.add(r.get(HANDLERNAME));
170             }
171         }
172         return required;
173     }
174 }