Project

General

Profile

« Previous | Next » 

Revision 892efc69

Added by Andreas Kohlbecker almost 14 years ago

merging /branches/cdmlib/SPRINT-Chichorieae1/ to trunk

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/pesi/out/PesiOccurrenceExport.java
11 11

  
12 12
import java.sql.Connection;
13 13
import java.sql.PreparedStatement;
14
import java.sql.ResultSet;
14 15
import java.sql.SQLException;
15 16
import java.util.HashMap;
16 17
import java.util.List;
......
31 32
import eu.etaxonomy.cdm.model.description.Distribution;
32 33
import eu.etaxonomy.cdm.model.description.TaxonDescription;
33 34
import eu.etaxonomy.cdm.model.location.NamedArea;
34
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
35 35
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
36 36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37 37
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
......
120 120
					if (taxonBase.isInstanceOf(Taxon.class)) {
121 121

  
122 122
						// Set the current Taxon
123
						setTaxon(CdmBase.deproxy(taxonBase, Taxon.class));
123
						taxon = CdmBase.deproxy(taxonBase, Taxon.class);
124 124

  
125 125
						// Determine the TaxonDescriptions
126
						Set<TaxonDescription> taxonDescriptions = getTaxon().getDescriptions();
126
						Set<TaxonDescription> taxonDescriptions = taxon.getDescriptions();
127 127

  
128 128
						// Determine the DescriptionElements (Citations) for the current Taxon
129 129
						for (TaxonDescription taxonDescription : taxonDescriptions) {
130 130
							Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
131 131
							for (DescriptionElementBase descriptionElement : descriptionElements) {
132
								Set<DescriptionElementSource> elementSources = descriptionElement.getSources();
132 133
								
133 134
								if (descriptionElement.isInstanceOf(Distribution.class)) {
134
//									logger.error("Distribution instance found.");
135 135
									Distribution distribution = CdmBase.deproxy(descriptionElement, Distribution.class);
136 136
									setNamedArea(distribution.getArea());
137 137
									setDistribution(distribution);
138
									Set<DescriptionElementSource> elementSources = distribution.getSources();
139
									
138

  
140 139
									// Differentiate between descriptionElements with and without sources.
141
									if (! hasCitations(elementSources)) {
142
//										logger.error("Distribution has no sources. Exporting ...");
140
									if (elementSources.size() == 0 && state.getDbId(descriptionElement) != null) {
143 141
										doCount(count++, modCount, pluralString);
144
										success &= mapping.invoke(distribution);
142
										success &= mapping.invoke(descriptionElement);
145 143
									} else {
146
//										logger.error("Distribution has " + elementSources.size() + " sources. Exporting ...");
147 144
										for (DescriptionElementSource elementSource : elementSources) {
148 145
											ReferenceBase reference = elementSource.getCitation();
149 146
	
150 147
											// Citations can be empty (null): Is it wrong data or just a normal case?
151
											if (reference != null) {
152
//												logger.error("Exporting Reference " + reference.getTitleCache() + " ...");
148
											if (reference != null && state.getDbId(reference) != null) {
153 149
												doCount(count++, modCount, pluralString);
154 150
												success &= mapping.invoke(reference);
155
											} else {
156
												// This should never be the case. Testing purpose only.
157
//												logger.error("Citation of ElementSource is NULL. Nothing to export for ElementSource: " + elementSource.getUuid());
158 151
											}
159 152
										}
160 153
										
......
176 169

  
177 170
				// Start transaction
178 171
				txStatus = startTransaction(true);
179
				logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
172
				logger.error("Started new transaction. Fetching some " + pluralString + " first (max: " + limit + ") ...");
180 173
			}
181 174
			if (list.size() == 0) {
182 175
				logger.error("No " + pluralString + " left to fetch.");
......
196 189
	}
197 190

  
198 191
	/**
199
	 * Returns whether the given Set of DescriptionElementSources have a citation or not.
200
	 * @param elementSources
192
	 * Returns the identifier of the last occurrence committed to the database.
193
	 * @param state
201 194
	 * @return
202 195
	 */
203
	public static boolean hasCitations(Set<DescriptionElementSource> elementSources) {
204
		boolean notFound = true;
205
		if (elementSources.size() == 0) {
206
			return false;
207
		} else {
208
			for (DescriptionElementSource elementSource : elementSources) {
209
				notFound &= elementSource.getCitation() == null;
196
	private static Integer getLastOccurrenceId(PesiExportState state) {
197
		// Retrieve the identifier of the last stored record
198
		// Retrieve database identifier of the last created occurrence record.
199
		String lastRecordSql = "Select @@Identity From OccurrenceSource";
200
		Connection con = state.getConfig().getDestination().getConnection();
201
		PreparedStatement stmt = null;
202
		
203
		Integer occurrenceId = 0;
204
		try {
205
			stmt = con.prepareStatement(lastRecordSql);
206
	//		stmt.setString(1, dbTableName);
207
			ResultSet resultSet = stmt.executeQuery();
208
			while (resultSet.next()) {
209
				// Count of this resultset should be 1
210
				occurrenceId = resultSet.getInt(1);
210 211
			}
212
			if (occurrenceId == 0) {
213
				throw new RuntimeException();
214
			}
215
		} catch (SQLException e) {
216
			logger.error("SQLException during getOccurrenceId invoke.");
217
			e.printStackTrace();
211 218
		}
212
		if (notFound) {
213
			return false;
214
		} else {
215
			return true;
216
		}
217
	}
218 219

  
219
	/**
220
	 * @return the taxon
221
	 */
222
	public static Taxon getTaxon() {
223
		return taxon;
220
		return occurrenceId;
224 221
	}
225 222

  
226
	/**
227
	 * @param taxon the taxon to set
228
	 */
229
	public static void setTaxon(Taxon taxon) {
230
		PesiOccurrenceExport.taxon = taxon;
231
	}
232 223

  
233 224
	/**
234 225
	 * Creates the entries for the database table 'OccurrenceSource'.
......
255 246
				stmt.executeUpdate();
256 247
				return true;
257 248
			} catch (SQLException e) {
258
				logger.error("SQLException during update into " + dbTableName + ". Entity: " + entity.getUuid());
249
				logger.error("SQLException during getOccurrenceId invoke...");
259 250
				e.printStackTrace();
260 251
				return false;
261 252
			}
......
290 281

  
291 282
	/**
292 283
	 * Returns the <code>TaxonFk</code> attribute.
293
	 * @param entity
294 284
	 * @param state The {@link DbExportStateBase DbExportState}.
295 285
	 * @return The <code>TaxonFk</code> attribute.
296 286
	 * @see MethodMapper
......
301 291
		// since it awaits two parameters if one of them is of instance DbExportStateBase.
302 292
		Integer result = null;
303 293
		if (state != null && taxon != null) {
304
			result = state.getDbId(taxon);
294
			result = state.getDbId(taxon.getName());
305 295
		}
306 296
		return result;
307 297
	}
......
315 305
	@SuppressWarnings("unused")
316 306
	private static String getTaxonFullNameCache(AnnotatableEntity entity) {
317 307
		String result = null;
318
		if (getTaxon() != null) {
319
			TaxonNameBase taxonName = getTaxon().getName();
320
			if (taxonName != null) {
321
				result = taxonName.getTitleCache();
322
			}
323
		}
308
		result = taxon.getName().getTitleCache();
324 309
		return result;
325 310
	}
326 311

  
......
336 321
		if (getNamedArea() != null) {
337 322
			result = PesiTransformer.area2AreaId(namedArea);
338 323
		} else {
339
			logger.warn("A NamedArea could not be found for entity: " + entity.getUuid());
324
			logger.warn("This should never happen, but a NamedArea could not be found for entity: " + entity.getUuid());
340 325
		}
341 326
		return result;
342 327
	}
......
353 338
		if (getNamedArea() != null) {
354 339
			result = PesiTransformer.area2AreaCache(namedArea);
355 340
		} else {
356
			logger.warn("A NamedArea could not be found for entity: " + entity.getUuid());
341
			logger.warn("This should never happen, but a NamedArea could not be found for entity: " + entity.getUuid());
357 342
		}
358 343
		return result;
359 344
	}
......
429 414
		Integer result = null;		
430 415
		if (state != null && entity != null && entity.isInstanceOf(ReferenceBase.class)) {
431 416
			ReferenceBase reference = CdmBase.deproxy(entity, ReferenceBase.class);
432
			if (reference != null) {
433
				result = state.getDbId(reference);
434
			}
417
			result = state.getDbId(reference);
435 418
		}
436 419
		return result;
437 420
	}
......
446 429
		String result = null;
447 430
		if (entity != null && entity.isInstanceOf(ReferenceBase.class)) {
448 431
			ReferenceBase reference = CdmBase.deproxy(entity, ReferenceBase.class);
449
			if (reference != null) {
450
				result = reference.getTitle();
451
			}
432
			result = reference.getTitle();
452 433
		}
453 434
		return result;
454 435
	}

Also available in: Unified diff