2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.app
.cyprus
;
12 import java
.io
.FileNotFoundException
;
13 import java
.net
.URISyntaxException
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
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
;
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
;
52 public class CyprusAltitudeActivator
{
54 private static final Logger logger
= LogManager
.getLogger();
56 //database validation status (create, update, validate ...)
57 static DbSchemaValidation hbm2dll
= DbSchemaValidation
.VALIDATE
;
58 static final URI source
= cyprus_altitude();
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();
68 public static final UUID featureTreeUuid
= UUID
.fromString("14d1e912-5ec2-4d10-878b-828788b70a87");
71 static final UUID classificationUuid
= UUID
.fromString("0c2b5d25-7b15-4401-8b51-dd4be0ee5cab");
73 private static final String sourceReferenceTitle
= "Cyprus Excel Altitude Import";
75 private static final UUID uuidAltitudeFeature
= Feature
.uuidAltitude
;
78 static final CHECK check
= CHECK
.IMPORT_WITHOUT_CHECK
;
80 private void doImport(ICdmDataSource cdmDestination
){
82 List
<Map
<String
, String
>> excel
;
84 excel
= ExcelUtils
.parseXLS(source
, "coreTax");
85 } catch (FileNotFoundException e
) {
90 CdmApplicationController app
= CdmIoApplicationController
.NewInstance(cdmDestination
, hbm2dll
);
92 @SuppressWarnings("rawtypes")
93 Set
<TaxonBase
> taxaToSave
= new HashSet
<>();
95 TransactionStatus tx
= app
.startTransaction();
97 UUID uuidMikle77
= UUID
.fromString("9f5fa7ee-538b-4ae5-bd82-2a9503fea1d6");
98 UUID uuidMikle85
= UUID
.fromString("994403c4-c400-413d-9a1a-8531a40bfd8c");
100 Reference mikle77
= app
.getReferenceService().find(uuidMikle77
);
101 Reference mikle85
= app
.getReferenceService().find(uuidMikle85
);
103 Feature altitudeFeature
= (Feature
) app
.getTermService().find(uuidAltitudeFeature
);
104 if (altitudeFeature
== null){
105 throw new RuntimeException("Could not find altitudinal range feature");
108 MeasurementUnit meter
= (MeasurementUnit
)app
.getTermService().find(UUID
.fromString("8bef5055-789c-41e5-bea2-8dc2ea8ecdf6"));
111 for (Map
<String
, String
> row
: excel
){
113 UUID baseUuid
= makeUuid(row
, "uuid");
114 UUID acceptedUuid
= makeUuid(row
, "acceptedNameUuid");
115 UUID parentUuid
= makeUuid(row
, "parentUuid");
117 String altitudeMin
= row
.get("Min");
118 String altitudeMax
= row
.get("Max");
119 String acceptedName
= row
.get("AcceptedName");
121 String source
= row
.get("Source");
123 if (StringUtils
.isBlank(altitudeMin
)){
127 boolean hasAltitude
= false;
128 Reference sourceRef
= getSource(source
, mikle77
, mikle85
);
129 Taxon taxon
= getTaxon(app
, baseUuid
, acceptedUuid
, parentUuid
, acceptedName
, count
);
131 TaxonDescription desc
= getDescription(taxon
, sourceRef
);
133 hasAltitude
= makeAltitude(altitudeMin
, altitudeMax
, altitudeFeature
, sourceRef
, desc
, meter
, count
);
135 if(desc
.getTaxon() == null){
136 taxon
.addDescription(desc
);
138 taxaToSave
.add(taxon
);
140 logger
.warn("HasALtitude is false in " + count
);
143 logger
.warn("Taxon not recognized in line " + count
);
147 app
.getTaxonService().saveOrUpdate(taxaToSave
);
149 app
.commitTransaction(tx
);
153 private Taxon
getTaxon(CdmApplicationController app
, UUID baseUuid
, UUID acceptedUuid
, UUID parentUuid
, String acceptedName
, int row
) {
154 TaxonBase
<?
> base
= app
.getTaxonService().find(baseUuid
);
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
)){
162 logger
.warn("Base taxon (uuid) not in classification. Row: " + row
+ ", Taxon: " + base
.getTitleCache());
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
)){
172 logger
.warn("Synonym relation has changed somehow. Row: " + row
+ ", Taxon: " + base
.getTitleCache());
176 logger
.warn("Accepted taxon not in classification. Row: " + row
+ ", Taxon: " + base
.getTitleCache());
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());
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
)){
202 // private static final Pattern altitudePattern = Pattern.compile("\\d{1,4}(-\\d{1,4})?");
205 private boolean makeAltitude(String altitudeMin
, String altitudeMax
, Feature altitudeFeature
,
206 Reference sourceRef
, TaxonDescription desc
, MeasurementUnit meter
, int row
) {
208 QuantitativeData data
= QuantitativeData
.NewInstance(altitudeFeature
);
212 TaxonName nameUsedInSource
= null; //TODO
213 data
.addSource(OriginalSourceType
.PrimaryTaxonomicSource
, null, null, sourceRef
, null, nameUsedInSource
, null);
217 Integer min
= Integer
.valueOf(altitudeMin
);
218 StatisticalMeasurementValue minValue
= StatisticalMeasurementValue
.NewInstance(StatisticalMeasure
.MIN(), min
);
219 data
.addStatisticalValue(minValue
);
221 Integer max
= Integer
.valueOf(altitudeMax
);
222 StatisticalMeasurementValue maxValue
= StatisticalMeasurementValue
.NewInstance(StatisticalMeasure
.MAX(), max
);
223 data
.addStatisticalValue(maxValue
);
225 desc
.addElement(data
);
229 private TaxonDescription
getDescription(Taxon taxon
, Reference sourceRef
) {
231 //TODO Mikle currently does not exist as Source
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);
243 private Reference
getSource(String source
, Reference m77
, Reference m85
) {
244 if(StringUtils
.isNotBlank(source
)){
245 if (source
.equals("Meikle 1977")){
247 }else if (source
.equals("Meikle 1985")){
250 logger
.warn("Source not recognized: " + source
);
256 private UUID
makeUuid(Map
<String
, String
> row
, String colName
) {
257 if (StringUtils
.isBlank(row
.get(colName
))){
260 return UUID
.fromString(row
.get(colName
));
264 Reference sourceReference
;
265 private Reference
getSourceReference() {
266 if (sourceReference
== null){
267 sourceReference
= ReferenceFactory
.newGeneric();
268 sourceReference
.setTitleCache(sourceReferenceTitle
, true);
270 return sourceReference
;
275 public static URI
cyprus_altitude() {
278 sourceUrl
= new URI("file:/F:/data/cyprus/Cyprus-altitude-import-neu.xls");
279 // sourceUrl = new URI("file:/F:/data/cyprus/Zypern-Altitude.xls");
281 } catch (URISyntaxException e
) {
287 public static void main(String
[] args
) {
288 CyprusAltitudeActivator me
= new CyprusAltitudeActivator();
289 me
.doImport(cdmDestination
);