Project

General

Profile

« Previous | Next » 

Revision 51171c9d

Added by Andreas Müller almost 13 years ago

last updates for Taxon Excel Import and moving all success variables to state

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/excel/taxa/NormalExplicitImport.java
27 27
import eu.etaxonomy.cdm.common.CdmUtils;
28 28
import eu.etaxonomy.cdm.io.excel.common.ExcelRowBase.SourceDataHolder;
29 29
import eu.etaxonomy.cdm.model.agent.Team;
30
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
30 31
import eu.etaxonomy.cdm.model.common.CdmBase;
31 32
import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
33
import eu.etaxonomy.cdm.model.common.Extension;
34
import eu.etaxonomy.cdm.model.common.ExtensionType;
32 35
import eu.etaxonomy.cdm.model.common.Language;
33 36
import eu.etaxonomy.cdm.model.common.TimePeriod;
34 37
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
......
68 71
	
69 72
	public static Set<String> validMarkers = new HashSet<String>(Arrays.asList(new String[]{"", "valid", "accepted", "a", "v", "t"}));
70 73
	public static Set<String> synonymMarkers = new HashSet<String>(Arrays.asList(new String[]{"", "invalid", "synonym", "s", "i"}));
71
	
74
	public static final UUID uuidRefExtension = UUID.fromString("a46533df-7a78-448f-9b80-36d087fbdf2a");
72 75
	
73 76
	/* (non-Javadoc)
74 77
	 * @see eu.etaxonomy.cdm.io.excel.common.ExcelTaxonOrSpecimenImportBase#analyzeSingleValue(eu.etaxonomy.cdm.io.excel.common.ExcelTaxonOrSpecimenImportBase.KeyValue, eu.etaxonomy.cdm.io.excel.common.ExcelImportState)
75 78
	 */
76 79
	@Override
77
	protected boolean analyzeSingleValue(KeyValue keyValue, TaxonExcelImportState state) {
78
		boolean success = true;
80
	protected void analyzeSingleValue(KeyValue keyValue, TaxonExcelImportState state) {
79 81
		
80 82
		NormalExplicitRow normalExplicitRow = state.getCurrentRow();
81 83
		String key = keyValue.key;
......
124 126
			}else{
125 127
				String message = "Unexpected column header " + key;
126 128
				fireWarningEvent(message, state, 10);
127
				success = false;
129
				state.setUnsuccessfull();
128 130
				logger.error(message);
129 131
			}
130 132
		}
131
		return success;
133
		return;
132 134
	}
133 135

  
134 136

  
135 137
	/** 
136
	 *  Stores taxa records in DB
138
	 *  Create base taxa and add all information attached to it's name.
137 139
	 */
138 140
	@Override
139
    protected boolean firstPass(TaxonExcelImportState state) {
140
		boolean success = true;
141
    protected void firstPass(TaxonExcelImportState state) {
142
		
143
//		if (1==1){
144
//			return true;
145
//		}
146
//		System.out.println("FP:" + state.getCurrentLine());
141 147
		Rank rank = null;
142 148
		NormalExplicitRow taxonDataHolder = state.getCurrentRow();
143 149
		
......
161 167
					try {
162 168
						rank = Rank.getRankByEnglishName(rankStr, state.getConfig().getNomenclaturalCode(), false);
163 169
					} catch (UnknownCdmTypeException e) {
164
						success = false;
170
						state.setUnsuccessfull();
165 171
						logger.error(rankStr + " is not a valid rank.");
166 172
					}
167 173
				}
......
169 175
	            //taxon
170 176
				taxonBase = createTaxon(state, rank, taxonNameStr, authorStr, nameStatus);
171 177
			}else{
172
				return true;
178
				return;
173 179
			}
174 180
		}
175 181
		if (taxonBase == null){
176 182
			String message = "Taxon could not be created. Record will not be handled";
177 183
			fireWarningEvent(message, "Record: " + state.getCurrentLine(), 6);
178 184
			logger.warn(message);
179
			return false;
185
			state.setUnsuccessfull();
186
			return;
180 187
		}
181 188
		
182 189
		//protologue
......
190 197
			} catch (URISyntaxException e) {
191 198
				String warning = "URISyntaxException when trying to convert to URI: " + protologue;
192 199
				logger.error(warning);
200
				state.setUnsuccessfull();
193 201
			}	
194 202
		}
195

  
196
		//media
197
		for (String imageUrl : taxonDataHolder.getImages()){
198
			//TODO
199
			Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
200
			TaxonDescription td = taxon.getImageGallery(true);
201
			DescriptionElementBase mediaHolder;
202
			if (td.getElements().size() != 0){
203
				mediaHolder = td.getElements().iterator().next();
204
			}else{
205
				mediaHolder = TextData.NewInstance(Feature.IMAGE());
206
				td.addElement(mediaHolder);
207
			}
208
			try {
209
				Media media = getImageMedia(imageUrl, true);
210
				mediaHolder.addMedia(media);
211
			} catch (MalformedURLException e) {
212
				logger.warn("Can't add media: " + e.getMessage());
213
			}
214
		}
