adapt app-import to v5.45
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / app / cyprus / CyprusAltitudeActivator.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.app.cyprus;
11
12 import java.io.FileNotFoundException;
13 import java.net.URISyntaxException;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import org.apache.commons.lang3.StringUtils;
21 import org.apache.logging.log4j.LogManager;
22 import org.apache.logging.log4j.Logger;
23 import org.springframework.transaction.TransactionStatus;
24
25 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
26 import eu.etaxonomy.cdm.app.common.CdmDestinations;
27 import eu.etaxonomy.cdm.common.ExcelUtils;
28 import eu.etaxonomy.cdm.common.URI;
29 import eu.etaxonomy.cdm.database.DbSchemaValidation;
30 import eu.etaxonomy.cdm.database.ICdmDataSource;
31 import eu.etaxonomy.cdm.io.api.application.CdmIoApplicationController;
32 import eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK;
33 import eu.etaxonomy.cdm.model.common.CdmBase;
34 import eu.etaxonomy.cdm.model.description.Feature;
35 import eu.etaxonomy.cdm.model.description.MeasurementUnit;
36 import eu.etaxonomy.cdm.model.description.QuantitativeData;
37 import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
38 import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
39 import eu.etaxonomy.cdm.model.description.TaxonDescription;
40 import eu.etaxonomy.cdm.model.name.TaxonName;
41 import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
42 import eu.etaxonomy.cdm.model.reference.Reference;
43 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
44 import eu.etaxonomy.cdm.model.taxon.Synonym;
45 import eu.etaxonomy.cdm.model.taxon.Taxon;
46 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
47
48 /**
49 * @author a.mueller
50 * @since 16.12.2010
51 */
52 public class CyprusAltitudeActivator {
53
54 private static final Logger logger = LogManager.getLogger();
55
56 //database validation status (create, update, validate ...)
57 static DbSchemaValidation hbm2dll = DbSchemaValidation.VALIDATE;
58 static final URI source = cyprus_altitude();
59
60
61 static final ICdmDataSource cdmDestination = CdmDestinations.localH2();
62 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_local_mysql_test();
63 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_test_cyprus();
64 // static final ICdmDataSource cdmDestination = CdmDestinations.cdm_cyprus_production();
65
66
67 //feature tree uuid
68 public static final UUID featureTreeUuid = UUID.fromString("14d1e912-5ec2-4d10-878b-828788b70a87");
69
70 //classification
71 static final UUID classificationUuid = UUID.fromString("0c2b5d25-7b15-4401-8b51-dd4be0ee5cab");
72
73 private static final String sourceReferenceTitle = "Cyprus Excel Altitude Import";
74
75 private static final UUID uuidAltitudeFeature = Feature.uuidAltitude;
76
77 //check - import
78 static final CHECK check = CHECK.IMPORT_WITHOUT_CHECK;
79
80 private void doImport(ICdmDataSource cdmDestination){
81
82 List<Map<String, String>> excel;
83 try {
84 excel = ExcelUtils.parseXLS(source, "coreTax");
85 } catch (FileNotFoundException e) {
86 e.printStackTrace();
87 return;
88 }
89
90 CdmApplicationController app = CdmIoApplicationController.NewInstance(cdmDestination, hbm2dll);
91
92 @SuppressWarnings("rawtypes")
93 Set<TaxonBase> taxaToSave = new HashSet<>();
94
95 TransactionStatus tx = app.startTransaction();
96
97 UUID uuidMikle77 = UUID.fromString("9f5fa7ee-538b-4ae5-bd82-2a9503fea1d6");
98 UUID uuidMikle85 = UUID.fromString("994403c4-c400-413d-9a1a-8531a40bfd8c");
99
100 Reference mikle77 = app.getReferenceService().find(uuidMikle77);
101 Reference mikle85 = app.getReferenceService().find(uuidMikle85);
102
103 Feature altitudeFeature = (Feature) app.getTermService().find(uuidAltitudeFeature);
104 if (altitudeFeature == null){
105 throw new RuntimeException("Could not find altitudinal range feature");
106 }
107
108 MeasurementUnit meter = (MeasurementUnit)app.getTermService().find(UUID.fromString("8bef5055-789c-41e5-bea2-8dc2ea8ecdf6"));
109
110 int count =1;
111 for (Map<String, String> row : excel){
112 count++;
113 UUID baseUuid = makeUuid(row, "uuid");
114 UUID acceptedUuid = makeUuid(row, "acceptedNameUuid");
115 UUID parentUuid = makeUuid(row, "parentUuid");
116
117 String altitudeMin = row.get("Min");
118 String altitudeMax = row.get("Max");
119 String acceptedName = row.get("AcceptedName");
120
121 String source = row.get("Source");
122
123 if (StringUtils.isBlank(altitudeMin)){
124 continue;
125 }
126
127 boolean hasAltitude = false;
128 Reference sourceRef = getSource(source, mikle77, mikle85);
129 Taxon taxon = getTaxon(app, baseUuid, acceptedUuid, parentUuid, acceptedName, count);
130 if (taxon != null){
131 TaxonDescription desc = getDescription(taxon, sourceRef);
132
133 hasAltitude = makeAltitude(altitudeMin, altitudeMax, altitudeFeature, sourceRef, desc, meter, count);
134 if (hasAltitude){
135 if(desc.getTaxon() == null){
136 taxon.addDescription(desc);
137 }
138 taxaToSave.add(taxon);
139 }else{
140 logger.warn("HasALtitude is false in " + count);
141 }
142 }else{
143 logger.warn("Taxon not recognized in line " + count);
144 }
145 }
146
147 app.getTaxonService().saveOrUpdate(taxaToSave);
148
149 app.commitTransaction(tx);
150 }
151
152
153 private Taxon getTaxon(CdmApplicationController app, UUID baseUuid, UUID acceptedUuid, UUID parentUuid, String acceptedName, int row) {
154 TaxonBase<?> base = app.getTaxonService().find(baseUuid);
155
156 Taxon result = null;
157 if (base.isInstanceOf(Taxon.class)){
158 Taxon t = CdmBase.deproxy(base, Taxon.class);
159 if (t.getTaxonNodes().size() == 1 && t.getTaxonNodes().iterator().next().getClassification().getUuid().equals(classificationUuid)){
160 result = t;
161 }else{
162 logger.warn("Base taxon (uuid) not in classification. Row: " + row + ", Taxon: " + base.getTitleCache());
163 }
164 }
165 if (result == null){
166 TaxonBase<?> accepted = app.getTaxonService().find(acceptedUuid);
167 Taxon t = CdmBase.deproxy(accepted, Taxon.class);;
168 if (t.getTaxonNodes().size() == 1 && t.getTaxonNodes().iterator().next().getClassification().getUuid().equals(classificationUuid)){
169 if (hasSynonym(t, base)){
170 result = t;
171 }else{
172 logger.warn("Synonym relation has changed somehow. Row: " + row + ", Taxon: " + base.getTitleCache());
173 }
174
175 }else{
176 logger.warn("Accepted taxon not in classification. Row: " + row + ", Taxon: " + base.getTitleCache());
177 }
178 }
179
180 if (result != null){
181 if (! result.getName().getTitleCache().equals(acceptedName)){
182 logger.warn("AcceptedName and taxon name is not equal in " + row + ".\n" +
183 " Accepted Name: " + acceptedName + ";\n" +
184 " Taxon Name: " + result.getName().getTitleCache());
185 }
186 }
187
188 return result;
189 }
190
191 private boolean hasSynonym(Taxon t, TaxonBase<?> base) {
192 if (base.isInstanceOf(Synonym.class)){
193 for (Synonym syn : t.getSynonyms()){
194 if (syn.equals(base)){
195 return true;
196 }
197 }
198 }
199 return false;
200 }
201
202 // private static final Pattern altitudePattern = Pattern.compile("\\d{1,4}(-\\d{1,4})?");
203
204
205 private boolean makeAltitude(String altitudeMin, String altitudeMax, Feature altitudeFeature,
206 Reference sourceRef, TaxonDescription desc, MeasurementUnit meter, int row) {
207
208 QuantitativeData data = QuantitativeData.NewInstance(altitudeFeature);
209
210 //Meikle
211 if (source != null){
212 TaxonName nameUsedInSource = null; //TODO
213 data.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, sourceRef, null, nameUsedInSource, null);
214 }
215 data.setUnit(meter);
216
217 Integer min = Integer.valueOf(altitudeMin);
218 StatisticalMeasurementValue minValue = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.MIN(), min);
219 data.addStatisticalValue(minValue);
220
221 Integer max = Integer.valueOf(altitudeMax);
222 StatisticalMeasurementValue maxValue = StatisticalMeasurementValue.NewInstance(StatisticalMeasure.MAX(), max);
223 data.addStatisticalValue(maxValue);
224
225 desc.addElement(data);
226 return true;
227 }
228
229 private TaxonDescription getDescription(Taxon taxon, Reference sourceRef) {
230 if (taxon != null){
231 //TODO Mikle currently does not exist as Source
232
233 TaxonDescription desc = TaxonDescription.NewInstance();
234 desc.setTitleCache("Import from " + getSourceReference().getTitleCache(), true);
235 desc.addSource(OriginalSourceType.PrimaryTaxonomicSource, null, null, sourceRef,null);
236 desc.addSource(OriginalSourceType.Import, null, null, getSourceReference(), null);
237
238 return desc;
239 }
240 return null;
241 }
242
243 private Reference getSource(String source, Reference m77, Reference m85) {
244 if(StringUtils.isNotBlank(source)){
245 if (source.equals("Meikle 1977")){
246 return m77;
247 }else if (source.equals("Meikle 1985")){
248 return m85;
249 }else{
250 logger.warn("Source not recognized: " + source);
251 }
252 }
253 return null;
254 }
255
256 private UUID makeUuid(Map<String, String> row, String colName) {
257 if (StringUtils.isBlank(row.get(colName))){
258 return null;
259 }else{
260 return UUID.fromString(row.get(colName));
261 }
262 }
263
264 Reference sourceReference;
265 private Reference getSourceReference() {
266 if (sourceReference == null){
267 sourceReference = ReferenceFactory.newGeneric();
268 sourceReference.setTitleCache(sourceReferenceTitle, true);
269 }
270 return sourceReference;
271
272 }
273
274 //Cyprus
275 public static URI cyprus_altitude() {
276 URI sourceUrl;
277 try {
278 sourceUrl = new URI("file:/F:/data/cyprus/Cyprus-altitude-import-neu.xls");
279 // sourceUrl = new URI("file:/F:/data/cyprus/Zypern-Altitude.xls");
280 return sourceUrl;
281 } catch (URISyntaxException e) {
282 e.printStackTrace();
283 return null;
284 }
285 }
286
287 public static void main(String[] args) {
288 CyprusAltitudeActivator me = new CyprusAltitudeActivator();
289 me.doImport(cdmDestination);
290 }
291
292 }