(no commit message)
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / pesi / out / PesiRelTaxonExport.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.cdm.io.pesi.out;
11
12 import java.sql.SQLException;
13 import java.util.List;
14
15 import org.apache.log4j.Logger;
16 import org.springframework.stereotype.Component;
17 import org.springframework.transaction.TransactionStatus;
18
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;
28
29 /**
30 * @author a.mueller
31 * @author e.-m.lee
32 * @date 23.02.2010
33 *
34 */
35 @Component
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;
40
41 private static int modCount = 1000;
42 private static final String dbTableName = "RelTaxon";
43 private static final String pluralString = "TaxonRelationships";
44
45 public PesiRelTaxonExport() {
46 super();
47 }
48
49 /* (non-Javadoc)
50 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
51 */
52 @Override
53 public Class<? extends CdmBase> getStandardMethodParameter() {
54 return standardMethodParameter;
55 }
56
57 /* (non-Javadoc)
58 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
59 */
60 @Override
61 protected boolean doCheck(PesiExportState state) {
62 boolean result = true;
63 return result;
64 }
65
66 /* (non-Javadoc)
67 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
68 */
69 @Override
70 protected boolean doInvoke(PesiExportState state) {
71 try {
72 logger.info("Start: Make " + pluralString + " ...");
73
74 // Stores whether this invoke was successful or not.
75 boolean success = true ;
76
77 // PESI: Clear the database table RelTaxon.
78 doDelete(state);
79
80 // Start transaction
81 TransactionStatus txStatus = startTransaction(true);
82
83 // CDM: Get all Relationships
84 List<RelationshipBase> list = getTaxonService().getAllRelationships(100000000, 0);
85
86 // Get specific mappings: (CDM) TaxonRelationship -> (PESI) RelTaxon
87 PesiExportMapping mapping = getMapping();
88
89 // Initialize the db mapper
90 mapping.initialize(state);
91
92 // PESI: Create the RelTaxa
93 int count = 0;
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);
98 }
99 }
100
101 // Commit transaction
102 commitTransaction(txStatus);
103 logger.info("End: Make " + pluralString + " ..." + getSuccessString(success));
104
105 return success;
106 } catch(SQLException e) {
107 e.printStackTrace();
108 logger.error(e.getMessage());
109 return false;
110 }
111 }
112
113 /**
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.
117 */
118 protected boolean doDelete(PesiExportState state) {
119 PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
120
121 String sql;
122 Source destination = pesiConfig.getDestination();
123
124 // Clear RelTaxon
125 sql = "DELETE FROM " + dbTableName;
126 destination.setQuery(sql);
127 destination.update(sql);
128 return true;
129 }
130
131 /* (non-Javadoc)
132 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
133 */
134 @Override
135 protected boolean isIgnore(PesiExportState state) {
136 return ! state.getConfig().isDoRelTaxa();
137 }
138
139 /**
140 * Returns the <code>TaxonFk1</code> attribute.
141 * @param relationship The {@link RelationshipBase Relationship}.
142 * @return The <code>TaxonFk1</code> attribute.
143 * @see MethodMapper
144 */
145 @SuppressWarnings("unused")
146 private static Integer getTaxonFk1(RelationshipBase<?, ?, ?> relationship, DbExportStateBase<?> state) {
147 return getObjectFk(relationship, state, true);
148 }
149
150 /**
151 * Returns the <code>TaxonFk2</code> attribute.
152 * @param relationship The {@link RelationshipBase Relationship}.
153 * @return The <code>TaxonFk2</code> attribute.
154 * @see MethodMapper
155 */
156 @SuppressWarnings("unused")
157 private static Integer getTaxonFk2(RelationshipBase<?, ?, ?> relationship, DbExportStateBase<?> state) {
158 return getObjectFk(relationship, state, false);
159 }
160
161 /**
162 * Returns the <code>RelTaxonQualifierFk</code> attribute.
163 * @param relationship The {@link RelationshipBase Relationship}.
164 * @return The <code>RelTaxonQualifierFk</code> attribute.
165 * @see MethodMapper
166 */
167 @SuppressWarnings("unused")
168 private static Integer getRelTaxonQualifierFk(RelationshipBase<?, ?, ?> relationship) {
169 return PesiTransformer.taxRelation2RelTaxonQualifierFk(relationship);
170 }
171
172 /**
173 * Returns the <code>RelQualifierCache</code> attribute.
174 * @param relationship The {@link RelationshipBase Relationship}.
175 * @return The <code>RelQualifierCache</code> attribute.
176 * @see MethodMapper
177 */
178 @SuppressWarnings("unused")
179 private static String getRelQualifierCache(RelationshipBase<?, ?, ?> relationship) {
180 // TODO
181 return null;
182 }
183
184 /**
185 * Returns the <code>Notes</code> attribute.
186 * @param relationship The {@link RelationshipBase Relationship}.
187 * @return The <code>Notes</code> attribute.
188 * @see MethodMapper
189 */
190 @SuppressWarnings("unused")
191 private static String getNotes(RelationshipBase<?, ?, ?> relationship) {
192 // TODO
193 return null;
194 }
195
196 /**
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.
202 */
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();
211 }
212 if (taxon != null) {
213 CdmBase cdmBase = taxon.getSec();
214 return state.getDbId(cdmBase);
215 }
216 logger.warn("No taxon found for relationship: " + relationship.toString());
217 return null;
218 }
219
220 /**
221 * Returns the CDM to PESI specific export mappings.
222 * @return The {@link PesiExportMapping PesiExportMapping}.
223 */
224 private PesiExportMapping getMapping() {
225 PesiExportMapping mapping = new PesiExportMapping(dbTableName);
226
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));
234
235 return mapping;
236 }
237
238 }