Project

General

Profile

Download (13.3 KB) Statistics
| Branch: | Revision:
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.old;
11

    
12
import java.sql.SQLException;
13
import java.util.List;
14
import java.util.Set;
15

    
16
import org.apache.log4j.Logger;
17
import org.springframework.stereotype.Component;
18
import org.springframework.transaction.TransactionStatus;
19

    
20
import eu.etaxonomy.cdm.io.common.Source;
21
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
22
import eu.etaxonomy.cdm.io.pesi.out.PesiExportBase;
23
import eu.etaxonomy.cdm.io.pesi.out.PesiExportConfigurator;
24
import eu.etaxonomy.cdm.io.pesi.out.PesiExportMapping;
25
import eu.etaxonomy.cdm.io.pesi.out.PesiExportState;
26
import eu.etaxonomy.cdm.io.pesi.out.PesiTransformer;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
29
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
30
import eu.etaxonomy.cdm.model.description.TaxonDescription;
31
import eu.etaxonomy.cdm.model.description.TextData;
32
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
33
import eu.etaxonomy.cdm.model.reference.Reference;
34
import eu.etaxonomy.cdm.model.taxon.Taxon;
35
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
36

    
37
/**
38
 * 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>
39
 * Inserts into DataWarehouse database table <code>AdditionalTaxonSource</code>.
40
 * @author e.-m.lee
41
 * @date 23.02.2010
42
 *
43
 */
