Project

General

Profile

Download (12.3 KB) Statistics
| Branch: | Tag: | 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;
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.berlinModel.out.mapper.DbStringMapper;
21
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.DbTimePeriodMapper;
22
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.IdMapper;
23
import eu.etaxonomy.cdm.io.berlinModel.out.mapper.MethodMapper;
24
import eu.etaxonomy.cdm.io.common.Source;
25
import eu.etaxonomy.cdm.io.common.IImportConfigurator.DO_REFERENCES;
26
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
29
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
30

    
31
/**
32
 * @author a.mueller
33
 * @author e.-m.lee
34
 * @date 11.02.2010
35
 *
36
 */
37
@Component
38
@SuppressWarnings("unchecked")
39
public class PesiSourceExport extends PesiExportBase {
40
	private static final Logger logger = Logger.getLogger(PesiSourceExport.class);
41
	private static final Class<? extends CdmBase> standardMethodParameter = ReferenceBase.class;
42

    
43
	private static int modCount = 1000;
44
	private static final String dbTableName = "Source";
45
	private static final String pluralString = "Sources";
46

    
47
	public PesiSourceExport() {
48
		super();
49
	}
50

    
51
	/* (non-Javadoc)
52
	 * @see eu.etaxonomy.cdm.io.pesi.out.PesiExportBase#getStandardMethodParameter()
53
	 */
54
	@Override
55
	public Class<? extends CdmBase> getStandardMethodParameter() {
56
		return standardMethodParameter;
57
	}
58

    
59
	/* (non-Javadoc)
60
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
61
	 */
62
	@Override
63
	protected boolean doCheck(PesiExportState state) {
64
		boolean result = true;
65
		return result;
66
	}
67

    
68
	/* (non-Javadoc)
69
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
70
	 */
71
	@Override
72
	protected boolean doInvoke(PesiExportState state) {
73
		try{
74
			logger.error("*** Started Making " + pluralString + " ...");
75

    
76
			// Get the limit for objects to save within a single transaction.
77
			int limit = state.getConfig().getLimitSave();
78

    
79
			// Stores whether this invoke was successful or not.
80
			boolean success = true ;
81

    
82
			// PESI: Clear the database table Source.
83
			doDelete(state);
84

    
85
			// Create the database table that hosts state information
86
			state.createStateTables();
87

    
88
			// CDM: Get the number of all available references.
89
//			int maxCount = getReferenceService().count(null);
90
//			logger.error("Total amount of " + maxCount + " " + pluralString + " will be exported.");
91

    
92
			// Get specific mappings: (CDM) Reference -> (PESI) Source
93
			PesiExportMapping mapping = getMapping();
94

    
95
			// Initialize the db mapper
96
			mapping.initialize(state);
97

    
98
			// PESI: Create the Sources
99
			// TODO: Store CDM2PESI identifier pairs for later use in other export classes - PesiExportState
100
			int count = 0;
101
			int pastCount = 0;
102
			TransactionStatus txStatus = null;
103
			List<ReferenceBase> list = null;
104

    
105
			// Start transaction
106
			txStatus = startTransaction(true);
107
			logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
108
			while ((list = getReferenceService().list(null, limit, count, null, null)).size() > 0) {
109

    
110
				logger.error("Fetched " + list.size() + " " + pluralString + ". Exporting...");
111
				for (ReferenceBase<?> reference : list) {
112
					doCount(count++, modCount, pluralString);
113
					success &= mapping.invoke(reference);
114
				}
115

    
116
				// Commit transaction
117
				commitTransaction(txStatus);
118
				logger.error("Committed transaction.");
119
				logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
120
				pastCount = count;
121

    
122
				// Start transaction
123
				txStatus = startTransaction(true);
124
				logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + limit + ") ...");
125
			}
126
			if (list.size() == 0) {
127
				logger.error("No " + pluralString + " left to fetch.");
128
			}
129
			// Commit transaction
130
			commitTransaction(txStatus);
131
			logger.error("Committed transaction.");
132

    
133
			logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
134

    
135
			return success;
136
		} catch (SQLException e) {
137
			e.printStackTrace();
138
			logger.error(e.getMessage());
139
			return false;
140
		}
141
	}
142

    
143
	/**
144
	 * Deletes all entries of database tables related to <code>Source</code>.
145
	 * @param state The {@link PesiExportState PesiExportState}.
146
	 * @return Whether the delete operation was successful or not.
147
	 */
