Project

General

Profile

Download (14.4 KB) Statistics
| Branch: | Revision:
1
/**
2
* Copyright (C) 2009 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.io.pesi.out;
10

    
11
import java.sql.SQLException;
12
import java.util.ArrayList;
13
import java.util.HashSet;
14
import java.util.List;
15
import java.util.Set;
16

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

    
21
import eu.etaxonomy.cdm.io.common.IExportConfigurator.DO_REFERENCES;
22
import eu.etaxonomy.cdm.io.common.Source;
23
import eu.etaxonomy.cdm.io.common.mapping.out.DbExtensionMapper;
24
import eu.etaxonomy.cdm.io.common.mapping.out.DbStringMapper;
25
import eu.etaxonomy.cdm.io.common.mapping.out.DbTimePeriodMapper;
26
import eu.etaxonomy.cdm.io.common.mapping.out.DbUriMapper;
27
import eu.etaxonomy.cdm.io.common.mapping.out.IdMapper;
28
import eu.etaxonomy.cdm.io.common.mapping.out.MethodMapper;
29
import eu.etaxonomy.cdm.io.pesi.erms.ErmsTransformer;
30
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
31
import eu.etaxonomy.cdm.model.common.CdmBase;
32
import eu.etaxonomy.cdm.model.common.ExtensionType;
33
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
34
import eu.etaxonomy.cdm.model.reference.Reference;
35
import eu.etaxonomy.cdm.model.reference.ReferenceType;
36

    
37
/**
38
 * The export class for {@link eu.etaxonomy.cdm.model.reference.Reference References}.<p>
39
 * Inserts into DataWarehouse database table <code>Source</code>.
40
 * @author e.-m.lee
41
 * @date 11.02.2010
42
 *
43
 */
