Project

General

Profile

« Previous | Next » 

Revision 738cecfa

Added by Andreas Müller over 13 years ago

quick&dirty implementation of common names in TCS

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/tcsxml/in/TcsXmlImportState.java
10 10

  
11 11
package eu.etaxonomy.cdm.io.tcsxml.in;
12 12

  
13
import java.util.Map;
14

  
13 15
import org.apache.log4j.Logger;
14 16

  
15
import eu.etaxonomy.cdm.io.common.CdmImportBase;
16 17
import eu.etaxonomy.cdm.io.common.ImportStateBase;
18
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
17 19

  
18 20
/**
19 21
 * @author a.mueller
......
24 26
	@SuppressWarnings("unused")
25 27
	private static final Logger logger = Logger.getLogger(TcsXmlImportState.class);
26 28

  
29
	//TODO make it better
30
	private Map<String, CommonTaxonName> commonNameMap = null;
31
	
27 32
	public TcsXmlImportState(TcsXmlImportConfigurator config) {
28 33
		super(config);
29 34
	}
30 35
	
36
	public Map<String, CommonTaxonName> getCommonNameMap() {
37
		return commonNameMap;
38
	}
39

  
40
	public void setCommonNameMap(Map<String, CommonTaxonName> commonNameMap) {
41
		this.commonNameMap = commonNameMap;
42
	}
43

  
44

  
45
	
46
	
47
	
31 48
//	/* (non-Javadoc)
32 49
//	 * @see eu.etaxonomy.cdm.io.common.IoStateBase#initialize(eu.etaxonomy.cdm.io.common.IoConfiguratorBase)
33 50
//	 */
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/tcsxml/in/TcsXmlTaxonImport.java
10 10
package eu.etaxonomy.cdm.io.tcsxml.in;
11 11

  
12 12
import java.util.ArrayList;
13
import java.util.HashMap;
13 14
import java.util.HashSet;
14 15
import java.util.Iterator;
15 16
import java.util.List;
17
import java.util.Map;
16 18
import java.util.Set;
17 19

  
18 20
import org.apache.log4j.Logger;
......
30 32
import eu.etaxonomy.cdm.io.common.ImportHelper;
31 33
import eu.etaxonomy.cdm.io.common.MapWrapper;
32 34
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
35
import eu.etaxonomy.cdm.model.common.Language;
36
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
33 37
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
34 38
import eu.etaxonomy.cdm.model.name.NonViralName;
35 39
import eu.etaxonomy.cdm.model.name.Rank;
36 40
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
37
import eu.etaxonomy.cdm.model.reference.IGeneric;
38 41
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
39 42
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
40 43
import eu.etaxonomy.cdm.model.taxon.Synonym;
......
128 131
		MapWrapper<TaxonBase> taxonMap = (MapWrapper<TaxonBase>)state.getStore(ICdmIO.TAXON_STORE);
129 132
		MapWrapper<TaxonNameBase<?,?>> taxonNameMap = (MapWrapper<TaxonNameBase<?,?>>)state.getStore(ICdmIO.TAXONNAME_STORE);
130 133
		MapWrapper<ReferenceBase> referenceMap = (MapWrapper<ReferenceBase>)state.getStore(ICdmIO.REFERENCE_STORE);
134
		Map<String, CommonTaxonName> commonNameMap = new HashMap<String, CommonTaxonName>();
135
		
131 136
		ITaxonService taxonService = getTaxonService();
132 137

  
133 138
		ResultWrapper<Boolean> success = ResultWrapper.NewInstance(true);
......
166 171
			childName = "Name";
167 172
			obligatory = true;
168 173
			Element elName = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
169
			TaxonNameBase<?,?> taxonName = makeName(elName, null, taxonNameMap, success);
170
			elementList.add(childName.toString());
171
			
172
			//TODO how to handle
173
			childName = "Rank";
174
			obligatory = false;
175
			Element elRank = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
176
			Rank rank = TcsXmlTaxonNameImport.makeRank(elRank);
177
			if (rank != null){
178
				logger.warn("Rank in TaxonIO not yet implemented");
179
			}
180
			elementList.add(childName.toString());
181
			
182
			childName = "AccordingTo";
183
			obligatory = false;
184
			Element elAccordingTo = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
185
			ReferenceBase sec = makeAccordingTo(elAccordingTo, referenceMap, success);
186
			elementList.add(childName.toString());
187
			// TODO may sec be null?
188
			if (sec == null){
189
				sec = unknownSec();
190
			}