148
	protected boolean doDelete(PesiExportState state) {
149
		PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
150
		
151
		String sql;
152
		Source destination =  pesiConfig.getDestination();
153

    
154
		// Clear Occurrences
155
		sql = "DELETE FROM Occurrence";
156
		destination.setQuery(sql);
157
		destination.update(sql);
158

    
159
		// Clear Taxa
160
		sql = "DELETE FROM Taxon";
161
		destination.setQuery(sql);
162
		destination.update(sql);
163

    
164
		// Clear Sources
165
		sql = "DELETE FROM " + dbTableName;
166
		destination.setQuery(sql);
167
		destination.update(sql);
168
		
169
		return true;
170
	}
171
	
172
	/**
173
	 * Returns the <code>IMIS_Id</code> attribute.
174
	 * @param reference The {@link ReferenceBase Reference}.
175
	 * @return The <code>IMIS_Id</code> attribute.
176
	 * @see MethodMapper
177
	 */
178
	@SuppressWarnings("unused")
179
	private static Integer getIMIS_Id(ReferenceBase<?> reference) {
180
		// TODO
181
		// Where is the IMIS_Id from an ERMS import stored in CDM?
182
		return null;
183
	}
184
	
185
	/**
186
	 * Returns the <code>SourceCategoryFK</code> attribute.
187
	 * @param reference The {@link ReferenceBase Reference}.
188
	 * @return The <code>SourceCategoryFK</code> attribute.
189
	 * @see MethodMapper
190
	 */
191
	@SuppressWarnings("unused")
192
	private static Integer getSourceCategoryFK(ReferenceBase<?> reference) {
193
		return PesiTransformer.reference2SourceCategoryFK(reference);
194
	}
195
	
196
	/**
197
	 * Returns the <code>SourceCategoryCache</code> attribute.
198
	 * @param reference The {@link ReferenceBase Reference}.
199
	 * @return The <code>SourceCategoryCache</code> attribute.
200
	 * @see MethodMapper
201
	 */
202
	@SuppressWarnings("unused")
203
	private static String getSourceCategoryCache(ReferenceBase<?> reference) {
204
		return PesiTransformer.getSourceCategoryCache(reference);
205
	}
206

    
207
	/**
208
	 * Returns the <code>Name</code> attribute. The corresponding CDM attribute is <code>title</code>.
209
	 * @param reference The {@link ReferenceBase Reference}.
210
	 * @return The <code>Name</code> attribute.
211
	 * @see MethodMapper
212
	 */
213
	@SuppressWarnings("unused")
214
	private static String getName(ReferenceBase<?> reference) {
215
		if (reference != null) {
216
			return reference.getTitle();
217
		} else {
218
			return null;
219
		}
220
	}
221

    
222
	/**
223
	 * Returns the <code>AuthorString</code> attribute. The corresponding CDM attribute is the <code>titleCache</code> of an <code>authorTeam</code>.
224
	 * @param reference The {@link ReferenceBase Reference}.
225
	 * @return The <code>AuthorString</code> attribute.
226
	 * @see MethodMapper
227
	 */
228
	@SuppressWarnings("unused")
229
	private static String getAuthorString(ReferenceBase<?> reference) {
230
		String result = null;
231

    
232
		if (reference != null) {
233
			TeamOrPersonBase team = reference.getAuthorTeam();
234
			if (team != null) {
235
	//			result = team.getTitleCache();
236
				result = team.getNomenclaturalTitle();
237
			} else {
238
				result = null;
239
			}
240
		}
241
		
242
		return result;
243
	}
244

    
245
	/**
246
	 * Returns the <code>NomRefCache</code> attribute. The corresponding CDM attribute is <code>titleCache</code>.
247
	 * @param reference The {@link ReferenceBase Reference}.
248
	 * @return The <code>NomRefCache</code> attribute.
249
	 * @see MethodMapper
250
	 */
251
	@SuppressWarnings("unused")
252
	private static String getNomRefCache(ReferenceBase<?> reference) {
253
		if (reference != null) {
254
			return reference.getTitleCache();
255
		} else {
256
			return null;
257
		}
258
	}
259

    
260
	/**
261
	 * Returns the <code>Notes</code> attribute.
262
	 * @param reference The {@link ReferenceBase Reference}.
263
	 * @return The <code>Notes</code> attribute.
264
	 * @see MethodMapper
265
	 */
266
	@SuppressWarnings("unused")
267
	private static String getNotes(ReferenceBase<?> reference) {
268
		// TODO
269
		return null;
270
	}
