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
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.springframework
.stereotype
.Component
;
17 import org
.springframework
.transaction
.TransactionStatus
;
19 import eu
.etaxonomy
.cdm
.io
.common
.DbExportStateBase
;
20 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
21 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
.DO_REFERENCES
;
22 import eu
.etaxonomy
.cdm
.io
.common
.mapping
.out
.MethodMapper
;
23 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
24 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
25 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
26 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
30 * The export class for NoteSources.
31 * Inserts into DataWarehouse database table <code>NoteSource</code>.
37 public class PesiNoteSourceExport
extends PesiExportBase
{
38 private static final Logger logger
= Logger
.getLogger(PesiNoteSourceExport
.class);
39 private static final Class
<?
extends CdmBase
> standardMethodParameter
= DescriptionElementBase
.class;
41 private static int modCount
= 1000;
42 private static final String dbTableName
= "NoteSource";
43 private static final String pluralString
= "NoteSources";
45 public PesiNoteSourceExport() {
50 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
53 public Class
<?
extends CdmBase
> getStandardMethodParameter() {
54 return standardMethodParameter
;
58 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
61 protected boolean doCheck(PesiExportState state
) {
62 boolean result
= true;
67 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
70 protected void doInvoke(PesiExportState state
) {
72 logger
.error("*** Started Making " + pluralString
+ " ...");
74 // Get the limit for objects to save within a single transaction.
75 // int pageSize = state.getConfig().getLimitSave();
81 // Stores whether this invoke was successful or not.
82 boolean success
= true;
84 // PESI: Clear the database table NoteSource.
87 // Get specific mappings: (CDM) ? -> (PESI) NoteSource
88 PesiExportMapping mapping
= getMapping();
90 // Initialize the db mapper
91 mapping
.initialize(state
);
93 // PESI: Create the NoteSource
96 TransactionStatus txStatus
= null;
97 List
<DescriptionElementBase
> list
= null;
100 txStatus
= startTransaction(true);
101 logger
.info("Started new transaction. Fetching some " + pluralString
+ " (max: " + pageSize
+ ") ...");
102 while ((list
= getDescriptionService().listDescriptionElements(null, null, null, pageSize
, pageNumber
, null)).size() > 0) {
104 logger
.info("Fetched " + list
.size() + " " + pluralString
+ ". Exporting...");
105 for (DescriptionElementBase descriptionElement
: list
) {
107 if (getNoteCategoryFk(descriptionElement
) != null && neededValuesNotNull(descriptionElement
, state
)) {
108 doCount(count
++, modCount
, pluralString
);
109 success
&= mapping
.invoke(descriptionElement
);
113 // Commit transaction
114 commitTransaction(txStatus
);
115 logger
.debug("Committed transaction.");
116 logger
.info("Exported " + (count
- pastCount
) + " " + pluralString
+ ". Total: " + count
);
120 txStatus
= startTransaction(true);
121 logger
.info("Started new transaction. Fetching some " + pluralString
+ " (max: " + pageSize
+ ") ...");
123 // Increment pageNumber
126 if (list
.size() == 0) {
127 logger
.info("No " + pluralString
+ " left to fetch.");
129 // Commit transaction
130 commitTransaction(txStatus
);
131 logger
.debug("Committed transaction.");
133 logger
.info("*** Finished Making " + pluralString
+ " ..." + getSuccessString(success
));
136 state
.setUnsuccessfull();
139 } catch (SQLException e
) {
141 logger
.error(e
.getMessage());
142 state
.setUnsuccessfull();
148 * Checks whether needed values for an entity are NULL.
151 private boolean neededValuesNotNull(DescriptionElementBase descriptionElement
, PesiExportState state
) {
152 boolean result
= true;
153 if (getSourceFk(descriptionElement
, state
) == null) {
154 logger
.error("SourceFk is NULL, but is not allowed to be. Therefore no record was written to export database for this descriptionElement: " + descriptionElement
.getUuid());
161 * Deletes all entries of database tables related to <code>NoteSource</code>.
162 * @param state The {@link PesiExportState PesiExportState}.
163 * @return Whether the delete operation was successful or not.
165 protected boolean doDelete(PesiExportState state
) {
166 PesiExportConfigurator pesiConfig
= (PesiExportConfigurator
) state
.getConfig();
169 Source destination
= pesiConfig
.getDestination();
172 sql
= "DELETE FROM " + dbTableName
;
173 destination
.setQuery(sql
);
174 destination
.update(sql
);
179 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
182 protected boolean isIgnore(PesiExportState state
) {
183 return ! ( state
.getConfig().isDoNoteSources() && state
.getConfig().isDoNotes() && state
.getConfig().getDoReferences().equals(DO_REFERENCES
.ALL
));
187 * Returns the <code>NoteCategoryFk</code> attribute.
188 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
189 * @return The <code>NoteCategoryFk</code> attribute.
191 private static Integer
getNoteCategoryFk(DescriptionElementBase descriptionElement
) {
192 Integer result
= null;
193 result
= PesiTransformer
.feature2NodeCategoryFk(descriptionElement
.getFeature());
198 * Returns the <code>NoteFk</code> attribute.
199 * @param description The {@link TaxonDescription TaxonDescription}.
200 * @param state The {@link PesiExportState PesiExportState}.
201 * @return The <code>NoteFk</code> attribute.
204 @SuppressWarnings("unused")
205 private static Integer
getNoteFk(DescriptionElementBase descriptionElement
, PesiExportState state
) {
206 Integer result
= null;
207 result
= state
.getDbId(descriptionElement
);
212 * Returns the <code>SourceFk</code> attribute.
213 * @param description The {@link TaxonDescription TaxonDescription}.
214 * @param state The {@link DbExportStateBase DbExportState}.
215 * @return The <code>SourceFk</code> attribute.
218 private static Integer
getSourceFk(DescriptionElementBase descriptionElement
, PesiExportState state
) {
219 Integer result
= null;
220 result
= state
.getDbId(descriptionElement
);
225 * Returns the <code>SourceNameCache</code> attribute.
226 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
227 * @return The <code>SourceNameCache</code> attribute.
230 @SuppressWarnings("unused")
231 private static String
getSourceNameCache(DescriptionElementBase descriptionElement
) {
232 String result
= null;
234 DescriptionBase
<?
> inDescription
= descriptionElement
.getInDescription();
235 if (inDescription
!= null && inDescription
.isInstanceOf(TaxonDescription
.class)) {
236 TaxonDescription taxonDescription
= CdmBase
.deproxy(inDescription
, TaxonDescription
.class);
237 Taxon taxon
= taxonDescription
.getTaxon();
238 result
= taxon
.getSec().getTitleCache();
245 * Returns the <code>SourceDetail</code> attribute.
246 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
247 * @return The <code>SourceDetail</code> attribute.
250 @SuppressWarnings("unused")
251 private static String
getSourceDetail(DescriptionElementBase descriptionElement
) {
252 //FIXME this is a replacement for the deprecated descriptionElement.getCitationMicroReference()
253 //it needs to be checked what should be done when multiple sources exist
254 if (descriptionElement
.getSources().size() < 1){
257 if (descriptionElement
.getSources().size() > 1){
258 logger
.warn("Multiple sources exist");
260 return descriptionElement
.getSources().iterator().next().getCitationMicroReference();
262 // return descriptionElement.getCitationMicroReference(); // TODO: What should be used instead?
266 * Returns the CDM to PESI specific export mappings.
267 * @return The {@link PesiExportMapping PesiExportMapping}.
269 private PesiExportMapping
getMapping() {
270 PesiExportMapping mapping
= new PesiExportMapping(dbTableName
);
272 mapping
.addMapper(MethodMapper
.NewInstance("NoteFk", this.getClass(), "getNoteFk", standardMethodParameter
, PesiExportState
.class));
273 mapping
.addMapper(MethodMapper
.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter
, PesiExportState
.class));
274 mapping
.addMapper(MethodMapper
.NewInstance("SourceNameCache", this));
275 mapping
.addMapper(MethodMapper
.NewInstance("SourceDetail", this));