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
.io
.faunaEuropaea
;
12 import static eu
.etaxonomy
.cdm
.io
.faunaEuropaea
.FaunaEuropaeaTransformer
.A_AUCT
;
13 import static eu
.etaxonomy
.cdm
.io
.faunaEuropaea
.FaunaEuropaeaTransformer
.P_PARENTHESIS
;
14 import static eu
.etaxonomy
.cdm
.io
.faunaEuropaea
.FaunaEuropaeaTransformer
.Q_NO_RESTRICTION
;
15 import static eu
.etaxonomy
.cdm
.io
.faunaEuropaea
.FaunaEuropaeaTransformer
.R_GENUS
;
16 import static eu
.etaxonomy
.cdm
.io
.faunaEuropaea
.FaunaEuropaeaTransformer
.R_SUBGENUS
;
17 import static eu
.etaxonomy
.cdm
.io
.faunaEuropaea
.FaunaEuropaeaTransformer
.R_SPECIES
;
18 import static eu
.etaxonomy
.cdm
.io
.faunaEuropaea
.FaunaEuropaeaTransformer
.R_SUBSPECIES
;
19 import static eu
.etaxonomy
.cdm
.io
.faunaEuropaea
.FaunaEuropaeaTransformer
.T_STATUS_ACCEPTED
;
20 import static eu
.etaxonomy
.cdm
.io
.faunaEuropaea
.FaunaEuropaeaTransformer
.T_STATUS_NOT_ACCEPTED
;
22 import java
.sql
.ResultSet
;
23 import java
.sql
.SQLException
;
24 import java
.util
.Collection
;
25 import java
.util
.HashMap
;
26 import java
.util
.List
;
29 import java
.util
.UUID
;
31 import org
.apache
.log4j
.Logger
;
32 import org
.springframework
.stereotype
.Component
;
33 import org
.springframework
.transaction
.TransactionStatus
;
35 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
36 import eu
.etaxonomy
.cdm
.io
.berlinModel
.CdmOneToManyMapper
;
37 import eu
.etaxonomy
.cdm
.io
.berlinModel
.CdmStringMapper
;
38 import eu
.etaxonomy
.cdm
.io
.berlinModel
.in
.BerlinModelImportState
;
39 import eu
.etaxonomy
.cdm
.io
.common
.CdmAttributeMapperBase
;
40 import eu
.etaxonomy
.cdm
.io
.common
.CdmSingleAttributeMapperBase
;
41 import eu
.etaxonomy
.cdm
.io
.common
.ICdmIO
;
42 import eu
.etaxonomy
.cdm
.io
.common
.IImportConfigurator
;
43 import eu
.etaxonomy
.cdm
.io
.common
.ImportHelper
;
44 import eu
.etaxonomy
.cdm
.io
.common
.MapWrapper
;
45 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
46 import eu
.etaxonomy
.cdm
.io
.tcsxml
.in
.TcsXmlImportState
;
47 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
48 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
49 import eu
.etaxonomy
.cdm
.model
.common
.ISourceable
;
50 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
51 import eu
.etaxonomy
.cdm
.model
.common
.OriginalSource
;
52 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
53 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
54 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
55 import eu
.etaxonomy
.cdm
.model
.reference
.Database
;
56 import eu
.etaxonomy
.cdm
.model
.reference
.Generic
;
57 import eu
.etaxonomy
.cdm
.model
.reference
.PublicationBase
;
58 import eu
.etaxonomy
.cdm
.model
.reference
.Publisher
;
59 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
60 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
61 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
62 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
63 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
64 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonomicTree
;
65 import eu
.etaxonomy
.cdm
.strategy
.exceptions
.UnknownCdmTypeException
;
69 * @author a.babadshanjan
74 public class FaunaEuropaeaRelShipImport
extends FaunaEuropaeaImportBase
{
76 public static final String OS_NAMESPACE_TAXON
= "Taxon";
77 private static final Logger logger
= Logger
.getLogger(FaunaEuropaeaRelShipImport
.class);
80 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
83 protected boolean doCheck(FaunaEuropaeaImportState state
) {
84 boolean result
= true;
85 FaunaEuropaeaImportConfigurator fauEuConfig
= state
.getConfig();
86 logger
.warn("Checking for Taxa not yet fully implemented");
87 result
&= checkTaxonStatus(fauEuConfig
);
93 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
95 protected boolean isIgnore(FaunaEuropaeaImportState state
) {
96 return ! state
.getConfig().isDoTaxa();
99 private boolean checkTaxonStatus(FaunaEuropaeaImportConfigurator fauEuConfig
) {
100 boolean result
= true;
102 Source source
= fauEuConfig
.getSource();
104 ResultSet rs
= source
.getResultSet(sqlStr
);
106 // } catch (SQLException e) {
107 // e.printStackTrace();
114 * Import with complete taxon store
116 protected boolean doInvoke(FaunaEuropaeaImportState state
) {
118 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
= state
.getStores();
119 MapWrapper
<TeamOrPersonBase
> authorStore
= (MapWrapper
<TeamOrPersonBase
>)stores
.get(ICdmIO
.TEAM_STORE
);
120 authorStore
.makeEmpty();
121 Map
<Integer
, FaunaEuropaeaTaxon
> fauEuTaxonMap
= state
.getFauEuTaxonMap();
122 TransactionStatus txStatus
= null;
123 boolean success
= true;
125 if(logger
.isInfoEnabled()) { logger
.info("Start making taxa..."); }
127 if (state
.getConfig().isUseTransactions()) {
128 txStatus
= startTransaction();
131 success
= processTaxa(state
, fauEuTaxonMap
);
132 success
= saveTaxa(state
, state
.getHighestTaxonIndex(), state
.getConfig().getLimitSave());
134 if (state
.getConfig().isUseTransactions()) {
135 commitTransaction(txStatus
);
138 logger
.info("End making taxa...");
144 * Processes taxa from complete taxon store
146 private boolean processTaxa(FaunaEuropaeaImportState state
,
147 Map
<Integer
, FaunaEuropaeaTaxon
> fauEuTaxonMap
) {
149 if(logger
.isInfoEnabled()) { logger
.info("Processing taxa second pass..."); }
151 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
= state
.getStores();
152 MapWrapper
<TaxonBase
> taxonStore
= (MapWrapper
<TaxonBase
>)stores
.get(ICdmIO
.TAXON_STORE
);
153 FaunaEuropaeaImportConfigurator fauEuConfig
= state
.getConfig();
154 ReferenceBase
<?
> sourceRef
= fauEuConfig
.getSourceReference();
156 boolean success
= true;
158 for (int id
: fauEuTaxonMap
.keySet())
159 //for (int id : taxonStore.keySet())
161 TaxonBase
<?
> taxonBase
= taxonStore
.get(id
);
162 TaxonNameBase
<?
,?
> taxonName
= taxonBase
.getName();
163 FaunaEuropaeaTaxon fauEuTaxon
= fauEuTaxonMap
.get(id
);
165 if (logger
.isDebugEnabled()) { logger
.debug("Taxon # " + id
); }
166 createRelationships(fauEuTaxon
, taxonBase
, taxonName
, fauEuTaxonMap
, state
);
172 /** Creates relationships for existing taxon store in memory */
173 private boolean createRelationships(FaunaEuropaeaTaxon fauEuTaxon
,
174 TaxonBase
<?
> taxonBase
, TaxonNameBase
<?
,?
> taxonName
,
175 Map
<Integer
, FaunaEuropaeaTaxon
> fauEuTaxonMap
, FaunaEuropaeaImportState state
) {
177 int parentId
= fauEuTaxon
.getParentId();
178 int taxonId
= fauEuTaxon
.getId();
179 Map
<String
, MapWrapper
<?
extends CdmBase
>> stores
= state
.getStores();
180 MapWrapper
<TaxonBase
> taxonStore
= (MapWrapper
<TaxonBase
>)stores
.get(ICdmIO
.TAXON_STORE
);
181 ReferenceBase
<?
> sourceRef
= state
.getConfig().getSourceReference();
182 TaxonBase
<?
> parentTaxonBase
= taxonStore
.get(parentId
);
183 Taxon parentTaxon
= parentTaxonBase
.deproxy(parentTaxonBase
, Taxon
.class);
184 // FaunaEuropaeaTaxon parent = fauEuTaxonMap.get(parentId);
185 boolean success
= true;
187 if (!fauEuTaxon
.isValid()) { // FauEu Synonym
189 if (fauEuTaxon
.getAuthor() != null && fauEuTaxon
.getAuthor().equals("A_AUCT_NAME")) {
191 // add misapplied name relationship from this taxon to parent
192 Taxon taxon
= taxonBase
.deproxy(taxonBase
, Taxon
.class);
193 taxon
.addMisappliedName(parentTaxon
, sourceRef
, null);
194 if (logger
.isInfoEnabled()) {
195 logger
.info("Misapplied name created " + taxon
.getUuid());
198 } catch (Exception e
) {
199 logger
.error("Error creating misapplied name relationship for taxon (" +
204 else if((fauEuTaxon
.getAuthor() == null)
205 || (fauEuTaxon
.getAuthor() != null && !fauEuTaxon
.getAuthor().equals("A_AUCT_NAME"))) {
207 // add this synonym as heterotypic synonym to parent
208 Synonym synonym
= taxonBase
.deproxy(taxonBase
, Synonym
.class);
209 parentTaxon
.addSynonym(synonym
, SynonymRelationshipType
.HETEROTYPIC_SYNONYM_OF());
210 if (logger
.isDebugEnabled()) {
211 logger
.debug("Heterotypic synonym created " + synonym
.getUuid());
214 } catch (Exception e
) {
215 logger
.error("Error creating heterotypic synonym for taxon (" + parentId
+ ")");
216 // e.printStackTrace();
220 } else if (fauEuTaxon
.isValid()) { // FauEu Taxon
222 Taxon taxon
= taxonBase
.deproxy(taxonBase
, Taxon
.class);
225 // add this taxon as child to parent
226 if (parentTaxon
!= null) {
227 makeTaxonomicallyIncluded(state
, parentTaxon
, taxon
, sourceRef
, null);
228 if (logger
.isDebugEnabled()) {
229 logger
.debug("Parent-child (" + parentId
+ "-" + taxonId
+
230 ") relationship created");
234 } catch (Exception e
) {
235 logger
.error("Error creating taxonomically included relationship Parent-child (" +
236 parentId
+ "-" + taxonId
+ ")");
244 private boolean makeTaxonomicallyIncluded(FaunaEuropaeaImportState state
, Taxon toTaxon
, Taxon fromTaxon
, ReferenceBase citation
, String microCitation
){
245 boolean success
= true;
246 ReferenceBase sec
= toTaxon
.getSec();
247 TaxonomicTree tree
= state
.getTree(sec
);
249 tree
= makeTree(state
, sec
);
251 success
= tree
.addParentChild(toTaxon
, fromTaxon
, citation
, microCitation
);