215

  
216
		//tdwg label
217
		for (String tdwg : taxonDataHolder.getDistributions()){
218
			//TODO
219
			Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
220
			TaxonDescription td = this.getTaxonDescription(taxon, false, true);
221
			NamedArea area = TdwgArea.getAreaByTdwgAbbreviation(tdwg);
222
			if (area == null){
223
				area = TdwgArea.getAreaByTdwgLabel(tdwg);
224
			}
225
			if (area != null){
226
				Distribution distribution = Distribution.NewInstance(area, PresenceTerm.PRESENT());
227
				td.addElement(distribution);
228
			}else{
229
				String message = "TDWG area could not be recognized: " + tdwg;
230
				logger.warn(message);
231
			}
232
			
233
		}
234
		
235

  
236 203
		
237 204
		state.putTaxon(id, taxonBase);
238 205
		getTaxonService().save(taxonBase);
239 206

  
240
		return success;
207
		return;
241 208
    }
242 209

  
243 210

  
244 211

  
245 212
	/** 
246
	 *  Stores parent-child, synonym and common name relationships
213
	 *  Stores parent-child, synonym and common name relationships.
214
	 *  Adds all taxon related descriptive information (this is not done in the first pass
215
	 *  because the information may also be attached to a synonym).
247 216
	 */
248 217
	@Override
249
    protected boolean secondPass(TaxonExcelImportState state) {
250
//		System.out.println(state.getCurrentLine());
251
		boolean success = true;
218
    protected void secondPass(TaxonExcelImportState state) {
219
		System.out.println(state.getCurrentLine());
252 220
		try {
253 221
			NormalExplicitRow taxonDataHolder = state.getCurrentRow();
254 222
			String taxonNameStr = taxonDataHolder.getScientificName();
......
282 250
								Reference citation = state.getConfig().getSourceReference();
283 251
								String microCitation = null;
284 252
								Taxon childTaxon = taxon;
285
								success &= makeParent(state, parentTaxon, childTaxon, citation, microCitation);
253
								makeParent(state, parentTaxon, childTaxon, citation, microCitation);
286 254
								getTaxonService().saveOrUpdate(parentTaxon);
287 255
							} else {
288 256
								String message = "Taxonomic parent not found for " + taxonNameStr;
289 257
								logger.warn(message);
290 258
								fireWarningEvent(message, state, 6);
291
								success = false;
259
								state.setUnsuccessfull();
292 260
							}
293 261
						}else{
294 262
							//do nothing (parent == 0) no parent exists
......
309 277
							String message = "Unhandled exception (%s) occurred during synonym import/update";
310 278
							message = String.format(message, e.getMessage());
311 279
							fireWarningEvent(message, state, 10); 
312
							success = false;
280
							state.setUnsuccessfull();
313 281
						}
314 282
					}else{
315 283
						acceptedTaxon = null;
......
328 296
				String message = "Accepted taxon could not be found. Can't add additional data (common names, descriptive data, ...) to taxon";
329 297
				fireWarningEvent(message, state, 6);
330 298
			}else{	
299
				//common names
331 300
				if (CdmUtils.isNotEmpty(commonNameStr)){			// add common name to taxon
332 301
					handleCommonName(state, taxonNameStr, commonNameStr, acceptedTaxon);
333 302
				}
334 303
				
304

  
305
				//media
306
				for (String imageUrl : taxonDataHolder.getImages()){
307
					TaxonDescription td = acceptedTaxon.getImageGallery(true);
308
					DescriptionElementBase mediaHolder;
309
					if (td.getElements().size() != 0){
310
						mediaHolder = td.getElements().iterator().next();
311
					}else{
312
						mediaHolder = TextData.NewInstance(Feature.IMAGE());
313
						td.addElement(mediaHolder);
314
					}
315
					try {
316
						Media media = getImageMedia(imageUrl, true);
317
						mediaHolder.addMedia(media);
318
					} catch (MalformedURLException e) {
319
						logger.warn("Can't add media: " + e.getMessage());
320
						state.setUnsuccessfull();
321
					}
322
				}
323

  
324
				//tdwg label
325
				for (String tdwg : taxonDataHolder.getDistributions()){
326
					TaxonDescription td = this.getTaxonDescription(acceptedTaxon, state.getConfig().getSourceReference() ,false, true);
327
					NamedArea area = TdwgArea.getAreaByTdwgAbbreviation(tdwg);
328
					if (area == null){
329
						area = TdwgArea.getAreaByTdwgLabel(tdwg);
330
					}
331
					if (area != null){
332
						Distribution distribution = Distribution.NewInstance(area, PresenceTerm.PRESENT());
333
						td.addElement(distribution);
334
					}else{
335
						String message = "TDWG area could not be recognized: " + tdwg;
336
						logger.warn(message);
337
						state.setUnsuccessfull();
338
					}
339
				}
340
				
341
				//features
335 342
				handleFeatures(state, taxonDataHolder, acceptedTaxon, nameUsedInSource);
336 343
			}
337 344
		} catch (Exception e) {
338 345
			e.printStackTrace();
339 346
		}
340
		return success;