191
			
192
			TaxonBase taxonBase;
193
			if (synonymIdSet.contains(strId)){
194
				taxonBase = Synonym.NewInstance(taxonName, sec);
174
			if (isVernacular(success, elName)){
175
				handleVernacularName(success, strId, elName, commonNameMap);
195 176
			}else{
196
				taxonBase = Taxon.NewInstance(taxonName, sec);	
177
				TaxonNameBase<?,?> taxonName = makeScientificName(elName, null, taxonNameMap, success);
178
				elementList.add(childName.toString());
179
				
180
				//TODO how to handle
181
				childName = "Rank";
182
				obligatory = false;
183
				Element elRank = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
184
				Rank rank = TcsXmlTaxonNameImport.makeRank(elRank);
185
				if (rank != null){
186
					logger.warn("Rank in TaxonIO not yet implemented");
187
				}
188
				elementList.add(childName.toString());
189
				
190
				childName = "AccordingTo";
191
				obligatory = false;
192
				Element elAccordingTo = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
193
				ReferenceBase sec = makeAccordingTo(elAccordingTo, referenceMap, success);
194
				elementList.add(childName.toString());
195
				// TODO may sec be null?
196
				if (sec == null){
197
					sec = unknownSec();
198
				}
199
				
200
				TaxonBase taxonBase;
201
				if (synonymIdSet.contains(strId)){
202
					taxonBase = Synonym.NewInstance(taxonName, sec);
203
				}else{
204
					taxonBase = Taxon.NewInstance(taxonName, sec);	
205
				}
206
				
207
				childName = "TaxonRelationships";
208
				obligatory = false;
209
				Element elTaxonRelationships = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
210
				makeTaxonRelationships(taxonBase, elTaxonRelationships, success);
211
				elementList.add(childName.toString());
212
	
213
				childName = "SpecimenCircumscription";
214
				obligatory = false;
215
				Element elSpecimenCircumscription = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
216
				makeSpecimenCircumscription(taxonBase, elSpecimenCircumscription, success);
217
				elementList.add(childName.toString());
218
	
219
				childName = "CharacterCircumscription";
220
				obligatory = false;
221
				Element elCharacterCircumscription = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
222
				makeCharacterCircumscription(taxonBase, elCharacterCircumscription, success);
223
				elementList.add(childName.toString());
224
	
225
				
226
				childName = "ProviderLink";
227
				obligatory = false;
228
				Element elProviderLink = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
229
				makeProviderLink(taxonBase, elProviderLink, success);
230
				elementList.add(childName.toString());
231
				
232
				childName = "ProviderSpecificData";
233
				obligatory = false;
234
				Element elProviderSpecificData = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
235
				makeProviderSpecificData(taxonBase, elProviderSpecificData, success);
236
				elementList.add(childName.toString());
237
	
238
				testAdditionalElements(elTaxonConcept, elementList);
239
				ImportHelper.setOriginalSource(taxonBase, config.getSourceReference(), strId, idNamespace);
240
				taxonMap.put(strId, taxonBase);
197 241
			}
198 242
			
199
			childName = "TaxonRelationships";
200
			obligatory = false;
201
			Element elTaxonRelationships = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
202
			makeTaxonRelationships(taxonBase, elTaxonRelationships, success);
203
			elementList.add(childName.toString());
204

  
205
			childName = "SpecimenCircumscription";
206
			obligatory = false;
207
			Element elSpecimenCircumscription = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
208
			makeSpecimenCircumscription(taxonBase, elSpecimenCircumscription, success);
209
			elementList.add(childName.toString());
210

  
211
			childName = "CharacterCircumscription";
212
			obligatory = false;
213
			Element elCharacterCircumscription = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
214
			makeCharacterCircumscription(taxonBase, elCharacterCircumscription, success);
215
			elementList.add(childName.toString());
216

  
217
			
218
			childName = "ProviderLink";
219
			obligatory = false;
220
			Element elProviderLink = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
221
			makeProviderLink(taxonBase, elProviderLink, success);
222
			elementList.add(childName.toString());
223
			
224
			childName = "ProviderSpecificData";
225
			obligatory = false;
226
			Element elProviderSpecificData = XmlHelp.getSingleChildElement(success, elTaxonConcept, childName, tcsNamespace, obligatory);
227
			makeProviderSpecificData(taxonBase, elProviderSpecificData, success);
228
			elementList.add(childName.toString());
229

  
230
			testAdditionalElements(elTaxonConcept, elementList);
231
			ImportHelper.setOriginalSource(taxonBase, config.getSourceReference(), strId, idNamespace);
232
			taxonMap.put(strId, taxonBase);
233
			
234 243
		}