44
@Component
45
@SuppressWarnings("unchecked")
46
public class PesiAdditionalTaxonSourceExport extends PesiExportBase {
47
	private static final Logger logger = Logger.getLogger(PesiAdditionalTaxonSourceExport.class);
48
	private static final Class<? extends CdmBase> standardMethodParameter = Reference.class;
49

    
50
	private static int modCount = 1000;
51
	private static final String dbTableName = "AdditionalTaxonSource";
52
	private static final String pluralString = "DescriptionElements";
53
	private static final String parentPluralString = "Taxa";
54
	private static boolean sourceUse_AdditionalSource = false;
55
	private static boolean sourceUse_NomenclaturalReference = false;
56
	private static boolean sourceUse_SourceOfSynonymy = false;
57
	private static TaxonNameBase currentTaxonName = null;
58
	private static String citationMicroReference = null;
59
	
60
	public PesiAdditionalTaxonSourceExport() {
61
		super();
62
	}
63

    
64
	/* (non-Javadoc)
65
	 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
66
	 */
67
	@Override
68
	public Class<? extends CdmBase> getStandardMethodParameter() {
69
		return standardMethodParameter;
70
	}
71

    
72
	/* (non-Javadoc)
73
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
74
	 */
75
	@Override
76
	protected boolean doCheck(PesiExportState state) {
77
		boolean result = true;
78
		return result;
79
	}
80

    
81
	/* (non-Javadoc)
82
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
83
	 */
84
	@Override
85
	protected void doInvoke(PesiExportState state) {
86
		try {
87
			logger.error("*** Started Making " + pluralString + " ...");
88

    
89
			// Get the limit for objects to save within a single transaction.
90
//			int limit = state.getConfig().getLimitSave();
91
			int limit = 1000;
92

    
93
			// Stores whether this invoke was successful or not.
94
			boolean success = true;
95
	
96
			// PESI: Clear the database table Note.
97
			doDelete(state);
98
	
99
			// CDM: Get the number of all available description elements.
100
//			int maxCount = getDescriptionService().count(null);
101
//			logger.error("Total amount of " + maxCount + " " + pluralString + " will be exported.");
102

    
103
			// Get specific mappings: (CDM) DescriptionElement -> (PESI) Note
104
			PesiExportMapping mapping = getMapping();
105
	
106
			// Initialize the db mapper
107
			mapping.initialize(state);
108
	
109
			// PESI: Create the Notes
110
			int count = 0;
111
			int taxonCount = 0;
112
			int pastCount = 0;
113
			TransactionStatus txStatus = null;
114
			List<TaxonBase> list = null;
115

    
116
			// Start transaction
117
			txStatus = startTransaction(true);
118
			logger.info("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
119
			while ((list = getTaxonService().list(null, limit, taxonCount, null, null)).size() > 0) {
120

    
121
				taxonCount += list.size();
122
				logger.info("Fetched " + list.size() + " " + parentPluralString + ".");
123
				
124
				logger.info("PHASE 2: Check for SourceUse 'Additional Source'");
125
				sourceUse_AdditionalSource = true;
126
				for (TaxonBase taxonBase : list) {
127
					
128
					// Set the current Taxon
129
					currentTaxonName = taxonBase.getName();
130

    
131
					if (taxonBase.isInstanceOf(Taxon.class)) {
132
						
133
						Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
134

    
135
						// Determine the TaxonDescriptions
136
						Set<TaxonDescription> taxonDescriptions = taxon.getDescriptions();
137

    
138
						// Determine the DescriptionElements (Citations) for the current Taxon
139
						for (TaxonDescription taxonDescription : taxonDescriptions) {
140
							Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
141
							
142
							for (DescriptionElementBase descriptionElement : descriptionElements) {
143
								// According to FaEu Import those DescriptionElementBase elements are of instance TextData
144
								// There are no other indicators
145
								if (descriptionElement.isInstanceOf(TextData.class)) {
146
									Set<DescriptionElementSource> elementSources = descriptionElement.getSources();
147
									
148
									for (DescriptionElementSource elementSource : elementSources) {
149
										
150
										// Set the CitationMicroReference so it is accessible later in getSourceDetail()
151
										setCitationMicroReference(elementSource.getCitationMicroReference());
152
	
153
										// Get the citation
154
										Reference<?> reference = elementSource.getCitation();
155
										
156
										// Check whether it was a synonym originally
157
										TaxonNameBase<?,?> nameUsedInSource = elementSource.getNameUsedInSource();
158
										if (nameUsedInSource != null) {
159
											// It was a synonym originally: Set currentTaxonName to synonym's taxonName
160
											currentTaxonName = nameUsedInSource;
161
										}
162
										
163
										// Citations can be empty (null): Is it wrong data or just a normal case?
164
										if (reference != null) {
165
											if (neededValuesNotNull(reference, state)) {
166
												doCount(count++, modCount, pluralString);
167
												success &= mapping.invoke(reference);
168
											}
169
										}
170
									}
171
								}
172
							}
173
						}
174
					}
175
					
176
				}
177
				sourceUse_AdditionalSource = false;
178
				logger.error("Exported " + (count - pastCount) + " " + pluralString + ".");
179
				
180
				// Commit transaction
181
				commitTransaction(txStatus);
182
				logger.error("Committed transaction.");
183
				logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
184
				pastCount = count;
185

    
186
				// Start transaction
187
				txStatus = startTransaction(true);
188
				logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
189
			}
190
			if (list.size() == 0) {
191
				logger.error("No " + pluralString + " left to fetch.");
192
			}
193
			
194
			list = null;
195
			// Commit transaction
196
			commitTransaction(txStatus);
197
			logger.error("Committed transaction.");
198
	
199
			logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
200
			
201
			return;
202
		} catch (SQLException e) {
203
			e.printStackTrace();
204
			logger.error(e.getMessage());
205
			state.setUnsuccessfull();
206
			return;
207
		}
208
	}
209

    
210
	/**
211
	 * Checks whether needed values for an entity are NULL.
212
	 * @return
213
	 */
214
	private boolean neededValuesNotNull(Reference<?> reference, PesiExportState state) {
215
		boolean result = true;
216
		if (getSourceFk(reference, state) == null) {
217
			logger.error("SourceFk is NULL, but is not allowed to be. Therefore no record was written to export database for this reference: " + reference.getUuid());
218
			result = false;
219
		}
220
		if (getSourceUseFk(reference) == null) {
221
			logger.error("SourceUseFk is NULL, but is not allowed to be. Therefore no record was written to export database for this reference: " + reference.getUuid());
222
			result = false;
223
		}
224
		return result;
225
	}
226
	
227
	/**
228
	 * Deletes all entries of database tables related to <code>AdditionalTaxonSource</code>.
229
	 * @param state The {@link PesiExportState PesiExportState}.
230
	 * @return Whether the delete operation was successful or not.
231
	 */
232
	protected boolean doDelete(PesiExportState state) {
233
		PesiExportConfigurator pesiConfig = state.getConfig();
234
		
235
		String sql;
236
		Source destination =  pesiConfig.getDestination();
237

    
238
		// Clear AdditionalTaxonSource
239
		sql = "DELETE FROM " + dbTableName;
240
		destination.setQuery(sql);
241
		destination.update(sql);
242
		return true;
243
	}
244

    
245
	/* (non-Javadoc)
246
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
247
	 */
248
	@Override
249
	protected boolean isIgnore(PesiExportState state) {
250
		return ! ( state.getConfig().isDoAdditionalTaxonSource());
251
	}
252

    
253
	/**
254
	 * Returns the <code>TaxonFk</code> attribute.
255
	 * @param reference The {@link Reference Reference}.
256
	 * @see MethodMapper
257
	 */
258
	@SuppressWarnings("unused")
259
	private static Integer getTaxonFk(Reference<?> reference, PesiExportState state) {
260
		// Reference parameter isn't needed, but the DbSingleAttributeExportMapperBase throws a type mismatch exception otherwise
261
		// since it awaits two parameters if one of them is of instance DbExportStateBase.
262
		Integer result = null;
263
		if (state != null && currentTaxonName != null) {
264
			result = state.getDbId(currentTaxonName);
265
		}
266
		return result;
267
	}
268
	
269
	/**
270
	 * Returns the <code>SourceFk</code> attribute.
271
	 * @param reference The {@link Reference Reference}.
272
	 * @return The <code>SourceFk</code> attribute.
273
	 * @see MethodMapper
274
	 */
275
	private static Integer getSourceFk(Reference<?> reference, PesiExportState state) {
276
		Integer result = null;
277
		if (state != null && reference != null) {
278
			result = state.getDbId(reference);
279
		}
280
		return result;
281
	}
282
	
283
	/**
284
	 * Returns the <code>SourceUseFk</code> attribute.
285
	 * @param reference The {@link Reference Reference}.
286
	 * @return The <code>SourceUseFk</code> attribute.
287
	 * @see MethodMapper
288
	 */
289
	private static Integer getSourceUseFk(Reference<?> reference) {
290
		// TODO
291
		Integer result = null;
292
		if (sourceUse_AdditionalSource) {
293
			result = PesiTransformer.sourceUseIdSourceUseId(3);
294
		} else if (sourceUse_SourceOfSynonymy) {
295
			result = PesiTransformer.sourceUseIdSourceUseId(4);
296
		} else if (sourceUse_NomenclaturalReference) {
297
			result = PesiTransformer.sourceUseIdSourceUseId(8);
298
		}
299
		return result;
300
	}
301
	
302
	/**
303
	 * Returns the <code>SourceUseCache</code> attribute.
304
	 * @param reference The {@link Reference Reference}.
305
	 * @return The <code>SourceUseCache</code> attribute.
306
	 * @see MethodMapper
307
	 */
308
	@SuppressWarnings("unused")
309
	private static String getSourceUseCache(Reference<?> reference) {
310
		// TODO
311
		String result = null;
312
		//CHANGED, use PesiTransformer.getAdditionalSourceCacheByKey instead
313
//		if (sourceUse_AdditionalSource) {
314
//			result = PesiTransformer.sourceUseId2SourceUseCache(3);
315
//		} else if (sourceUse_SourceOfSynonymy) {
316
//			result = PesiTransformer.sourceUseId2SourceUseCache(4);
317
//		} else if (sourceUse_NomenclaturalReference) {
318
//			result = PesiTransformer.sourceUseId2SourceUseCache(8);
319
//		}
320
		return result;
321
	}
322
	
323
	/**
324
	 * Returns the <code>SourceNameCache</code> attribute.
325
	 * @param reference The {@link Reference Reference}.
326
	 * @return The <code>SourceNameCache</code> attribute.
327
	 * @see MethodMapper
328
	 */
329
	@SuppressWarnings("unused")
330
	private static String getSourceNameCache(Reference<?> reference) {
331
		String result = null;
332
		if (reference != null) {
333
			result = reference.getTitle();
334
		}
335
		return result;
336
	}
337
	
338
	/**
339
	 * Returns the <code>SourceDetail</code> attribute.
340
	 * @param reference The {@link Reference Reference}.
341
	 * @return The <code>SourceDetail</code> attribute.
342
	 * @see MethodMapper
343
	 */
344
	@SuppressWarnings("unused")
345
	private static String getSourceDetail(Reference<?> reference) {
346
		return PesiAdditionalTaxonSourceExport.citationMicroReference;
347
	}
348

    
349
	/**
350
	 * @param citationMicroReference the citationMicroReference to set
351
	 */
352
	public static void setCitationMicroReference(String citationMicroReference) {
353
		PesiAdditionalTaxonSourceExport.citationMicroReference = citationMicroReference;
354
	}
355

    
356
	/**
357
	 * Returns the CDM to PESI specific export mappings.
358
	 * @return The {@link PesiExportMapping PesiExportMapping}.
359
	 */
360
	private PesiExportMapping getMapping() {
361
		PesiExportMapping mapping = new PesiExportMapping(dbTableName);
362
		
363
		mapping.addMapper(MethodMapper.NewInstance("TaxonFk", this.getClass(), "getTaxonFk", standardMethodParameter, PesiExportState.class));
364
		mapping.addMapper(MethodMapper.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter, PesiExportState.class));
365
		mapping.addMapper(MethodMapper.NewInstance("SourceUseFk", this));
366
		mapping.addMapper(MethodMapper.NewInstance("SourceUseCache", this));
367
		mapping.addMapper(MethodMapper.NewInstance("SourceNameCache", this));
368
		mapping.addMapper(MethodMapper.NewInstance("SourceDetail", this));
369
		
370
		return mapping;
371
	}
372

    
373
}
(1-1/6)