Project

General

Profile

« Previous | Next » 

Revision 5947a91a

Added by Andreas Müller almost 12 years ago

latest changes for PESI export, moving old IOs to extra package

View differences:

.gitattributes
1326 1326
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/indexFungorum/IndexFungorumTransformer.java -text
1327 1327
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/merging/FaunaEuErmsMerging.java -text
1328 1328
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/ExpertsAndLastActionMapper.java -text
1329
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiAdditionalTaxonSourceExport.java -text
1330 1329
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiCollectionExportMapping.java -text
1331 1330
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiDescriptionExport.java -text
1332 1331
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiExportBase.java -text
......
1334 1333
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiExportMapping.java -text
1335 1334
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiExportState.java -text
1336 1335
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiFinalUpdateExport.java -text
1337
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiImageExport.java -text
1338
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiNoteExport.java -text
1339
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiNoteSourceExport.java -text
1340
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiOccurrenceExport.java -text
1341
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiOccurrenceSourceExport.java -text
1342 1336
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiRelTaxonExport.java -text
1343 1337
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiSourceExport.java -text
1344 1338
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiTaxonExport.java -text
1345 1339
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiTransformer.java -text
1340
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/old/PesiAdditionalTaxonSourceExport.java -text
1341
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/old/PesiImageExport.java -text
1342
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/old/PesiNoteExport.java -text
1343
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/old/PesiNoteSourceExport.java -text
1344
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/old/PesiOccurrenceExport.java -text
1345
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/old/PesiOccurrenceSourceExport.java -text
1346 1346
cdm-pesi/src/main/resources/log4j.properties -text
1347 1347
cdm-pesi/src/test/java/eu/etaxonomy/cdm/io/common/TestConnection.java -text
1348 1348
cdm-pesi/src/test/java/eu/etaxonomy/cdm/io/erms/ermsErrors.txt -text
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiAdditionalTaxonSourceExport.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
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.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.common.DescriptionElementSource;
25
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26
import eu.etaxonomy.cdm.model.description.TaxonDescription;
27
import eu.etaxonomy.cdm.model.description.TextData;
28
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
29
import eu.etaxonomy.cdm.model.reference.Reference;
30
import eu.etaxonomy.cdm.model.taxon.Taxon;
31
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
32

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

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

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

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

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

  
85
			// Get the limit for objects to save within a single transaction.
86
//			int limit = state.getConfig().getLimitSave();
87
			int limit = 1000;
88

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

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

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

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

  
127
					if (taxonBase.isInstanceOf(Taxon.class)) {
128
						
129
						Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
130

  
131
						// Determine the TaxonDescriptions
132
						Set<TaxonDescription> taxonDescriptions = taxon.getDescriptions();
133

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

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

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

  
234
		// Clear AdditionalTaxonSource
235
		sql = "DELETE FROM " + dbTableName;
236
		destination.setQuery(sql);
237
		destination.update(sql);
238
		return true;
239
	}
240

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

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

  
345
	/**
346
	 * @param citationMicroReference the citationMicroReference to set
347
	 */
348
	public static void setCitationMicroReference(String citationMicroReference) {
349
		PesiAdditionalTaxonSourceExport.citationMicroReference = citationMicroReference;
350
	}