244
		state.setCommonNameMap(commonNameMap);
245
		
235 246
		//invokeRelations(source, cdmApp, deleteAll, taxonMap, referenceMap);
236 247
		logger.info(i + " taxa handled. Saving ...");
237 248
		taxonService.save(taxonMap.objects());
......
239 250
		return success.getValue();
240 251
	}
241 252
	
253
	private void handleVernacularName(ResultWrapper<Boolean> success, String taxonId, Element elName, Map<String, CommonTaxonName> commonNameMap) {
254
		String name = elName.getTextNormalize();
255
		//TODO ref
256
		
257
		Language language = null;
258
		String strLanguage = elName.getAttributeValue("language");
259
		//TODO
260
		//Language
261
		if (strLanguage != null){
262
			language = Language.getLanguageByLabel(strLanguage);
263
			if (language == null){
264
				language = Language.getLanguageByDescription(strLanguage);
265
			}
266
			if (language == null){
267
				logger.warn("language ("+strLanguage+") not found for name " + name);	
268
			}
269
		}
270
		CommonTaxonName commonName = CommonTaxonName.NewInstance(name, language);
271
		commonNameMap.put(taxonId, commonName);
272
		
273
		//TODO check other elements
274
	}
275

  
276

  
277
	private boolean isVernacular(ResultWrapper<Boolean> success, Element elName) {
278
		try{
279
			String strScientific = elName.getAttributeValue("scientific");
280
			boolean scientific = Boolean.valueOf(strScientific);
281
			return ! scientific;
282
		} catch (Exception e) {
283
			logger.warn("Value for scientific is not boolean");
284
			success.setValue(false);
285
			return false;
286
		}
287
	}
288

  
289

  
242 290
	