347
		return;
341 348
	}
342 349

  
343 350

  
......
355 362
			
356 363
			for (int i = 0; i < textList.size(); i++){
357 364
				String featureText = textList.get(i);
358
				String featureLanguage = languageList.get(i);
365
				String featureLanguage = languageList == null ? null :languageList.get(i);
359 366
				Language language = getFeatureLanguage(featureLanguage, state);
360 367
				//TODO
361
				TaxonDescription td = this.getTaxonDescription(acceptedTaxon, false, true);
368
				TaxonDescription td = this.getTaxonDescription(acceptedTaxon, state.getConfig().getSourceReference() ,false, true);
362 369
				TextData textData = TextData.NewInstance(feature);
363 370
				textData.putText(language, featureText);
364 371
				td.addElement(textData);
......
374 381
					for (SourceType type : sourceMap.keySet()){
375 382
						String value = sourceMap.get(type);
376 383
						if (type.equals(SourceType.Author)){
377
							Team team = Team.NewInstance();
378
							team.setTitleCache(value, true);
379
							ref.setAuthorTeam(team);
384
							TeamOrPersonBase author = getAuthorAccordingToConfig(value, state);
385
							ref.setAuthorTeam(author);
380 386
						}else if (type.equals(SourceType.Title)) {
381 387
							ref.setTitle(value);
382 388
						}else if (type.equals(SourceType.Year)) {
383 389
							ref.setDatePublished(TimePeriod.parseString(value));
390
						}else if (type.equals(SourceType.RefExtension)) {
391
							ExtensionType extensionType = getExtensionType(state, uuidRefExtension, "RefExtension", "Reference Extension", "RefExt.");
392
							Extension extension = Extension.NewInstance(ref, value, extensionType);
384 393
						}
385 394
						refExists = true;
386 395
					}
387 396
					if (refExists){
397
						ref = getReferenceAccordingToConfig(ref, state);
388 398
						source.setCitation(ref);
389 399
						source.setNameUsedInSource(nameUsedInSource);
390 400
					}
......
394 404
		}
395 405
	}
396 406

  
407
	private Map<String, UUID> referenceMapping = new HashMap<String, UUID>();
408
	private Map<UUID, Reference> referenceStore = new HashMap<UUID, Reference>();
409
	
410
	private Reference getReferenceAccordingToConfig(Reference value, TaxonExcelImportState state) {
411
		Reference result = null;
412
		String titleCache = value.getTitleCache();
413
		UUID referenceUuid = referenceMapping.get(titleCache);
414
		if (referenceUuid != null){
415
			result = referenceStore.get(referenceUuid);
416
		}
417
		if (result == null){
418
			result = value;
419
			referenceStore.put(result.getUuid(), result);
420
		}
421
		if (referenceUuid == null){
422
			referenceMapping.put(titleCache, result.getUuid());
423
		}
424
		return result;
425
	}
426

  
427
	
428
	private Map<String, UUID> authorMapping = new HashMap<String, UUID>();
429
	private Map<UUID, TeamOrPersonBase> authorStore = new HashMap<UUID, TeamOrPersonBase>();
430
	
431
	private TeamOrPersonBase getAuthorAccordingToConfig(String value, TaxonExcelImportState state) {
432
		TeamOrPersonBase result = null;
433
		UUID authorUuid = authorMapping.get(value);
434
		if (authorUuid != null){
435
			result = authorStore.get(authorUuid);
436
		}
437
		if (result == null){
438
			//TODO parsing
439
			TeamOrPersonBase author = Team.NewInstance();
440
			author.setTitleCache(value, true);
441
			result = author; 
442
			authorStore.put(result.getUuid(), result);
443
		}
444
		if (authorUuid == null){
445
			authorMapping.put(value, result.getUuid());
446
		}
447
		return result;
448
	}
449

  
397 450

  
398 451
	private Map<String, UUID> languageMapping = new HashMap<String, UUID>();
399 452

  
......
545 598
		}
546 599
	}
547 600

  
548
	private boolean makeParent(TaxonExcelImportState state, Taxon parentTaxon, Taxon childTaxon, Reference citation, String microCitation){
549
		boolean success = true;
601
	private void makeParent(TaxonExcelImportState state, Taxon parentTaxon, Taxon childTaxon, Reference citation, String microCitation){
550 602
		Reference sec = state.getConfig().getSourceReference();
551 603
		
552 604
//		Reference sec = parentTaxon.getSec();
......
555 607
			tree = makeTree(state, sec);
556 608
		}
557 609
		if (sec.equals(childTaxon.getSec())){
558
			success &=  (null !=  tree.addParentChild(parentTaxon, childTaxon, citation, microCitation));
610
			boolean success =  (null !=  tree.addParentChild(parentTaxon, childTaxon, citation, microCitation));
611
			if (success == false){
612
				state.setUnsuccessfull();
613
			}
559 614
		}else{
560 615
			logger.warn("No relationship added for child " + childTaxon.getTitleCache());
561 616
		}
562
		return success;
617
		return;
563 618
	}
564 619
	
565 620

  

Also available in: Unified diff