351

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

  
369
}
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiDescriptionExport.java
14 14
import java.sql.Connection;
15 15
import java.sql.PreparedStatement;
16 16
import java.sql.SQLException;
17
import java.sql.Types;
17 18
import java.util.Arrays;
18 19
import java.util.HashSet;
19 20
import java.util.List;
......
27 28
import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
28 29
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
29 30
import eu.etaxonomy.cdm.io.common.Source;
31
import eu.etaxonomy.cdm.io.common.mapping.DbIgnoreMapper;
30 32
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
31 33
import eu.etaxonomy.cdm.io.common.mapping.out.CollectionExportMapping;
32 34
import eu.etaxonomy.cdm.io.common.mapping.out.DbAreaMapper;
35
import eu.etaxonomy.cdm.io.common.mapping.out.DbConstantMapper;
33 36
import eu.etaxonomy.cdm.io.common.mapping.out.DbDescriptionElementTaxonMapper;
34 37
import eu.etaxonomy.cdm.io.common.mapping.out.DbDistributionStatusMapper;
35 38
import eu.etaxonomy.cdm.io.common.mapping.out.DbExportIgnoreMapper;
......
41 44
import eu.etaxonomy.cdm.io.common.mapping.out.DbSingleSourceMapper;
42 45
import eu.etaxonomy.cdm.io.common.mapping.out.DbStringMapper;
43 46
import eu.etaxonomy.cdm.io.common.mapping.out.DbTextDataMapper;
47
import eu.etaxonomy.cdm.io.common.mapping.out.DbTimePeriodMapper;
44 48
import eu.etaxonomy.cdm.io.common.mapping.out.IdMapper;
45 49
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
46 50
import eu.etaxonomy.cdm.io.profiler.ProfilerController;
......
64 68
import eu.etaxonomy.cdm.model.location.TdwgArea;
65 69
import eu.etaxonomy.cdm.model.name.NonViralName;
66 70
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
71
import eu.etaxonomy.cdm.model.reference.Reference;
67 72
import eu.etaxonomy.cdm.model.taxon.Taxon;
68 73
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
69 74
/**
......
180 185
		occurrenceMapping.initialize(state);
181 186

  
182 187
		// Get specific mappings: (CDM) DescriptionElement -> (PESI) Additional taxon source
183
		PesiExportMapping addittionalSourceMapping = getAdditionalTaxonSourceMapping();
184
		addittionalSourceMapping.initialize(state);
188
		PesiExportMapping addSourceSourceMapping = getAddTaxonSourceSourceMapping();
189
		addSourceSourceMapping.initialize(state);
190
		PesiExportMapping additionalSourceMapping = getAdditionalTaxonSourceMapping();
191
		additionalSourceMapping.initialize(state);
185 192

  
186 193
		// Get specific mappings: (CDM) DescriptionElement -> (PESI) Additional taxon source
187 194
		PesiExportMapping vernacularMapping = getVernacularNamesMapping();
......
212 219
			for (Taxon taxon : list) {
213 220
				countTaxa++;
214 221
				doCount(count++, modCount, pluralString);
215
				success &= handleSingleTaxon(taxon, state, notesMapping, occurrenceMapping, addittionalSourceMapping, vernacularMapping, imageMapping);	
222
				success &= handleSingleTaxon(taxon, state, notesMapping, occurrenceMapping, addSourceSourceMapping, 
223
						additionalSourceMapping, vernacularMapping, imageMapping);	
216 224
			}
217 225
			list = null;
218 226
			state.setCurrentTaxon(null);
......
277 285
	}
278 286

  
279 287
	private boolean handleSingleTaxon(Taxon taxon, PesiExportState state, PesiExportMapping notesMapping, PesiExportMapping occurrenceMapping,
280
			PesiExportMapping addittionalSourceMapping, PesiExportMapping vernacularMapping, PesiExportMapping imageMapping) throws SQLException {
288
			PesiExportMapping addSourceSourceMapping, PesiExportMapping additionalSourceMapping, 
289
			PesiExportMapping vernacularMapping, PesiExportMapping imageMapping) throws SQLException {
281 290
		boolean success = true;
282 291
		Set<DescriptionBase<?>> descriptions = new HashSet<DescriptionBase<?>>();
283 292
		descriptions.addAll(taxon.getDescriptions());
......
292 301
			boolean isImageGallery = desc.isImageGallery();
293 302
			for (DescriptionElementBase element : desc.getElements()){
294 303
				success &= handleDescriptionElement(state, notesMapping, occurrenceMapping, vernacularMapping, imageMapping,
295
						isImageGallery, element);
304
						addSourceSourceMapping, additionalSourceMapping, isImageGallery, element);
296 305
			}
297 306
		}
298 307
		return success;
......
300 309

  
301 310
	private boolean handleDescriptionElement(PesiExportState state, PesiExportMapping notesMapping,
302 311
			PesiExportMapping occurrenceMapping, PesiExportMapping vernacularMapping, PesiExportMapping imageMapping, 
303
			boolean isImageGallery, DescriptionElementBase element) throws SQLException {
312
			PesiExportMapping addSourceSourceMapping, PesiExportMapping additionalSourceMapping, boolean isImageGallery, DescriptionElementBase element) throws SQLException {
304 313
		try {
305 314
			boolean success = true;
306 315
			if (isImageGallery){
......
310 319
			}else if (isCommonName(element)){
311 320
				countCommonName++;
312 321
				if (element.isInstanceOf(TextData.class)){
313
					//
322
					//we do not import text data common names
314 323
				}else{
315 324
					success &= vernacularMapping.invoke(element);
316 325
				}
......
326 335
				}
327 336
			}else if (isAdditionalTaxonSource(element)){
328 337
				countAdditionalSources++;
329
//					success &= addittionalSourceMapping.invoke(element);
338
				success &= addSourceSourceMapping.invoke(element);
339
				success &= additionalSourceMapping.invoke(element);
330 340
			}else if (isExcludedNote(element)){
331 341
				//do nothing
332 342
			}else if (isPesiNote(element)){
......
418 428
		if (feature == null){
419 429
			return false;
420 430
		}
421
		return (feature.equals(Feature.CITATION()));
431
		return (feature.equals(Feature.CITATION()) || feature.equals(Feature.ADDITIONAL_PUBLICATION()));
422 432
	}
423 433

  
424 434
	private boolean isOccurrence(DescriptionElementBase element) {
......
607 617
	 * @return The <code>Note_2</code> attribute.
608 618
	 * @see MethodMapper
609 619
	 */
610
	@SuppressWarnings("unused")
611
	private static String getNote_2(DescriptionElementBase descriptionElement) {
612
		logger.warn("Not yet implemented");
620
	@SuppressWarnings("unused") //used for mapper
621
	private static String getNote_2(DescriptionElementBase element) {
622
		//E+M map links -> medium
623
		if (element.getFeature() != null && element.getFeature().getUuid().equals(BerlinModelTransformer.uuidFeatureMaps)){
624
			String text = CdmBase.deproxy(element, TextData.class).getText(Language.ENGLISH());
625
			if (text.contains("medium")){
626
				return "medium";
627
			}
628
		}
613 629
		return null;
614 630
	}
615 631

  
......
782 798
		mapping.addMapper(IdMapper.NewInstance("NoteId"));
783 799
		mapping.addMapper(DbTextDataMapper.NewInstance(Language.ENGLISH(), "Note_1"));
784 800
		//TODO
785
		mapping.addMapper(DbExportNotYetImplementedMapper.NewInstance("Note_2", "Need to research what Note_2 is for"));
801
		mapping.addMapper(MethodMapper.NewInstance("Note_2", this, DescriptionElementBase.class));
786 802
		mapping.addMapper(MethodMapper.NewInstance("NoteCategoryFk", this, DescriptionElementBase.class ));
787 803
		
788 804
		mapping.addMapper(MethodMapper.NewInstance("NoteCategoryCache", this, DescriptionElementBase.class, PesiExportState.class ));
......
849 865

  
850 866
		return mapping;
851 867
	}
868
	
869

  
870
	/**
871
	 * Returns the CDM to PESI specific export mappings for additional taxon sources to create a new
872
	 * source for the additional source
873
	 * @see #{@link PesiDescriptionExport#getAdditionalTaxonSourceMapping()}
874
	 * @return The {@link PesiExportMapping PesiExportMapping}.
875
	 */