271

    
272
	/**
273
	 * Returns the <code>RefIdInSource</code> attribute.
274
	 * @param reference The {@link ReferenceBase Reference}.
275
	 * @return The <code>RefIdInSource</code> attribute.
276
	 * @see MethodMapper
277
	 */
278
	@SuppressWarnings("unused")
279
	private static String getRefIdInSource(ReferenceBase<?> reference) {
280
		String result = null;
281

    
282
		if (reference != null) {
283
			Set<IdentifiableSource> sources = reference.getSources();
284
			if (sources.size() == 1) {
285
				result = sources.iterator().next().getIdInSource();
286
			} else if (sources.size() > 1) {
287
				logger.warn("Reference has multiple IdentifiableSources: " + reference.getUuid() + " (" + reference.getTitleCache() + ")");
288
				int count = 1;
289
				for (IdentifiableSource source : sources) {
290
					result += source.getIdInSource();
291
					if (count < sources.size()) {
292
						result += "; ";
293
					}
294
					count++;
295
				}
296
			}
297
		}
298

    
299
		return result;
300
	}
301

    
302
	/**
303
	 * Returns the <code>OriginalDB</code> attribute. The corresponding CDM attribute is the <code>titleCache</code> of a <code>citation</code>.
304
	 * @param reference The {@link ReferenceBase Reference}.
305
	 * @return The <code>OriginalDB</code> attribute.
306
	 * @see MethodMapper
307
	 */
308
	@SuppressWarnings("unused")
309
	private static String getOriginalDB(ReferenceBase<?> reference) {
310
		String result = "";
311

    
312
		if (reference != null) {
313
			Set<IdentifiableSource> sources = reference.getSources();
314
			if (sources.size() == 1) {
315
				ReferenceBase citation = sources.iterator().next().getCitation();
316
				if (citation != null) {
317
					result = PesiTransformer.databaseString2Abbreviation(citation.getTitleCache()); //or just title
318
				} else {
319
					logger.warn("OriginalDB can not be determined because the citation of this source is NULL: " + sources.iterator().next().getUuid());
320
				}
321
			} else if (sources.size() > 1) {
322
				logger.warn("Taxon has multiple IdentifiableSources: " + reference.getUuid() + " (" + reference.getTitleCache() + ")");
323
				int count = 1;
324
				for (IdentifiableSource source : sources) {
325
					ReferenceBase citation = source.getCitation();
326
					if (citation != null) {
327
						result += PesiTransformer.databaseString2Abbreviation(citation.getTitleCache());
328
						if (count < sources.size()) {
329
							result += "; ";
330
						}
331
						count++;
332
					}
333
				}
334
			} else {
335
				result = null;
336
			}
337
		}
338

    
339
		return result;
340
	}
341

    
342
	/* (non-Javadoc)
343
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
344
	 */
345
	@Override
346
	protected boolean isIgnore(PesiExportState state) {
347
		return ! (((PesiExportConfigurator) state.getConfig()).getDoReferences().equals(DO_REFERENCES.ALL));
348
	}
349

    
350
	/**
351
	 * Returns the CDM to PESI specific export mappings.
352
	 * @return The {@link PesiExportMapping PesiExportMapping}.
353
	 */
354
	private PesiExportMapping getMapping() {
355
		PesiExportMapping mapping = new PesiExportMapping(dbTableName);
356
		
357
		mapping.addMapper(IdMapper.NewInstance("SourceId"));
358
		mapping.addMapper(MethodMapper.NewInstance("IMIS_Id", this));
359
		mapping.addMapper(MethodMapper.NewInstance("SourceCategoryFK", this));
360
		mapping.addMapper(MethodMapper.NewInstance("SourceCategoryCache", this));
361
		mapping.addMapper(MethodMapper.NewInstance("Name", this));
362
		mapping.addMapper(DbStringMapper.NewInstance("referenceAbstract", "Abstract"));
363
		mapping.addMapper(DbStringMapper.NewInstance("title", "Title"));
364
		mapping.addMapper(MethodMapper.NewInstance("AuthorString", this));
365
		mapping.addMapper(DbTimePeriodMapper.NewInstance("datePublished", "RefYear"));
366
		mapping.addMapper(MethodMapper.NewInstance("NomRefCache", this));
367
		mapping.addMapper(DbStringMapper.NewInstance("uri", "Link"));
368
		mapping.addMapper(MethodMapper.NewInstance("Notes", this));
369
		mapping.addMapper(MethodMapper.NewInstance("RefIdInSource", this));
370
		mapping.addMapper(MethodMapper.NewInstance("OriginalDB", this));
371

    
372
		return mapping;
373
	}
374

    
375
}
(12-12/14)