3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.io
.pesi
.out
;
12 import java
.sql
.SQLException
;
13 import java
.util
.List
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.springframework
.stereotype
.Component
;
18 import org
.springframework
.transaction
.TransactionStatus
;
20 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
21 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.out
.MethodMapper
;
22 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
23 import eu
.etaxonomy
.cdm
.model
.common
.DescriptionElementSource
;
24 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
25 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
26 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
27 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
28 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
30 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
33 * The export class for additional information linked to {@link eu.etaxonomy.cdm.model.taxon.Taxon Taxa} and {@link eu.etaxonomy.cdm.model.reference.Reference References}.<p>
34 * Inserts into DataWarehouse database table <code>AdditionalTaxonSource</code>.
40 @SuppressWarnings("unchecked")
41 public class PesiAdditionalTaxonSourceExport
extends PesiExportBase
{
42 private static final Logger logger
= Logger
.getLogger(PesiAdditionalTaxonSourceExport
.class);
43 private static final Class
<?
extends CdmBase
> standardMethodParameter
= Reference
.class;
45 private static int modCount
= 1000;
46 private static final String dbTableName
= "AdditionalTaxonSource";
47 private static final String pluralString
= "DescriptionElements";
48 private static final String parentPluralString
= "Taxa";
49 private static boolean sourceUse_AdditionalSource
= false;
50 private static boolean sourceUse_NomenclaturalReference
= false;
51 private static boolean sourceUse_SourceOfSynonymy
= false;
52 private static TaxonNameBase currentTaxonName
= null;
53 private static String citationMicroReference
= null;
55 public PesiAdditionalTaxonSourceExport() {
60 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
63 public Class
<?
extends CdmBase
> getStandardMethodParameter() {
64 return standardMethodParameter
;
68 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
71 protected boolean doCheck(PesiExportState state
) {
72 boolean result
= true;
77 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
80 protected void doInvoke(PesiExportState state
) {
82 logger
.error("*** Started Making " + pluralString
+ " ...");
84 // Get the limit for objects to save within a single transaction.
85 // int limit = state.getConfig().getLimitSave();
88 // Stores whether this invoke was successful or not.
89 boolean success
= true;
91 // PESI: Clear the database table Note.
94 // CDM: Get the number of all available description elements.
95 // int maxCount = getDescriptionService().count(null);
96 // logger.error("Total amount of " + maxCount + " " + pluralString + " will be exported.");
98 // Get specific mappings: (CDM) DescriptionElement -> (PESI) Note
99 PesiExportMapping mapping
= getMapping();
101 // Initialize the db mapper
102 mapping
.initialize(state
);
104 // PESI: Create the Notes
108 TransactionStatus txStatus
= null;
109 List
<TaxonBase
> list
= null;
112 txStatus
= startTransaction(true);
113 logger
.error("Started new transaction. Fetching some " + parentPluralString
+ " first (max: " + limit
+ ") ...");
114 while ((list
= getTaxonService().list(null, limit
, taxonCount
, null, null)).size() > 0) {
116 taxonCount
+= list
.size();
117 logger
.error("Fetched " + list
.size() + " " + parentPluralString
+ ".");
119 logger
.error("PHASE 2: Check for SourceUse 'Additional Source'");
120 sourceUse_AdditionalSource
= true;
121 for (TaxonBase taxonBase
: list
) {
123 // Set the current Taxon
124 currentTaxonName
= taxonBase
.getName();
126 if (taxonBase
.isInstanceOf(Taxon
.class)) {
128 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
130 // Determine the TaxonDescriptions
131 Set
<TaxonDescription
> taxonDescriptions
= taxon
.getDescriptions();
133 // Determine the DescriptionElements (Citations) for the current Taxon
134 for (TaxonDescription taxonDescription
: taxonDescriptions
) {
135 Set
<DescriptionElementBase
> descriptionElements
= taxonDescription
.getElements();
137 for (DescriptionElementBase descriptionElement
: descriptionElements
) {
138 // According to FaEu Import those DescriptionElementBase elements are of instance TextData
139 // There are no other indicators
140 if (descriptionElement
.isInstanceOf(TextData
.class)) {
141 Set
<DescriptionElementSource
> elementSources
= descriptionElement
.getSources();
143 for (DescriptionElementSource elementSource
: elementSources
) {
145 // Set the CitationMicroReference so it is accessible later in getSourceDetail()
146 setCitationMicroReference(elementSource
.getCitationMicroReference());
149 Reference reference
= elementSource
.getCitation();
151 // Check whether it was a synonym originally
152 TaxonNameBase nameUsedInSource
= elementSource
.getNameUsedInSource();
153 if (nameUsedInSource
!= null) {
154 // It was a synonym originally: Set currentTaxonName to synonym's taxonName
155 currentTaxonName
= nameUsedInSource
;
158 // Citations can be empty (null): Is it wrong data or just a normal case?
159 if (reference
!= null) {
160 if (neededValuesNotNull(reference
, state
)) {
161 doCount(count
++, modCount
, pluralString
);
162 success
&= mapping
.invoke(reference
);
172 sourceUse_AdditionalSource
= false;
173 logger
.error("Exported " + (count
- pastCount
) + " " + pluralString
+ ".");
175 // Commit transaction
176 commitTransaction(txStatus
);
177 logger
.error("Committed transaction.");
178 logger
.error("Exported " + (count
- pastCount
) + " " + pluralString
+ ". Total: " + count
);
182 txStatus
= startTransaction(true);
183 logger
.error("Started new transaction. Fetching some " + parentPluralString
+ " first (max: " + limit
+ ") ...");
185 if (list
.size() == 0) {
186 logger
.error("No " + pluralString
+ " left to fetch.");
188 // Commit transaction
189 commitTransaction(txStatus
);
190 logger
.error("Committed transaction.");
192 logger
.error("*** Finished Making " + pluralString
+ " ..." + getSuccessString(success
));
195 } catch (SQLException e
) {
197 logger
.error(e
.getMessage());
198 state
.setUnsuccessfull();
204 * Checks whether needed values for an entity are NULL.
207 private boolean neededValuesNotNull(Reference
<?
> reference
, PesiExportState state
) {
208 boolean result
= true;
209 if (getSourceFk(reference
, state
) == null) {
210 logger
.error("SourceFk is NULL, but is not allowed to be. Therefore no record was written to export database for this reference: " + reference
.getUuid());
213 if (getSourceUseFk(reference
) == null) {
214 logger
.error("SourceUseFk is NULL, but is not allowed to be. Therefore no record was written to export database for this reference: " + reference
.getUuid());
221 * Deletes all entries of database tables related to <code>AdditionalTaxonSource</code>.
222 * @param state The {@link PesiExportState PesiExportState}.
223 * @return Whether the delete operation was successful or not.
225 protected boolean doDelete(PesiExportState state
) {
226 PesiExportConfigurator pesiConfig
= state
.getConfig();
229 Source destination
= pesiConfig
.getDestination();
231 // Clear AdditionalTaxonSource
232 sql
= "DELETE FROM " + dbTableName
;
233 destination
.setQuery(sql
);
234 destination
.update(sql
);
239 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
242 protected boolean isIgnore(PesiExportState state
) {
244 // return state.getConfig().isDoAdditionalTaxonSource()
249 * Returns the <code>TaxonFk</code> attribute.
250 * @param reference The {@link Reference Reference}.
253 @SuppressWarnings("unused")
254 private static Integer
getTaxonFk(Reference
<?
> reference
, PesiExportState state
) {
255 // Reference parameter isn't needed, but the DbSingleAttributeExportMapperBase throws a type mismatch exception otherwise
256 // since it awaits two parameters if one of them is of instance DbExportStateBase.
257 Integer result
= null;
258 if (state
!= null && currentTaxonName
!= null) {
259 result
= state
.getDbId(currentTaxonName
);
265 * Returns the <code>SourceFk</code> attribute.
266 * @param reference The {@link Reference Reference}.
267 * @return The <code>SourceFk</code> attribute.
270 private static Integer
getSourceFk(Reference
<?
> reference
, PesiExportState state
) {
271 Integer result
= null;
272 if (state
!= null && reference
!= null) {
273 result
= state
.getDbId(reference
);
279 * Returns the <code>SourceUseFk</code> attribute.
280 * @param reference The {@link Reference Reference}.
281 * @return The <code>SourceUseFk</code> attribute.
284 private static Integer
getSourceUseFk(Reference
<?
> reference
) {
286 Integer result
= null;
287 if (sourceUse_AdditionalSource
) {
288 result
= PesiTransformer
.sourceUseIdSourceUseId(3);
289 } else if (sourceUse_SourceOfSynonymy
) {
290 result
= PesiTransformer
.sourceUseIdSourceUseId(4);
291 } else if (sourceUse_NomenclaturalReference
) {
292 result
= PesiTransformer
.sourceUseIdSourceUseId(8);
298 * Returns the <code>SourceUseCache</code> attribute.
299 * @param reference The {@link Reference Reference}.
300 * @return The <code>SourceUseCache</code> attribute.
303 @SuppressWarnings("unused")
304 private static String
getSourceUseCache(Reference
<?
> reference
) {
306 String result
= null;
307 if (sourceUse_AdditionalSource
) {
308 result
= PesiTransformer
.sourceUseId2SourceUseCache(3);
309 } else if (sourceUse_SourceOfSynonymy
) {
310 result
= PesiTransformer
.sourceUseId2SourceUseCache(4);
311 } else if (sourceUse_NomenclaturalReference
) {
312 result
= PesiTransformer
.sourceUseId2SourceUseCache(8);
318 * Returns the <code>SourceNameCache</code> attribute.
319 * @param reference The {@link Reference Reference}.
320 * @return The <code>SourceNameCache</code> attribute.
323 @SuppressWarnings("unused")
324 private static String
getSourceNameCache(Reference
<?
> reference
) {
325 String result
= null;
326 if (reference
!= null) {
327 result
= reference
.getTitle();
333 * Returns the <code>SourceDetail</code> attribute.
334 * @param reference The {@link Reference Reference}.
335 * @return The <code>SourceDetail</code> attribute.
338 @SuppressWarnings("unused")
339 private static String
getSourceDetail(Reference
<?
> reference
) {
340 return PesiAdditionalTaxonSourceExport
.citationMicroReference
;
344 * @param citationMicroReference the citationMicroReference to set
346 public static void setCitationMicroReference(String citationMicroReference
) {
347 PesiAdditionalTaxonSourceExport
.citationMicroReference
= citationMicroReference
;
351 * Returns the CDM to PESI specific export mappings.
352 * @return The {@link PesiExportMapping PesiExportMapping}.
354 private PesiExportMapping
getMapping() {
355 PesiExportMapping mapping
= new PesiExportMapping(dbTableName
);
357 mapping
.addMapper(MethodMapper
.NewInstance("TaxonFk", this.getClass(), "getTaxonFk", standardMethodParameter
, PesiExportState
.class));
358 mapping
.addMapper(MethodMapper
.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter
, PesiExportState
.class));
359 mapping
.addMapper(MethodMapper
.NewInstance("SourceUseFk", this));
360 mapping
.addMapper(MethodMapper
.NewInstance("SourceUseCache", this));
361 mapping
.addMapper(MethodMapper
.NewInstance("SourceNameCache", this));
362 mapping
.addMapper(MethodMapper
.NewInstance("SourceDetail", this));