876
	private PesiExportMapping getAddTaxonSourceSourceMapping() {
877
		PesiExportMapping sourceMapping = new PesiExportMapping(PesiSourceExport.dbTableName);
878
		
879
		sourceMapping.addMapper(IdMapper.NewInstance("SourceId"));
880
		sourceMapping.addMapper(DbConstantMapper.NewInstance("SourceCategoryFk", Types.INTEGER, PesiTransformer.REF_UNRESOLVED));
881
		sourceMapping.addMapper(DbConstantMapper.NewInstance("SourceCategoryCache", Types.VARCHAR, PesiTransformer.REF_STR_UNRESOLVED));
882
		
883
//		sourceMapping.addMapper(MethodMapper.NewInstance("NomRefCache", PesiSourceExport.class, "getNomRefCache", Reference.class));
884
		
885
		sourceMapping.addMapper(DbTextDataMapper.NewInstance(Language.ENGLISH(), "NomRefCache"));
886
		
887
		return sourceMapping;
888
	}
852 889

  
890
	
853 891
	/**
854 892
	 * Returns the CDM to PESI specific export mappings for additional taxon sources.
893
	 * @see #{@link PesiDescriptionExport#getAddTaxonSourceSourceMapping()}
855 894
	 * @return The {@link PesiExportMapping PesiExportMapping}.
856 895
	 */