243 291
	private boolean hasIsSynonymRelation(Element taxonConcept, Namespace rdfNamespace){
244 292
		boolean result = false;
......
265 313
	 * @param elTaxonRelationships
266 314
	 * @param success
267 315
	 */
268
	private TaxonNameBase<?, ?> makeName(Element elName, NomenclaturalCode code, MapWrapper<? extends TaxonNameBase<?,?>> objectMap, ResultWrapper<Boolean> success){
316
	private TaxonNameBase<?, ?> makeScientificName(Element elName, NomenclaturalCode code, MapWrapper<? extends TaxonNameBase<?,?>> objectMap, ResultWrapper<Boolean> success){
269 317
		TaxonNameBase<?, ?> result = null;
270 318
		if (elName != null){
271
			//scientific
272
			try {
273
				String strScientific = elName.getAttributeValue("scientific");
274
				boolean scientific = Boolean.valueOf(strScientific);
275
				if (! scientific){
276
					//TODO
277
					logger.warn("Non scientific names not yet implemented");
278
					success.setValue(false);
279
				}
280
			} catch (Exception e) {
281
				logger.warn("Value for scientific is not boolean");
282
			}
283 319
			String language = elName.getAttributeValue("language");
284
			//TODO
285 320
			//Language
286 321
			if (language != null){
287
				logger.warn("language for name not yet implemented");	
322
				logger.warn("language for name not yet implemented. Language for scientific name should always be Latin");	
288 323
			}
289 324
			Class<? extends IdentifiableEntity> clazz = (Class<? extends IdentifiableEntity>)NonViralName.class;
290 325
			if (code != null){
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/tcsxml/in/TcsXmlTaxonRelationsImport.java
11 11

  
12 12
import java.util.HashSet;
13 13
import java.util.List;
14
import java.util.Map;
14 15
import java.util.Set;
15 16

  
16 17
import org.apache.log4j.Logger;
......
23 24
import eu.etaxonomy.cdm.io.common.ICdmIO;
24 25
import eu.etaxonomy.cdm.io.common.MapWrapper;
25 26
import eu.etaxonomy.cdm.io.tcsxml.TcsXmlTransformer;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
26 28
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
29
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
30
import eu.etaxonomy.cdm.model.description.TaxonDescription;
27 31
import eu.etaxonomy.cdm.model.name.NonViralName;
28 32
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
29 33
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
......
213 217
		try {
214 218
			ResultWrapper<Boolean> isInverse = new ResultWrapper<Boolean>();
215 219
			isInverse.setValue(false);
216
			RelationshipTermBase<?> relType = TcsXmlTransformer.tcsRelationshipType2Relationship(strRelType, isInverse);
217
			
218
			//toTaxon (should be part of relationshipType)
219
			boolean isSynonym = (relType instanceof SynonymRelationshipType);
220
			TaxonBase toTaxon = getToTaxon(elRelationship, taxonMap, isSynonym, success);
221
			
222
			if (toTaxon != null && fromTaxon != null){
223
				//exchange taxa if relationship is inverse
224
				if (isInverse.getValue() == true ){
225
					TaxonBase tmp = toTaxon;
226
					toTaxon = fromTaxon;
227
					fromTaxon = tmp;
228
				}
220
			if ("has vernacular".equalsIgnoreCase(strRelType)){
221
				handleVernacular(success, state, elRelationship, fromTaxon);
222
			}else{
223
				RelationshipTermBase<?> relType = TcsXmlTransformer.tcsRelationshipType2Relationship(strRelType, isInverse);
229 224
				
230
				//Create relationship
231
				if (! (toTaxon instanceof Taxon)){
232
					logger.warn("TaxonBase toTaxon is not of Type 'Taxon'. Relationship is not added.");
233
					success.setValue(false);
234
				}else{
235
					Taxon taxonTo = (Taxon)toTaxon;
236
					ReferenceBase citation = null;
237
					String microReference = null;
238
					if (relType instanceof SynonymRelationshipType){
239
						SynonymRelationshipType synRelType = (SynonymRelationshipType)relType;
240
						if (! (fromTaxon instanceof Synonym )){
241
							logger.warn("TaxonBase fromTaxon is not of Type 'Synonym'. Relationship is not added.");
242
							success.setValue(false);
243
						}else{
244
							Synonym synonym = (Synonym)fromTaxon;
245
							TaxonNameBase<?,?> synName = synonym.getName();
246
							TaxonNameBase<?,?> accName = taxonTo.getName();
247
							if (synName != null && accName != null && synName.isHomotypic(accName)
248
										&& ( synRelType.equals(SynonymRelationshipType.SYNONYM_OF()))){
249
								synRelType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF(); 
250
							}
251
							if (! relationExists(taxonTo, synonym, synRelType)){
252
								taxonTo.addSynonym(synonym, synRelType,  citation, microReference);	
253
							}else{
254
								//TODO citation, microReference
255
								//TODO different synRelTypes -> warning
225
				//toTaxon (should be part of relationshipType)
226
				boolean isSynonym = (relType instanceof SynonymRelationshipType);
227
				TaxonBase toTaxon = getToTaxon(elRelationship, taxonMap, isSynonym, success);
228
				
229
				if (toTaxon != null && fromTaxon != null){
230
					//exchange taxa if relationship is inverse
231
					if (isInverse.getValue() == true ){
232
						TaxonBase tmp = toTaxon;
233
						toTaxon = fromTaxon;
234
						fromTaxon = tmp;
235
					}
236
					
237
					//Create relationship
238
					if (! (toTaxon instanceof Taxon)){
239
						logger.warn("TaxonBase toTaxon is not of Type 'Taxon'. Relationship is not added.");
240
						success.setValue(false);
241
					}else{
242
						Taxon taxonTo = (Taxon)toTaxon;
243
						ReferenceBase citation = null;
244
						String microReference = null;
245
						if (relType instanceof SynonymRelationshipType){
246
							SynonymRelationshipType synRelType = (SynonymRelationshipType)relType;
247
							if (! (fromTaxon instanceof Synonym )){
248
								logger.warn("TaxonBase fromTaxon is not of Type 'Synonym'. Relationship is not added.");
256 249
								success.setValue(false);
250
							}else{
251
								Synonym synonym = (Synonym)fromTaxon;
252
								TaxonNameBase<?,?> synName = synonym.getName();
253
								TaxonNameBase<?,?> accName = taxonTo.getName();
254
								if (synName != null && accName != null && synName.isHomotypic(accName)
255
											&& ( synRelType.equals(SynonymRelationshipType.SYNONYM_OF()))){
256
									synRelType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF(); 
257
								}
258
								if (! relationExists(taxonTo, synonym, synRelType)){
259
									taxonTo.addSynonym(synonym, synRelType,  citation, microReference);	
260
								}else{
261
									//TODO citation, microReference
262
									//TODO different synRelTypes -> warning
263
									success.setValue(false);
264
								}
257 265
							}
266
						}else if (relType instanceof TaxonRelationshipType){
267
							makeTaxonRelationship(state, (TaxonRelationshipType)relType, fromTaxon, taxonTo, citation, microReference, success);
268
						}else{
269
							logger.warn("Unknown Relationshiptype");
270
							success.setValue(false);
258 271
						}
259
					}else if (relType instanceof TaxonRelationshipType){
260
						makeTaxonRelationship(state, (TaxonRelationshipType)relType, fromTaxon, taxonTo, citation, microReference, success);
261
					}else{
262
						logger.warn("Unknown Relationshiptype");
263
						success.setValue(false);
272
						taxonStore.add(toTaxon);
264 273
					}
265
					taxonStore.add(toTaxon);
266
				}
267
			}else{
268
				if (toTaxon == null){
269
					logger.warn("toTaxon (" + /*strToTaxon + */ ") could  not be found in taxonMap. Relationship of type " + strRelType + " was not added to CDM");
270
				}
271
				if (fromTaxon == null){
272
					logger.warn("fromTaxon (" + /*strTaxonAbout + */") could not be found in taxonMap. Relationship was not added to CDM");
274
				}else{
275
					if (toTaxon == null){
276
						logger.warn("toTaxon (" + /*strToTaxon + */ ") could  not be found in taxonMap. Relationship of type " + strRelType + " was not added to CDM");
277
					}
278
					if (fromTaxon == null){
279
						logger.warn("fromTaxon (" + /*strTaxonAbout + */") could not be found in taxonMap. Relationship was not added to CDM");
280
					}
281
					success.setValue(false);
273 282
				}
274
				success.setValue(false);
275
			}
276
			
283
			}		
277 284
		} catch (UnknownCdmTypeException e) {
278 285
			//TODO
279 286
			logger.warn("relationshipType " + strRelType + " not yet implemented");
......
282 289
		return;
283 290
	}
284 291
	
292
	private void handleVernacular(ResultWrapper<Boolean> success, TcsXmlImportState state, Element elRelationship, TaxonBase taxonBase) {
293
		if (! taxonBase.isInstanceOf(Taxon.class)){
294
			logger.warn("From Taxon is not of type Taxon but of type " +  taxonBase.getClass().getSimpleName());
295
			success.setValue(false);
296
			return;
297
		}else{
298
			Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
299
			Map<String, CommonTaxonName> commonNameMap = state.getCommonNameMap();
300
			CommonTaxonName commonTaxonName = getCommonName(elRelationship, commonNameMap, success);
301
			TaxonDescription description = getDescription(taxon);
302
			description.addElement(commonTaxonName);
303
		}
304
	}
305
	
306
	private TaxonDescription getDescription(Taxon taxon) {
307
		if (taxon.getDescriptions().isEmpty()){
308
			return TaxonDescription.NewInstance(taxon);
309
		}else{
310
			//TODO only if the description represents this TCS file
311
			return taxon.getDescriptions().iterator().next();
312
		}
313
	}
314

  
315
	private CommonTaxonName getCommonName(Element elTaxonRelationship, Map<String, CommonTaxonName> commonNameMap, ResultWrapper<Boolean> success){
316
		CommonTaxonName result = null;
317
		if (elTaxonRelationship == null || commonNameMap == null){
318
			success.setValue(false);
319
		}else{
320
			String childName = "ToTaxonConcept";
321
			boolean obligatory = true;
322
			Element elToTaxonConcept = XmlHelp.getSingleChildElement(success, elTaxonRelationship, childName, elTaxonRelationship.getNamespace(), obligatory);
323
			
324
			String linkType = elToTaxonConcept.getAttributeValue("linkType");
325
			if (linkType == null || linkType.equals("local")){
326
				String ref = elToTaxonConcept.getAttributeValue("ref");
327
				if (ref != null){
328
					result = commonNameMap.get(ref);
329
				}else{
330
					logger.warn("Non ref not yet implemented for vernacular name relationship");
331
				}
332
			}else{
333
				logger.warn("External link types for vernacular name not yet implemented");
334
			}
335
		}
336
		return result;
337
	}
338

  
285 339
	private void makeTaxonRelationship(TcsXmlImportState state, TaxonRelationshipType relType, TaxonBase fromTaxon, Taxon taxonTo, ReferenceBase citation, String microReference, ResultWrapper<Boolean> success){
286 340
		TaxonRelationshipType taxRelType = (TaxonRelationshipType)relType;
287 341
		if (! (fromTaxon instanceof Taxon )){

Also available in: Unified diff