44
@Component
45
public class PesiSourceExport extends PesiExportBase {
46
	private static final Logger logger = Logger.getLogger(PesiSourceExport.class);
47
	private static final Class<? extends CdmBase> standardMethodParameter = Reference.class;
48

    
49
	private static int modCount = 1000;
50
	public static final String dbTableName = "Source";
51
	private static final String pluralString = "Sources";
52
	List<Integer> storedSourceIds = new ArrayList<Integer>();
53

    
54
	public PesiSourceExport() {
55
		super();
56
	}
57

    
58
	/* (non-Javadoc)
59
	 * @see eu.etaxonomy.cdm.io.pesi.out.PesiExportBase#getStandardMethodParameter()
60
	 */
61
	@Override
62
	public Class<? extends CdmBase> getStandardMethodParameter() {
63
		return standardMethodParameter;
64
	}
65

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

    
75
	/**
76
	 * Checks whether a sourceId was stored already.
77
	 * @param sourceId
78
	 * @return
79
	 */
80
	protected boolean isStoredSourceId(Integer sourceId) {
81
		if (storedSourceIds.contains(sourceId)) {
82
			return true;
83
		} else {
84
			return false;
85
		}
86
	}
87

    
88
	/**
89
	 * Adds a sourceId to the list of storedSourceIds.
90
	 * @param sourceId
91
	 */
92
	protected void addToStoredSourceIds(Integer sourceId) {
93
		if (! storedSourceIds.contains(sourceId)) {
94
			this.storedSourceIds.add(sourceId);
95
		}
96
	}
97

    
98
	/* (non-Javadoc)
99
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
100
	 */
101
	@Override
102
	protected void doInvoke(PesiExportState state) {
103
		try{
104
			logger.info("*** Started Making " + pluralString + " ...");
105

    
106
			PesiExportConfigurator pesiExportConfigurator = state.getConfig();
107

    
108
			// Get the limit for objects to save within a single transaction.
109
			int limit = pesiExportConfigurator.getLimitSave();
110

    
111
			// Stores whether this invoke was successful or not.
112
			boolean success = true ;
113

    
114
			// PESI: Clear the database table Source.
115
			//doDelete(state);  -> done by stored procedure
116

    
117
			// Get specific mappings: (CDM) Reference -> (PESI) Source
118
			PesiExportMapping mapping = getMapping();
119

    
120
			// Initialize the db mapper
121
			mapping.initialize(state);
122

    
123
			// Create the Sources
124
			int count = 0;
125
			int pastCount = 0;
126
			TransactionStatus txStatus = null;
127
			List<Reference> list = null;
128

    
129
//			logger.error("PHASE 1...");
130
			// Start transaction
131
			txStatus = startTransaction(true);
132
			logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
133
			while ((list = getReferenceService().list(null, limit, count, null, null)).size() > 0) {
134

    
135
				logger.debug("Fetched " + list.size() + " " + pluralString + ". Exporting...");
136
				for (Reference reference : list) {
137
					doCount(count++, modCount, pluralString);
138
					success &= mapping.invoke(reference);
139
				}
140

    
141
				// Commit transaction
142
				commitTransaction(txStatus);
143
				logger.debug("Committed transaction.");
144
				logger.info("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
145
				pastCount = count;
146

    
147
				// Start transaction
148
				txStatus = startTransaction(true);
149
				logger.info("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
150
			}
151
			if (list.size() == 0) {
152
				logger.info("No " + pluralString + " left to fetch.");
153
			}
154
			// Commit transaction
155
			commitTransaction(txStatus);
156
			logger.info("Committed transaction.");
157

    
158
			logger.info("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
159

    
160
			if (!success){
161
				state.setUnsuccessfull();
162
			}
163
			return;
164
		} catch (SQLException e) {
165
			e.printStackTrace();
166
			logger.error(e.getMessage());
167
			state.setUnsuccessfull();
168
			return;
169
		}
170
	}
171

    
172
	/**
173
	 * Deletes all entries of database tables related to <code>Source</code>.
174
	 * @param state The {@link PesiExportState PesiExportState}.
175
	 * @return Whether the delete operation was successful or not.
176
	 */
177
	protected boolean doDelete(PesiExportState state) {
178
		PesiExportConfigurator pesiConfig = state.getConfig();
179

    
180
		String sql;
181
		Source destination =  pesiConfig.getDestination();
182

    
183
		// Clear Occurrences
184
		sql = "DELETE FROM Occurrence";
185
		destination.setQuery(sql);
186
		destination.update(sql);
187

    
188
		// Clear Taxa
189
		sql = "DELETE FROM Taxon";
190
		destination.setQuery(sql);
191
		destination.update(sql);
192

    
193
		// Clear Sources
194
		sql = "DELETE FROM " + dbTableName;
195
		destination.setQuery(sql);
196
		destination.update(sql);
197

    
198
		return true;
199
	}
200

    
201
	/**
202
	 * Returns the <code>IMIS_Id</code> attribute.
203
	 * @param reference The {@link Reference Reference}.
204
	 * @return The <code>IMIS_Id</code> attribute.
205
	 * @see MethodMapper
206
	 */
207
	@SuppressWarnings("unused")
208
	private static Integer getIMIS_Id(Reference reference) {
209
		return null;
210
	}
211

    
212
	/**
213
	 * Returns the <code>SourceCategoryFK</code> attribute.
214
	 * @param reference The {@link Reference Reference}.
215
	 * @return The <code>SourceCategoryFK</code> attribute.
216
	 * @see MethodMapper
217
	 */
218
	@SuppressWarnings("unused")
219
	private static Integer getSourceCategoryFK(Reference reference) {
220
		Integer result = null;
221
		try {
222
		result = PesiTransformer.reference2SourceCategoryFK(reference);
223
		} catch (Exception e) {
224
			e.printStackTrace();
225
		}
226
		return result;
227
	}
228

    
229
	/**
230
	 * Returns the <code>SourceCategoryCache</code> attribute.
231
	 * @param reference The {@link Reference Reference}.
232
	 * @return The <code>SourceCategoryCache</code> attribute.
233
	 * @see MethodMapper
234
	 */
235
	@SuppressWarnings("unused")
236
	private static String getSourceCategoryCache(Reference reference, PesiExportState state) {
237
		return state.getTransformer().getCacheByReference(reference);
238
	}
239

    
240
	/**
241
	 * Returns the <code>Name</code> attribute. The corresponding CDM attribute is <code>title</code>.
242
	 * @param reference The {@link Reference Reference}.
243
	 * @return The <code>Name</code> attribute.
244
	 * @see MethodMapper
245
	 */
246
	@SuppressWarnings("unused")
247
	private static String getName(Reference reference) {
248
		if (reference != null) {
249
			return reference.getTitleCache(); // was getTitle()
250
		} else {
251
			return null;
252
		}
253
	}
254

    
255
	/**
256
	 * Returns the <code>AuthorString</code> attribute. The corresponding CDM attribute is the <code>titleCache</code> of an <code>authorTeam</code>.
257
	 * @param reference The {@link Reference Reference}.
258
	 * @return The <code>AuthorString</code> attribute.
259
	 * @see MethodMapper
260
	 */
261
	@SuppressWarnings("unused")
262
	private static String getAuthorString(Reference reference) {
263
		String result = null;
264

    
265
		try {
266
		if (reference != null) {
267
			TeamOrPersonBase team = reference.getAuthorship();
268
			if (team != null) {
269
				result = team.getTitleCache();
270
//				result = team.getNomenclaturalTitle();
271
			} else {
272
				result = null;
273
			}
274
		}
275
		} catch (Exception e) {
276
			e.printStackTrace();
277
		}
278

    
279
		return result;
280
	}
281

    
282
	/**
283
	 * Returns the <code>NomRefCache</code> attribute. The corresponding CDM attribute is <code>titleCache</code>.
284
	 * @param reference The {@link Reference Reference}.
285
	 * @return The <code>NomRefCache</code> attribute.
286
	 * @see MethodMapper
287
	 */
288
	@SuppressWarnings("unused")
289
	private static String getNomRefCache(Reference reference) {
290
		return null;
291
//		if (reference != null) {
292
//			return reference.getTitleCache();
293
//		} else {
294
//			return null;
295
//		}
296
	}
297

    
298
	/**
299
	 * Returns the <code>Notes</code> attribute.
300
	 * @param reference The {@link Reference Reference}.
301
	 * @return The <code>Notes</code> attribute.
302
	 * @see MethodMapper
303
	 */
304
	@SuppressWarnings("unused")
305
	private static String getNotes(Reference reference) {
306
		// TODO
307
		return null;
308
	}
309

    
310
	/**
311
	 * Returns the <code>RefIdInSource</code> attribute.
312
	 * @param reference The {@link Reference Reference}.
313
	 * @return The <code>RefIdInSource</code> attribute.
314
	 * @see MethodMapper
315
	 */
316
	@SuppressWarnings("unused")
317
	private static String getRefIdInSource(Reference reference) {
318
		String result = null;
319

    
320
		try {
321
		if (reference != null) {
322
			Set<IdentifiableSource> sourceAll = reference.getSources();
323
			Set<IdentifiableSource> sourceCandidates = filterOriginalPesiDbSources(sourceAll);
324

    
325
			if (sourceCandidates.size() == 1) {
326
				result = sourceCandidates.iterator().next().getIdInSource();
327
			} else if (sourceCandidates.size() > 1) {
328
				logger.warn("Reference for RefIdInSource has multiple IdentifiableSources which are candidates for a PESI originalDbSource. RefIdInSource can't be determined correctly and will be left out: " + reference.getUuid() + " (" + reference.getTitleCache() + ")");
329
				int count = 1;
330
//				for (IdentifiableSource source : sources) {
331
//					result += source.getIdInSource();
332
//					if (count < sources.size()) {
333
//						result += "; ";
334
//					}
335
//					count++;
336
//				}
337
			}
338
		}
339
		} catch (Exception e) {
340
			e.printStackTrace();
341
		}
342

    
343
		return result;
344
	}
345

    
346
	private static Set<IdentifiableSource> filterOriginalPesiDbSources(
347
			Set<IdentifiableSource> sourceAll) {
348
		Set<IdentifiableSource> sourceCandidates = new HashSet<IdentifiableSource>();
349
		for (IdentifiableSource source : sourceAll){
350
			if (isOriginalPesiDbSource(source)){
351
				sourceCandidates.add(source);
352
			}
353
		}
354
		return sourceCandidates;
355
	}
356

    
357
	private static boolean isOriginalPesiDbSource(IdentifiableSource source) {
358
		return (source.getCitation() != null) &&
359
				source.getCitation().getType().equals(ReferenceType.Database);
360
	}
361

    
362
	/**
363
	 * Returns the <code>OriginalDB</code> attribute. The corresponding CDM attribute is the <code>titleCache</code> of a <code>citation</code>.
364
	 * @param reference The {@link Reference Reference}.
365
	 * @return The <code>OriginalDB</code> attribute.
366
	 * @see MethodMapper
367
	 */
368
	@SuppressWarnings("unused")
369
	private static String getOriginalDB(Reference reference) {
370
		String result = "";
371

    
372
		try {
373
		if (reference != null) {
374
			Set<IdentifiableSource> sourcesAll = reference.getSources();
375
			Set<IdentifiableSource> sourceCandidates = filterOriginalPesiDbSources(sourcesAll);
376

    
377
			if (sourceCandidates.size() == 1) {
378
				Reference citation = sourceCandidates.iterator().next().getCitation();
379
				if (citation != null) {
380
					result = PesiTransformer.databaseString2Abbreviation(citation.getTitleCache()); //or just title
381
				} else {
382
					logger.warn("OriginalDB can not be determined because the citation of this source is NULL: " + sourceCandidates.iterator().next().getUuid());
383
				}
384
			} else if (sourceCandidates.size() > 1) {
385
				logger.warn("Taxon has multiple IdentifiableSources: " + reference.getUuid() + " (" + reference.getTitleCache() + ")");
386
				int count = 1;
387
				for (IdentifiableSource source : sourceCandidates) {
388
					Reference citation = source.getCitation();
389
					if (citation != null) {
390
						result += PesiTransformer.databaseString2Abbreviation(citation.getTitleCache());
391
						if (count < sourceCandidates.size()) {
392
							result += "; ";
393
						}
394
						count++;
395
					}
396
				}
397
			} else {
398
				result = null;
399
			}
400
		}
401
		} catch (Exception e) {
402
			e.printStackTrace();
403
		}
404

    
405
		return result;
406
	}
407

    
408
	/* (non-Javadoc)
409
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
410
	 */
411
	@Override
412
	protected boolean isIgnore(PesiExportState state) {
413
		return ! state.getConfig().getDoReferences().equals(DO_REFERENCES.ALL);
414
	}
415

    
416
	/**
417
	 * Returns the CDM to PESI specific export mappings.
418
	 * @return The {@link PesiExportMapping PesiExportMapping}.
419
	 */
420
	private PesiExportMapping getMapping() {
421
		PesiExportMapping mapping = new PesiExportMapping(dbTableName);
422
		ExtensionType extensionType = null;
423

    
424
		mapping.addMapper(IdMapper.NewInstance("SourceId"));
425

    
426
		// IMIS_Id
427
		extensionType = (ExtensionType)getTermService().find(ErmsTransformer.IMIS_UUID);
428
		if (extensionType != null) {
429
			mapping.addMapper(DbExtensionMapper.NewInstance(extensionType, "IMIS_Id"));
430
		} else {
431
			mapping.addMapper(MethodMapper.NewInstance("IMIS_Id", this));
432
		}
433

    
434
		mapping.addMapper(MethodMapper.NewInstance("SourceCategoryFK", this));
435
		mapping.addMapper(MethodMapper.NewInstance("SourceCategoryCache", this, Reference.class, PesiExportState.class));
436
		mapping.addMapper(MethodMapper.NewInstance("Name", this));
437
		mapping.addMapper(DbStringMapper.NewInstance("referenceAbstract", "Abstract"));
438
		mapping.addMapper(DbStringMapper.NewInstance("title", "Title"));
439
		mapping.addMapper(MethodMapper.NewInstance("AuthorString", this));
440
		mapping.addMapper(DbTimePeriodMapper.NewInstance("datePublished", "RefYear"));
441
		mapping.addMapper(MethodMapper.NewInstance("NomRefCache", this));
442
		mapping.addMapper(DbUriMapper.NewInstance("uri", "Link"));
443
		mapping.addMapper(MethodMapper.NewInstance("Notes", this));
444
		mapping.addMapper(MethodMapper.NewInstance("RefIdInSource", this));
445
		mapping.addMapper(MethodMapper.NewInstance("OriginalDB", this));
446

    
447
		return mapping;
448
	}
449

    
450
}
(10-10/12)