857 896
	private PesiExportMapping getAdditionalTaxonSourceMapping() {
897
	
858 898
		PesiExportMapping mapping = new PesiExportMapping(dbAdditionalSourceTableName);
859 899
		
860 900
		mapping.addMapper(MethodMapper.NewInstance("TaxonFk", this, DescriptionElementBase.class, PesiExportState.class));
861 901
		
862
		mapping.addMapper(DbSingleSourceMapper.NewInstance("SourceFk", of (DbSingleSourceMapper.EXCLUDE.WITH_ID) , ! IS_CACHE));
863
		mapping.addMapper(DbSingleSourceMapper.NewInstance("SourceNameCache", of(DbSingleSourceMapper.EXCLUDE.WITH_ID) , ! IS_CACHE));
902
		mapping.addMapper(IdMapper.NewInstance("SourceFk"));
903
		mapping.addMapper(DbTextDataMapper.NewInstance(Language.ENGLISH(), "SourceNameCache"));
904
		
905
		mapping.addMapper(DbConstantMapper.NewInstance("SourceUseFk", Types.INTEGER, PesiTransformer.NOMENCLATURAL_REFERENCE));
906
		mapping.addMapper(DbConstantMapper.NewInstance("SourceUseCache", Types.VARCHAR, PesiTransformer.STR_NOMENCLATURAL_REFERENCE));
864 907
		
865
//		mapping.addMapper(MethodMapper.NewInstance("SourceUseFk", this));
866
//		mapping.addMapper(MethodMapper.NewInstance("SourceUseCache", this));
867
//		mapping.addMapper(MethodMapper.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter, PesiExportState.class));
868
//		mapping.addMapper(MethodMapper.NewInstance("SourceNameCache", this));
869
//		mapping.addMapper(MethodMapper.NewInstance("SourceDetail", this));
908
//		mapping.addMapper(DbIgnoreMapper.NewInstance("SourceDetail", "SourceDetails not available for additional sources"));
870 909
		
871 910
		return mapping;
872 911
	}
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiExportBase.java
31 31
import eu.etaxonomy.cdm.model.common.MarkerType;
32 32
import eu.etaxonomy.cdm.model.common.RelationshipBase;
33 33
import eu.etaxonomy.cdm.model.description.Feature;
34
import eu.etaxonomy.cdm.model.description.TaxonDescription;
34 35
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
36
import eu.etaxonomy.cdm.model.description.TextData;
35 37
import eu.etaxonomy.cdm.model.name.BotanicalName;
36 38
import eu.etaxonomy.cdm.model.name.HybridRelationship;
37 39
import eu.etaxonomy.cdm.model.name.NameRelationship;
......
416 418
	protected Object getDbIdCdmWithExceptions(CdmBase cdmBase, PesiExportState state) {
417 419
		if (cdmBase.isInstanceOf(TaxonNameBase.class)){
418 420
			return ( cdmBase.getId() + state.getConfig().getNameIdStart() );
421
		}if (isAdditionalSource(cdmBase) ){
422
			return ( cdmBase.getId() + 2 * state.getConfig().getNameIdStart() );  //make it a separate variable if conflicts occur.
419 423
		}else{
420 424
			return super.getDbIdCdmWithExceptions(cdmBase, state);
421 425
		}
......
423 427
	
424 428
	
425 429
	
430
	private boolean isAdditionalSource(CdmBase cdmBase) {
431
		if (cdmBase.isInstanceOf(TextData.class)){
432
			TextData textData = CdmBase.deproxy(cdmBase, TextData.class);
433
			if (textData.getFeature().equals(Feature.ADDITIONAL_PUBLICATION()) ||
434
					textData.getFeature().equals(Feature.CITATION())){
435
				return true;
436
			}
437
		}
438
		return false;
439
	}
440

  
441

  
426 442
	protected MarkerType getUuidMarkerType(UUID uuid, PesiExportState state){
427 443
		if (uuid == null){
428 444
			uuid = UUID.randomUUID();
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiImageExport.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.net.URI;
13
import java.sql.Connection;
14
import java.sql.PreparedStatement;
15
import java.sql.SQLException;
16
import java.util.List;
17
import java.util.Set;
18

  
19
import org.apache.log4j.Logger;
20
import org.springframework.stereotype.Component;
21
import org.springframework.transaction.TransactionStatus;
22

  
23
import eu.etaxonomy.cdm.io.common.Source;
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26
import eu.etaxonomy.cdm.model.description.TaxonDescription;
27
import eu.etaxonomy.cdm.model.description.TextData;
28
import eu.etaxonomy.cdm.model.media.Media;
29
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
30
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
31
import eu.etaxonomy.cdm.model.reference.Reference;
32
import eu.etaxonomy.cdm.model.taxon.Taxon;
33
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
34

  
35
/**
36
 * The export class for Images.
37
 * Inserts into DataWarehouse database table <code>Image</code>.
38
 * @author e.-m.lee
39
 * @date 18.08.2010
40
 *
41
 */
42
@Component
43
public class PesiImageExport extends PesiExportBase {
44
	private static final Logger logger = Logger.getLogger(PesiImageExport.class);
45
	private static final Class<? extends CdmBase> standardMethodParameter = Reference.class;
46

  
47
	private static int modCount = 1000;
48
	private static final String dbTableName = "Image";
49
	private static final String pluralString = "DescriptionElements";
50
	private static final String parentPluralString = "Taxa";
51
	
52
	public PesiImageExport() {
53
		super();
54
	}
55

  
56
	/* (non-Javadoc)
57
	 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
58
	 */
59
	@Override
60
	public Class<? extends CdmBase> getStandardMethodParameter() {
61
		return standardMethodParameter;
62
	}
63

  
64
	/* (non-Javadoc)
65
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
66
	 */
67
	@Override
68
	protected boolean doCheck(PesiExportState state) {
69
		boolean result = true;
70
		return result;
71
	}
72

  
73
	/* (non-Javadoc)
74
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
75
	 */
76
	@Override
77
	protected void doInvoke(PesiExportState state) {
78

  
79
		logger.error("*** Started Making " + pluralString + " ...");
80

  
81
		// Get the limit for objects to save within a single transaction.
82
//			int limit = state.getConfig().getLimitSave();
83
		int limit = 1000;
84

  
85
		// PESI: Clear the database table Image.
86
		doDelete(state);
87

  
88
		// PESI: Create the Images
89
		int count = 0;
90
		int taxonCount = 0;
91
		int pastCount = 0;
92
		TransactionStatus txStatus = null;
93
		List<TaxonBase> list = null;
94

  
95
		Connection connection = state.getConfig().getDestination().getConnection();
96
		// Start transaction
97
		txStatus = startTransaction(true);
98
		logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
99
		while ((list = getTaxonService().list(null, limit, taxonCount, null, null)).size() > 0) {
100

  
101
			taxonCount += list.size();
102
			logger.error("Fetched " + list.size() + " " + parentPluralString + ".");
103
			
104
			logger.error("Check for Images...");
105
			for (TaxonBase taxonBase : list) {
106
				
107
				if (taxonBase.isInstanceOf(Taxon.class)) {
108
					
109
					Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
110

  
111
					// Determine the TaxonDescriptions
112
					Set<TaxonDescription> taxonDescriptions = taxon.getDescriptions();
113

  
114
					// Determine the DescriptionElements (Citations) for the current Taxon
115
					for (TaxonDescription taxonDescription : taxonDescriptions) {
116
						
117
						// Check whether this TaxonDescription contains images
118
						if (taxonDescription.isImageGallery()) {
119
							
120
							Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
121
							
122
							for (DescriptionElementBase descriptionElement : descriptionElements) {
123
								if (descriptionElement.isInstanceOf(TextData.class)) {
124
									List<Media> media = descriptionElement.getMedia();
125
									
126
									for (Media image : media) {
127
										Set<MediaRepresentation> representations = image.getRepresentations();
128
										
129
										for (MediaRepresentation representation : representations) {
130
											List<MediaRepresentationPart> representationParts = representation.getParts();
131
											
132
											for (MediaRepresentationPart representationPart : representationParts) {
133
												URI mediaUri = representationPart.getUri();
134
												
135
												// Add image data
136
												String thumb = null;
137
												Integer taxonFk = state.getDbId(taxonBase.getName());
138
												
139
												if (taxonFk != null && mediaUri != null) {
140
													doCount(count++, modCount, pluralString);
141
													invokeImages(thumb, mediaUri, taxonFk, connection);
142
												}
143
											}
144
										}
145
										
146
									}
147
								}
148
							}
149
						
150
						}
151
						
152
					}
153
				}
154
				
155
			}
156
			logger.error("Exported " + (count - pastCount) + " " + pluralString + ".");
157

  
158
			// Commit transaction
159
			commitTransaction(txStatus);
160
			logger.error("Committed transaction.");
161
			logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
162
			pastCount = count;
163

  
164
			// Start transaction
165
			txStatus = startTransaction(true);
166
			logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
167
		}
168
		if (list.size() == 0) {
169
			logger.error("No " + pluralString + " left to fetch.");
170
		}
171
		// Commit transaction
172
		commitTransaction(txStatus);
173
		logger.error("Committed transaction.");
174

  
175
		logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(true));
176
		
177
		return;
178
	}
179

  
180
	/**
181
	 * Inserts image data into the Image datawarehouse table.
182
	 * @param thumb
183
	 * @param url
184
	 * @param taxonFk
185
	 * @param connection
186
	 */
187
	private void invokeImages(String thumb, URI url, Integer taxonFk, Connection connection) {
188
		String imagesSql = "INSERT INTO Image (taxonFk, img_thumb, img_url) VALUES" +
189
				" (?, ?, ?)";
190
		try {
191
			PreparedStatement imagesStmt = connection.prepareStatement(imagesSql);
192
			
193
			if (taxonFk != null) {
194
				imagesStmt.setInt(1, taxonFk);
195
			} else {
196
				imagesStmt.setObject(1, null);
197
			}
198

  
199
			if (thumb != null) {
200
				imagesStmt.setString(2, thumb);
201
			} else {
202
				imagesStmt.setObject(2, null);
203
			}
204
			
205
			if (url != null) {
206
				imagesStmt.setString(3, url.toString());
207
			} else {
208
				imagesStmt.setObject(3, null);
209
			}
210
			
211
			imagesStmt.executeUpdate();
212
		} catch (SQLException e) {
213
			logger.error("Image could not be created. TaxonFk: " + taxonFk + ", Thumb: " + thumb + ", URL: " + url);
214
			e.printStackTrace();
215
		}
216

  
217
	}
218

  
219
	/**
220
	 * Deletes all entries of database tables related to <code>AdditionalTaxonSource</code>.
221
	 * @param state The {@link PesiExportState PesiExportState}.
222
	 * @return Whether the delete operation was successful or not.
223
	 */
224
	protected boolean doDelete(PesiExportState state) {
225
		PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
226
		
227
		String sql;
228
		Source destination =  pesiConfig.getDestination();
229

  
230
		// Clear AdditionalTaxonSource
231
		sql = "DELETE FROM " + dbTableName;
232
		destination.setQuery(sql);
233
		destination.update(sql);
234
		return true;
235
	}
236

  
237
	/* (non-Javadoc)
238
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
239
	 */
240
	@Override
241
	protected boolean isIgnore(PesiExportState state) {
242
		return ! ( state.getConfig().isDoImages());
243
	}
244

  
245
}
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiNoteExport.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.Connection;
13
import java.sql.PreparedStatement;
14
import java.sql.SQLException;
15
import java.util.Arrays;
16
import java.util.List;
17
import java.util.Map;
18
import java.util.Set;
19

  
20
import org.apache.log4j.Logger;
21
import org.joda.time.DateTime;
22
import org.springframework.stereotype.Component;
23
import org.springframework.transaction.TransactionStatus;
24

  
25
import eu.etaxonomy.cdm.io.common.DbExportStateBase;
26
import eu.etaxonomy.cdm.io.common.Source;
27
import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
28
import eu.etaxonomy.cdm.io.common.mapping.out.IdMapper;
29
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.common.Extension;
32
import eu.etaxonomy.cdm.model.common.ExtensionType;
33
import eu.etaxonomy.cdm.model.common.Language;
34
import eu.etaxonomy.cdm.model.common.LanguageString;
35
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
36
import eu.etaxonomy.cdm.model.description.DescriptionBase;
37
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
38
import eu.etaxonomy.cdm.model.description.Distribution;
39
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
40
import eu.etaxonomy.cdm.model.description.TaxonDescription;
41
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
42
import eu.etaxonomy.cdm.model.description.TextData;
43
import eu.etaxonomy.cdm.model.location.NamedArea;
44
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
45
import eu.etaxonomy.cdm.model.taxon.Taxon;
46
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
47

  
48
/**
49
 * The export class for {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase DescriptionElements}.<p>
50
 * Inserts into DataWarehouse database table <code>Note</code>.<p>
51
 * It is divided into two phases:<ul>
52
 * <li>Phase 1:	Export of DescriptionElements as Notes.
53
 * <li>Phase 2:	Export of TaxonName extensions <code>taxComment</code>, <code>fauComment</code> and <code>fauExtraCodes</code> as Notes.</ul>
54
 * @author e.-m.lee
55
 * @date 23.02.2010
56
 *
57
 */
58
@Component
59
public class PesiNoteExport extends PesiExportBase {
60
	private static final Logger logger = Logger.getLogger(PesiNoteExport.class);
61
	private static final Class<? extends CdmBase> standardMethodParameter = DescriptionElementBase.class;
62

  
63
	private static int modCount = 1000;
64
	private static final String dbTableName = "Note";
65
	private static final String pluralString = "Notes";
66
	private static final String parentPluralString = "Taxa";
67

  
68
	public PesiNoteExport() {
69
		super();
70
	}
71

  
72
	/* (non-Javadoc)
73
	 * @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
74
	 */
75
	@Override
76
	public Class<? extends CdmBase> getStandardMethodParameter() {
77
		return standardMethodParameter;
78
	}
79

  
80
	/* (non-Javadoc)
81
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
82
	 */
83
	@Override
84
	protected boolean doCheck(PesiExportState state) {
85
		boolean result = true;
86
		return result;
87
	}
88

  
89
	/* (non-Javadoc)
90
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
91
	 */
92
	@Override
93
	protected void doInvoke(PesiExportState state) {
94
		try {
95
			logger.info("*** Started Making " + pluralString + " ...");
96

  
97
			// Get the limit for objects to save within a single transaction.
98
			int limit = state.getConfig().getLimitSave();
99
			
100
			// Stores whether this invoke was successful or not.
101
			boolean success = true;
102
	
103
			// PESI: Clear the database table Note.
104
			doDelete(state);
105
		
106
			// Start transaction
107
			success &= doPhase01(state);
108

  
109
			
110
			logger.info("PHASE 2...");
111
			doPhase02(state, limit);
112

  
113

  
114
			logger.info("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
115
			
116
			if (!success){
117
				state.setUnsuccessfull();
118
			}
119
			return;
120
		} catch (SQLException e) {
121
			e.printStackTrace();
122
			logger.error(e.getMessage());
123
			state.setUnsuccessfull();
124
		}
125
	}
126

  
127
	//PHASE 01: Description Elements
128
	private boolean doPhase01(PesiExportState state) throws SQLException {
129
		logger.info("PHASE 1...");
130
		int count = 0;
131
		int pastCount = 0;
132
		 boolean success = true;
133
		
134
		 // Calculate the pageNumber
135
		int pageNumber = 1;
136
		int pageSize = 1000;
137

  
138

  
139
		// Get specific mappings: (CDM) DescriptionElement -> (PESI) Note
140
		PesiExportMapping mapping = getMapping();
141

  
142
		// Initialize the db mapper
143
		mapping.initialize(state);
144

  
145
		
146
		List<DescriptionElementBase> list = null;
147
		
148
		TransactionStatus txStatus = startTransaction(true);
149
		logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
150
		List<String> propPath = Arrays.asList(new String[]{"inDescription.taxon"});
151
		while ((list = getDescriptionService().listDescriptionElements(null, null, null, pageSize, pageNumber, propPath)).size() > 0) {
152

  
153
			logger.info("Fetched " + list.size() + " " + pluralString + ". Exporting...");
154
			for (DescriptionElementBase descriptionElement : list) {
155
				if (getNoteCategoryFk(descriptionElement) != null) {
156
					doCount(count++, modCount, pluralString);
157
					success &= mapping.invoke(descriptionElement);
158
				}
159
			}
160

  
161
			// Commit transaction
162
			commitTransaction(txStatus);
163
			logger.debug("Committed transaction.");
164
			logger.info("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
165
			pastCount = count;
166

  
167
			// Start transaction
168
			txStatus = startTransaction(true);
169
			logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
170
			
171
			// Increment pageNumber
172
			pageNumber++;
173
		}
174
		if (list.size() == 0) {
175
			logger.info("No " + pluralString + " left to fetch.");
176
		}
177
		// Commit transaction
178
		commitTransaction(txStatus);
179
		logger.info("Committed transaction.");
180
		return success;
181
	}
182

  
183
	//PHASE 02: Taxa extensions
184
	private void doPhase02(PesiExportState state, int limit) {
185
		TransactionStatus txStatus;
186
		txStatus = startTransaction(true);
187
		ExtensionType taxCommentExtensionType = (ExtensionType)getTermService().find(PesiTransformer.taxCommentUuid);
188
		ExtensionType fauCommentExtensionType = (ExtensionType)getTermService().find(PesiTransformer.fauCommentUuid);
189
		ExtensionType fauExtraCodesExtensionType = (ExtensionType)getTermService().find(PesiTransformer.fauExtraCodesUuid);
190
		List<TaxonBase> taxonBaseList = null;
191
		
192
		int count = 0;
193
		int pastCount = 0;
194
		Connection connection = state.getConfig().getDestination().getConnection();
195
		logger.info("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
196
		//logger.warn("TODO handle extensions on taxon level, not name level (");
197
		while ((taxonBaseList = getTaxonService().list(null, limit, count, null, null)).size() > 0) {
198

  
199
			logger.info("Fetched " + taxonBaseList.size() + " names. Exporting...");
200
			for (TaxonBase<?> taxon : taxonBaseList) {
201
				Set<Extension> extensions = taxon.getExtensions();
202
				for (Extension extension : extensions) {
203
					if (extension.getType().equals(taxCommentExtensionType)) {
204
						String taxComment = extension.getValue();
205
						invokeNotes(taxComment, 
206
								PesiTransformer.getNoteCategoryFk(PesiTransformer.taxCommentUuid), 
207
								PesiTransformer.getNoteCategoryCache(PesiTransformer.taxCommentUuid),
208
								null, null, getTaxonFk(taxon.getName(), state),connection);
209
					} else if (extension.getType().equals(fauCommentExtensionType)) {
210
						String fauComment = extension.getValue();
211
						invokeNotes(fauComment, 
212
								PesiTransformer.getNoteCategoryFk(PesiTransformer.fauCommentUuid), 
213
								PesiTransformer.getNoteCategoryCache(PesiTransformer.fauCommentUuid),
214
								null, null, getTaxonFk(taxon.getName(), state),connection);
215
					} else if (extension.getType().equals(fauExtraCodesExtensionType)) {
216
						String fauExtraCodes = extension.getValue();
217
						invokeNotes(fauExtraCodes, 
218
								PesiTransformer.getNoteCategoryFk(PesiTransformer.fauExtraCodesUuid), 
219
								PesiTransformer.getNoteCategoryCache(PesiTransformer.fauExtraCodesUuid),
220
								null, null, getTaxonFk(taxon.getName(), state),connection);
221
					}
222
				}
223
				
224
				doCount(count++, modCount, pluralString);
225
			}
226

  
227
			// Commit transaction
228
			commitTransaction(txStatus);
229
			logger.debug("Committed transaction.");
230
			logger.info("Exported " + (count - pastCount) + " names. Total: " + count);
231
			pastCount = count;
232

  
233
			// Start transaction
234
			txStatus = startTransaction(true);
235
			logger.info("Started new transaction. Fetching some taxa first (max: " + limit + ") ...");
236
		}
237
		if (taxonBaseList.size() == 0) {
238
			logger.info("No taxa left to fetch.");
239
		}
240
		// Commit transaction
241
		commitTransaction(txStatus);
242
		logger.debug("Committed transaction.");
243
	}
244

  
245
	/**
246
	 * @param taxComment
247
	 * @param noteCategoryFk
248
	 * @param noteCategoryCache
249
	 * @param object
250
	 * @param object2
251
	 */
252
	private void invokeNotes(String note, Integer noteCategoryFk,
253
			String noteCategoryCache, Integer languageFk, String languageCache, 
254
			Integer taxonFk, Connection connection) {
255
		String notesSql = "UPDATE Note SET Note_1 = ?, NoteCategoryFk = ?, NoteCategoryCache = ?, LanguageFk = ?, LanguageCache = ? WHERE TaxonFk = ?"; 
256
		try {
257
			PreparedStatement notesStmt = connection.prepareStatement(notesSql);
258
			
259
			if (note != null) {
260
				notesStmt.setString(1, note);
261
			} else {
262
				notesStmt.setObject(1, null);
263
			}
264
			
265
			if (noteCategoryFk != null) {
266
				notesStmt.setInt(2, noteCategoryFk);
267
			} else {
268
				notesStmt.setObject(2, null);
269
			}
270
			
271
			if (noteCategoryCache != null) {
272
				notesStmt.setString(3, noteCategoryCache);
273
			} else {
274
				notesStmt.setObject(3, null);
275
			}
276
			
277
			if (languageFk != null) {
278
				notesStmt.setInt(4, languageFk);
279
			} else {
280
				notesStmt.setObject(4, null);
281
			}
282
			
283
			if (languageCache != null) {
284
				notesStmt.setString(5, languageCache);
285
			} else {
286
				notesStmt.setObject(5, null);
287
			}
288
			
289
			if (taxonFk != null) {
290
				notesStmt.setInt(6, taxonFk);
291
			} else {
292
				notesStmt.setObject(6, null);
293
			}
294
			
295
			notesStmt.executeUpdate();
296
		} catch (SQLException e) {
297
			logger.error("Note could not be created: " + note);
298
			e.printStackTrace();
299
		}
300

  
301

  
302
	}
303

  
304
	/**
305
	 * Deletes all entries of database tables related to <code>Note</code>.
306
	 * @param state The PesiExportState
307
	 * @return Whether the delete operation was successful or not.
308
	 */
309
	protected boolean doDelete(PesiExportState state) {
310
		PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
311
		
312
		String sql;
313
		Source destination =  pesiConfig.getDestination();
314

  
315
		// Clear NoteSource
316
		sql = "DELETE FROM NoteSource";
317
		destination.setQuery(sql);
318
		destination.update(sql);
319

  
320
		// Clear Note
321
		sql = "DELETE FROM " + dbTableName;
322
		destination.setQuery(sql);
323
		destination.update(sql);
324
		return true;
325
	}
326

  
327
	/* (non-Javadoc)
328
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
329
	 */
330
	@Override
331
	protected boolean isIgnore(PesiExportState state) {
332
		return ! state.getConfig().isDoNotes();
333
	}
334

  
335
	/**
336
	 * Returns the <code>Note_1</code> attribute.
337
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
338
	 * @return The <code>Note_1</code> attribute.
339
	 * @see MethodMapper
340
	 */
341
	@SuppressWarnings("unused")
342
	private static String getNote_1(DescriptionElementBase descriptionElement) {
343
		String result = null;
344

  
345
		if (descriptionElement.isInstanceOf(TextData.class)) {
346
			TextData textData = CdmBase.deproxy(descriptionElement, TextData.class);
347
			result = textData.getText(Language.DEFAULT());
348
		}
349

  
350
		return result;
351
	}
352

  
353
	/**
354
	 * Returns the <code>Note_2</code> attribute.
355
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
356
	 * @return The <code>Note_2</code> attribute.
357
	 * @see MethodMapper
358
	 */
359
	@SuppressWarnings("unused")
360
	private static String getNote_2(DescriptionElementBase descriptionElement) {
361
		logger.warn("Not yet implemented");
362
		return null;
363
	}
364

  
365
	/**
366
	 * Returns the <code>NoteCategoryFk</code> attribute.
367
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
368
	 * @return The <code>NoteCategoryFk</code> attribute.
369
	 * @see MethodMapper
370
	 */
371
	private static Integer getNoteCategoryFk(DescriptionElementBase descriptionElement) {
372
		Integer result = null;
373
		result = PesiTransformer.feature2NoteCategoryFk(descriptionElement.getFeature());
374
		return result;
375
	}
376
	
377
	/**
378
	 * Returns the <code>NoteCategoryCache</code> attribute.
379
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
380
	 * @return The <code>NoteCategoryCache</code> attribute.
381
	 * @see MethodMapper
382
	 */
383
	@SuppressWarnings("unused")
384
	private static String getNoteCategoryCache(DescriptionElementBase descriptionElement, PesiExportState state) {
385
		return state.getTransformer().getCacheByFeature(descriptionElement.getFeature());
386
	}
387

  
388
	/**
389
	 * Returns the <code>LanguageFk</code> attribute.
390
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
391
	 * @return The <code>LanguageFk</code> attribute.
392
	 * @see MethodMapper
393
	 */
394
	@SuppressWarnings("unused")
395
	private static Integer getLanguageFk(DescriptionElementBase descriptionElement) {
396
		Language language = getLanguage(descriptionElement);
397

  
398
		return PesiTransformer.language2LanguageId(language);
399
	}
400

  
401
	/**
402
	 * Returns the <code>LanguageCache</code> attribute.
403
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
404
	 * @return The <code>LanguageCache</code> attribute.
405
	 * @throws UndefinedTransformerMethodException 
406
	 * @see MethodMapper
407
	 */
408
	@SuppressWarnings("unused")
409
	private static String getLanguageCache(DescriptionElementBase descriptionElement, PesiExportState state) throws UndefinedTransformerMethodException {
410
		Language language = getLanguage(descriptionElement);
411
		return state.getTransformer().getCacheByLanguage(language);
412
	}
413

  
414
	private static Language getLanguage(DescriptionElementBase descriptionElement) {
415
		Language language = null;
416

  
417
		Map<Language, LanguageString> multilanguageText = null;
418
		if (descriptionElement.isInstanceOf(CommonTaxonName.class)) {
419
			CommonTaxonName commonTaxonName = CdmBase.deproxy(descriptionElement, CommonTaxonName.class);
420
			language = commonTaxonName.getLanguage();
421
		} else if (descriptionElement.isInstanceOf(TextData.class)) {
422
			TextData textData = CdmBase.deproxy(descriptionElement, TextData.class);
423
			multilanguageText = textData.getMultilanguageText();
424
		} else if (descriptionElement.isInstanceOf(IndividualsAssociation.class)) {
425
			IndividualsAssociation individualsAssociation = CdmBase.deproxy(descriptionElement, IndividualsAssociation.class);
426
			multilanguageText = individualsAssociation.getDescription();
427
		} else if (descriptionElement.isInstanceOf(TaxonInteraction.class)) {
428
			TaxonInteraction taxonInteraction = CdmBase.deproxy(descriptionElement, TaxonInteraction.class);
429
			multilanguageText = taxonInteraction.getDescriptions();
430
		} else {
431
			logger.debug("Given descriptionElement does not support languages. Hence LanguageCache could not be determined: " + descriptionElement.getUuid());
432
		}
433
		
434
		if (multilanguageText != null) {
435
			Set<Language> languages = multilanguageText.keySet();
436

  
437
			// TODO: Think of something more sophisticated than this
438
			if (languages.size() > 0) {
439
				language = languages.iterator().next();
440
			}
441
			if (languages.size() > 1){
442
				logger.warn("There is more than 1 language for a given description (" + descriptionElement.getClass().getSimpleName() + "):" + descriptionElement.getUuid());
443
			}
444
		}
445
		return language;
446
	}
447

  
448
	/**
449
	 * Returns the <code>Region</code> attribute.
450
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
451
	 * @return The <code>Region</code> attribute.
452
	 * @see MethodMapper
453
	 */
454
	@SuppressWarnings("unused")
455
	private static String getRegion(DescriptionElementBase descriptionElement) {
456
		String result = null;
457
		DescriptionBase<?> inDescription = descriptionElement.getInDescription();
458
		
459
		try {
460
			// Area information are associated to TaxonDescriptions and Distributions.
461
			if (descriptionElement.isInstanceOf(Distribution.class)) {
462
				Distribution distribution = CdmBase.deproxy(descriptionElement, Distribution.class);
463
				//TODO not working any more after transformer refactoring
464
				result = new PesiTransformer(null).getCacheByNamedArea(distribution.getArea());
465
			} else if (inDescription != null && inDescription.isInstanceOf(TaxonDescription.class)) {
466
				TaxonDescription taxonDescription = CdmBase.deproxy(inDescription, TaxonDescription.class);
467
				Set<NamedArea> namedAreas = taxonDescription.getGeoScopes();
468
				if (namedAreas.size() == 1) {
469
					result = new PesiTransformer(null).getCacheByNamedArea(namedAreas.iterator().next());
470
				} else if (namedAreas.size() > 1) {
471
					logger.warn("This TaxonDescription contains more than one NamedArea: " + taxonDescription.getTitleCache());
472
				}
473
			}
474
		} catch (ClassCastException e) {
475
			// TODO Auto-generated catch block
476
			e.printStackTrace();
477
		} catch (UndefinedTransformerMethodException e) {
478
			// TODO Auto-generated catch block
479
			e.printStackTrace();
480
		}
481
		return result;
482
	}
483

  
484
	/**
485
	 * Returns the <code>TaxonFk</code> attribute.
486
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
487
	 * @param state The {@link PesiExportState PesiExportState}.
488
	 * @return The <code>TaxonFk</code> attribute.
489
	 * @see MethodMapper
490
	 */
491
	@SuppressWarnings("unused")
492
	private static Integer getTaxonFk(DescriptionElementBase descriptionElement, DbExportStateBase<?, PesiTransformer> state) {
493
		Integer result = null;
494
		DescriptionBase<?> inDescription = descriptionElement.getInDescription();
495
		if (inDescription != null && inDescription.isInstanceOf(TaxonDescription.class)) {
496
			TaxonDescription taxonDescription = CdmBase.deproxy(inDescription, TaxonDescription.class);
497
			Taxon taxon = taxonDescription.getTaxon();
498
			result = state.getDbId(taxon.getName());
499
		}
500
		return result;
501
	}
502
	
503
	/**
504
	 * Returns the TaxonFk for a given TaxonName.
505
	 * @param taxonName The {@link TaxonNameBase TaxonName}.
506
	 * @param state The {@link DbExportStateBase DbExportState}.
507
	 * @return
508
	 */
509
	private static Integer getTaxonFk(TaxonNameBase<?,?> taxonName, DbExportStateBase<?, PesiTransformer> state) {
510
		return state.getDbId(taxonName);
511
	}
512
	
513
	/**
514
	 * Returns the <code>LastAction</code> attribute.
515
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
516
	 * @return The <code>LastAction</code> attribute.
517
	 * @see MethodMapper
518
	 */
519
	@SuppressWarnings("unused")
520
	private static String getLastAction(DescriptionElementBase descriptionElement) {
521
		// TODO
522
		return null;
523
	}
524

  
525
	/**
526
	 * Returns the <code>LastActionDate</code> attribute.
527
	 * @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
528
	 * @return The <code>LastActionDate</code> attribute.
529
	 * @see MethodMapper
530
	 */
531
	@SuppressWarnings("unused")
532
	private static DateTime getLastActionDate(DescriptionElementBase descriptionElement) {
533
		DateTime result = null;
534
		return result;
535
	}
536

  
537
	/**
538
	 * Returns the CDM to PESI specific export mappings.
539
	 * @return The {@link PesiExportMapping PesiExportMapping}.
540
	 */
541
	private PesiExportMapping getMapping() {
542
		PesiExportMapping mapping = new PesiExportMapping(dbTableName);
543
		
544
		mapping.addMapper(IdMapper.NewInstance("NoteId"));
545
		mapping.addMapper(MethodMapper.NewInstance("Note_1", this));
546
		mapping.addMapper(MethodMapper.NewInstance("Note_2", this));
547
		mapping.addMapper(MethodMapper.NewInstance("NoteCategoryFk", this));
548
		mapping.addMapper(MethodMapper.NewInstance("NoteCategoryCache", this));
549
		mapping.addMapper(MethodMapper.NewInstance("LanguageFk", this));
550
		mapping.addMapper(MethodMapper.NewInstance("LanguageCache", this));
551
		mapping.addMapper(MethodMapper.NewInstance("Region", this));
552
		mapping.addMapper(MethodMapper.NewInstance("TaxonFk", this.getClass(), "getTaxonFk", standardMethodParameter, DbExportStateBase.class));
553
		mapping.addMapper(MethodMapper.NewInstance("LastAction", this));
554
		mapping.addMapper(MethodMapper.NewInstance("LastActionDate", this));
555

  
556
		return mapping;
557
	}
558

  
559
}
cdm-pesi/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiNoteSourceExport.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.common.DbExportStateBase;
20
import eu.etaxonomy.cdm.io.common.Source;
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff