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
.berlinModel
.out
.mapper
.CreatedAndNotesMapper
;
20 import eu
.etaxonomy
.cdm
.io
.berlinModel
.out
.mapper
.MethodMapper
;
21 import eu
.etaxonomy
.cdm
.io
.common
.DbExportStateBase
;
22 import eu
.etaxonomy
.cdm
.io
.common
.Source
;
23 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
24 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipBase
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationship
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
36 @SuppressWarnings("unchecked")
37 public class PesiRelTaxonExport
extends PesiExportBase
<RelationshipBase
> {
38 private static final Logger logger
= Logger
.getLogger(PesiRelTaxonExport
.class);
39 private static final Class
<?
extends CdmBase
> standardMethodParameter
= RelationshipBase
.class;
41 private static int modCount
= 1000;
42 private static final String dbTableName
= "RelTaxon";
43 private static final String pluralString
= "TaxonRelationships";
45 public PesiRelTaxonExport() {
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 boolean doInvoke(PesiExportState state
) {
72 logger
.info("Start: Make " + pluralString
+ " ...");
74 // Stores whether this invoke was successful or not.
75 boolean success
= true ;
77 // PESI: Clear the database table RelTaxon.
81 TransactionStatus txStatus
= startTransaction(true);
83 // CDM: Get all Relationships
84 List
<RelationshipBase
> list
= getTaxonService().getAllRelationships(100000000, 0);
86 // Get specific mappings: (CDM) TaxonRelationship -> (PESI) RelTaxon
87 PesiExportMapping mapping
= getMapping();
89 // Initialize the db mapper
90 mapping
.initialize(state
);
92 // PESI: Create the RelTaxa
94 for (RelationshipBase
<?
, ?
, ?
> relation
: list
) {
95 if (relation
.isInstanceOf(TaxonRelationship
.class) || relation
.isInstanceOf(SynonymRelationship
.class)) {
96 doCount(count
++, modCount
, pluralString
);
97 success
&= mapping
.invoke(relation
);
101 // Commit transaction
102 commitTransaction(txStatus
);
103 logger
.info("End: Make " + pluralString
+ " ..." + getSuccessString(success
));
106 } catch(SQLException e
) {
108 logger
.error(e
.getMessage());
114 * Deletes all entries of database tables related to <code>RelTaxon</code>.
115 * @param state The {@link PesiExportState PesiExportState}.
116 * @return Whether the delete operation was successful or not.
118 protected boolean doDelete(PesiExportState state
) {
119 PesiExportConfigurator pesiConfig
= (PesiExportConfigurator
) state
.getConfig();
122 Source destination
= pesiConfig
.getDestination();
125 sql
= "DELETE FROM " + dbTableName
;
126 destination
.setQuery(sql
);
127 destination
.update(sql
);
132 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
135 protected boolean isIgnore(PesiExportState state
) {
136 return ! state
.getConfig().isDoRelTaxa();
140 * Returns the <code>TaxonFk1</code> attribute.
141 * @param relationship The {@link RelationshipBase Relationship}.
142 * @return The <code>TaxonFk1</code> attribute.
145 @SuppressWarnings("unused")
146 private static Integer
getTaxonFk1(RelationshipBase
<?
, ?
, ?
> relationship
, DbExportStateBase
<?
> state
) {
147 return getObjectFk(relationship
, state
, true);
151 * Returns the <code>TaxonFk2</code> attribute.
152 * @param relationship The {@link RelationshipBase Relationship}.
153 * @return The <code>TaxonFk2</code> attribute.
156 @SuppressWarnings("unused")
157 private static Integer
getTaxonFk2(RelationshipBase
<?
, ?
, ?
> relationship
, DbExportStateBase
<?
> state
) {
158 return getObjectFk(relationship
, state
, false);
162 * Returns the <code>RelTaxonQualifierFk</code> attribute.
163 * @param relationship The {@link RelationshipBase Relationship}.
164 * @return The <code>RelTaxonQualifierFk</code> attribute.
167 @SuppressWarnings("unused")
168 private static Integer
getRelTaxonQualifierFk(RelationshipBase
<?
, ?
, ?
> relationship
) {
169 return PesiTransformer
.taxRelation2RelTaxonQualifierFk(relationship
);
173 * Returns the <code>RelQualifierCache</code> attribute.
174 * @param relationship The {@link RelationshipBase Relationship}.
175 * @return The <code>RelQualifierCache</code> attribute.
178 @SuppressWarnings("unused")
179 private static String
getRelQualifierCache(RelationshipBase
<?
, ?
, ?
> relationship
) {
185 * Returns the <code>Notes</code> attribute.
186 * @param relationship The {@link RelationshipBase Relationship}.
187 * @return The <code>Notes</code> attribute.
190 @SuppressWarnings("unused")
191 private static String
getNotes(RelationshipBase
<?
, ?
, ?
> relationship
) {
197 * Returns the database key of an object in the given relationship.
198 * @param relationship {@link RelationshipBase RelationshipBase}.
199 * @param state {@link DbExportStateBase DbExportStateBase}.
200 * @param isFrom A boolean value indicating whether the database key of the parent or child in this relationship is searched. <code>true</code> means the child is searched. <code>false</code> means the parent is searched.
201 * @return The database key of an object in the given relationship.
203 private static Integer
getObjectFk(RelationshipBase
<?
, ?
, ?
> relationship
, DbExportStateBase
<?
> state
, boolean isFrom
){
204 TaxonBase
<?
> taxon
= null;
205 if (relationship
.isInstanceOf(TaxonRelationship
.class)) {
206 TaxonRelationship tr
= (TaxonRelationship
)relationship
;
207 taxon
= (isFrom
) ? tr
.getFromTaxon(): tr
.getToTaxon();
208 } else if (relationship
.isInstanceOf(SynonymRelationship
.class)) {
209 SynonymRelationship sr
= (SynonymRelationship
)relationship
;
210 taxon
= (isFrom
) ? sr
.getSynonym() : sr
.getAcceptedTaxon();
213 CdmBase cdmBase
= taxon
.getSec();
214 return state
.getDbId(cdmBase
);
216 logger
.warn("No taxon found for relationship: " + relationship
.toString());
221 * Returns the CDM to PESI specific export mappings.
222 * @return The {@link PesiExportMapping PesiExportMapping}.
224 private PesiExportMapping
getMapping() {
225 PesiExportMapping mapping
= new PesiExportMapping(dbTableName
);
227 // mapping.addMapper(IdMapper.NewInstance("RelTaxonId"));
228 mapping
.addMapper(MethodMapper
.NewInstance("TaxonFk1", this.getClass(), "getTaxonFk1", standardMethodParameter
, DbExportStateBase
.class));
229 mapping
.addMapper(MethodMapper
.NewInstance("TaxonFk2", this.getClass(), "getTaxonFk2", standardMethodParameter
, DbExportStateBase
.class));
230 mapping
.addMapper(MethodMapper
.NewInstance("RelTaxonQualifierFk", this));
231 mapping
.addMapper(MethodMapper
.NewInstance("RelQualifierCache", this));
232 // mapping.addMapper(MethodMapper.NewInstance("Notes", this));
233 mapping
.addMapper(CreatedAndNotesMapper
.NewInstance(false));