Project

General

Profile

Download (10.5 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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

    
10
package eu.etaxonomy.cdm.io.tcsrdf;
11

    
12
import java.util.List;
13
import java.util.Set;
14

    
15
import org.apache.log4j.Logger;
16
import org.jdom.Attribute;
17
import org.jdom.Element;
18
import org.jdom.Namespace;
19
import org.springframework.stereotype.Component;
20

    
21
import eu.etaxonomy.cdm.common.XmlHelp;
22
import eu.etaxonomy.cdm.io.common.ICdmIO;
23
import eu.etaxonomy.cdm.io.common.ImportHelper;
24
import eu.etaxonomy.cdm.io.common.MapWrapper;
25
import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
26
import eu.etaxonomy.cdm.model.agent.Team;
27
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
28
import eu.etaxonomy.cdm.model.common.Marker;
29
import eu.etaxonomy.cdm.model.common.MarkerType;
30
import eu.etaxonomy.cdm.model.common.TimePeriod;
31
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
32
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
33
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
34
import eu.etaxonomy.cdm.model.name.NonViralName;
35
import eu.etaxonomy.cdm.model.name.Rank;
36
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
37
import eu.etaxonomy.cdm.model.reference.IGeneric;
38
import eu.etaxonomy.cdm.model.reference.Reference;
39
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
40
import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
41

    
42
/**
43
 * @author a.mueller
44
 * @created 29.05.2008
45
 * @version 1.0
46
 */
47
@Component
48
public class TcsRdfTaxonNameImport  extends TcsRdfImportBase implements ICdmIO<TcsRdfImportState> {
49
	private static final Logger logger = Logger.getLogger(TcsRdfTaxonNameImport.class);
50

    
51
	private static int modCount = 5000;
52
	
53
	public TcsRdfTaxonNameImport(){
54
		super();
55
	}
56

    
57
	@Override
58
	public boolean doCheck(TcsRdfImportState config){
59
		boolean result = true;
60
		logger.warn("BasionymRelations not yet implemented");
61
		logger.warn("Checking for TaxonNames not yet implemented");
62
		//result &= checkArticlesWithoutJournal(tcsConfig);
63
		//result &= checkPartOfJournal(tcsConfig);
64
		
65
		return result;
66
	}
67
	
68
	protected static CdmSingleAttributeXmlMapperBase[] standardMappers = new CdmSingleAttributeXmlMapperBase[]{
69
		new CdmTextElementMapper("genusPart", "genusOrUninomial") 
70
		, new CdmTextElementMapper("uninomial", "genusOrUninomial")  //TODO make it a more specific Mapper for both attributes
71
		, new CdmTextElementMapper("specificEpithet", "specificEpithet")
72
		, new CdmTextElementMapper("infraspecificEpithet", "infraSpecificEpithet")
73
		, new CdmTextElementMapper("infragenericEpithet", "infraGenericEpithet")
74
		, new CdmTextElementMapper("microReference", nsTcom, "nomenclaturalMicroReference")		
75
		
76
	};
77

    
78
	protected static CdmSingleAttributeXmlMapperBase[] operationalMappers = new CdmSingleAttributeXmlMapperBase[]{
79
		new CdmUnclearMapper("basionymAuthorship")
80
		, new CdmUnclearMapper("combinationAuthorship")
81
		, new CdmUnclearMapper("hasAnnotation")
82
		, new CdmUnclearMapper("rank")
83
		, new CdmUnclearMapper("nomenclaturalCode")
84
		, new CdmUnclearMapper("publishedIn", nsTcom)
85
		, new CdmUnclearMapper("year")
86
	};
87
	
88
	protected static CdmSingleAttributeXmlMapperBase[] unclearMappers = new CdmSingleAttributeXmlMapperBase[]{
89
		new CdmUnclearMapper("authorship")
90
		, new CdmUnclearMapper("rankString")
91
		, new CdmUnclearMapper("nameComplete")
92
		, new CdmUnclearMapper("hasBasionym")
93
		, new CdmUnclearMapper("dateOfEntry", nsTpalm)	
94
	};
95
	
96
	@Override
97
	protected void doInvoke(TcsRdfImportState state){
98
		
99
		MapWrapper<TaxonNameBase> taxonNameMap = (MapWrapper<TaxonNameBase>)state.getStore(ICdmIO.TAXONNAME_STORE);
100
		MapWrapper<Reference> referenceMap = (MapWrapper<Reference>)state.getStore(ICdmIO.REFERENCE_STORE);
101
		MapWrapper<TeamOrPersonBase> authorMap = (MapWrapper<TeamOrPersonBase>)state.getStore(ICdmIO.TEAM_STORE);
102

    
103
		String tcsElementName;
104
		Namespace tcsNamespace;
105
		String value;
106
		
107
		logger.info("start makeTaxonNames ...");
108
		TcsRdfImportConfigurator config = state.getConfig();
109
		Element root = config.getSourceRoot();
110
		
111
		Namespace rdfNamespace = config.getRdfNamespace();
112
		Namespace taxonNameNamespace = config.getTnNamespace();
113
		
114
		String idNamespace = "TaxonName";
115
		
116
		List<Element> elTaxonNames = root.getChildren("TaxonName", taxonNameNamespace);
117
		
118
		int i = 0;
119
		//for each taxonName
120
		for (Element elTaxonName : elTaxonNames){
121
			
122
			if ((++i % modCount) == 0){ logger.info("Names handled: " + (i-1));}
123
			
124
			Attribute about = elTaxonName.getAttribute("about", rdfNamespace);
125

    
126
			//create TaxonName element
127
			String nameAbout = elTaxonName.getAttributeValue("about", rdfNamespace);
128
			String strRank = XmlHelp.getChildAttributeValue(elTaxonName, "rank", taxonNameNamespace, "resource", rdfNamespace);
129
			String strNomenclaturalCode = XmlHelp.getChildAttributeValue(elTaxonName, "nomenclaturalCode", taxonNameNamespace, "resource", rdfNamespace);
130
			
131
			try {
132
				Rank rank = TcsRdfTransformer.rankString2Rank(strRank);
133
				NomenclaturalCode nomCode = TcsRdfTransformer.nomCodeString2NomCode(strNomenclaturalCode);
134
				TaxonNameBase nameBase = nomCode.getNewTaxonNameInstance(rank);
135
				
136
				Set<String> omitAttributes = null;
137
				makeStandardMapper(elTaxonName, nameBase, omitAttributes, standardMappers);
138
				
139
				//Reference
140
				//TODO
141
				tcsElementName = "publishedIn";
142
				tcsNamespace = config.getCommonNamespace();
143
				value = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, tcsNamespace);
144
				if (value != null){
145
					ReferenceFactory refFactory = ReferenceFactory.newInstance();
146
					IGeneric nomRef = refFactory.newGeneric(); //TODO
147
					nomRef.setTitleCache(value, true);
148
					nameBase.setNomenclaturalReference(nomRef);
149
					
150
					//TODO
151
					tcsElementName = "year";
152
					tcsNamespace = taxonNameNamespace;
153
					Integer year = null;
154
					value = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, tcsNamespace);
155
					if (value != null){
156
						try {
157
							year = Integer.valueOf(value);
158
							TimePeriod timeP = TimePeriod.NewInstance(year);
159
							nomRef.setDatePublished(timeP);
160
						} catch (RuntimeException e) {
161
							logger.warn("year could not be parsed");
162
						}
163
					}
164
					if (state.getConfig().isPublishReferences()){
165
						((Reference)nomRef).addMarker(Marker.NewInstance(MarkerType.PUBLISH(), false));
166
					}
167
				}
168
						
169
				//Status
170
				tcsNamespace = taxonNameNamespace;
171
				Element elAnnotation = elTaxonName.getChild("hasAnnotation", tcsNamespace);
172
				if (elAnnotation != null){
173
					Element elNomenclaturalNote = elAnnotation.getChild("NomenclaturalNote", tcsNamespace);
174
					if (elNomenclaturalNote != null){
175
						String statusValue = (String)ImportHelper.getXmlInputValue(elNomenclaturalNote, "note", tcsNamespace);
176
						String type = XmlHelp.getChildAttributeValue(elNomenclaturalNote, "type", tcsNamespace, "resource", rdfNamespace);
177
						String tdwgType = "http://rs.tdwg.org/ontology/voc/TaxonName#PublicationStatus";
178
						if (tdwgType.equalsIgnoreCase(type)){
179
							try {
180
								NomenclaturalStatusType statusType = TcsRdfTransformer.nomStatusString2NomStatus(statusValue);
181
								//NomenclaturalStatusType statusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusValue);
182
								if (statusType != null){
183
									nameBase.addStatus(NomenclaturalStatus.NewInstance(statusType));
184
								}
185
							} catch (UnknownCdmTypeException e) {
186
								if (! statusValue.equals("valid")){
187
									logger.warn("Unknown NomenclaturalStatusType: " +  statusValue);
188
								}
189
							}
190
						}
191
					}
192
				}
193
				
194
				if (nameBase instanceof NonViralName){
195
					NonViralName<?> nonViralName = (NonViralName<?>)nameBase;
196
					
197
					//AuthorTeams
198
					//TODO
199
					tcsElementName = "basionymAuthorship";
200
					String basionymAuthorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
201
					if (basionymAuthorValue != null){
202
						INomenclaturalAuthor basionymAuthor = Team.NewInstance();
203
						basionymAuthor.setNomenclaturalTitle(basionymAuthorValue);
204
						nonViralName.setBasionymAuthorTeam(basionymAuthor);
205
					}
206
						
207
					//TODO
208
					tcsElementName = "combinationAuthorship";
209
					String combinationAuthorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
210
					if (combinationAuthorValue != null){
211
						INomenclaturalAuthor combinationAuthor = Team.NewInstance();
212
						combinationAuthor.setNomenclaturalTitle(combinationAuthorValue);
213
						nonViralName.setCombinationAuthorTeam(combinationAuthor);
214
					}
215
					
216
					//set the authorshipCache
217
					tcsElementName = "authorship";
218
					String authorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
219
					String cache = nonViralName.getAuthorshipCache();
220
					if ( authorValue != null){
221
						//compare existing authorship cache with new one and check if it is necessary to 
222
						//make cache protected  //TODO refinement
223
						if (cache == null){
224
							nonViralName.setAuthorshipCache(authorValue);
225
						}else{
226
							cache = basionymAuthorValue == null ? cache : cache.replace(basionymAuthorValue, "");
227
							cache = combinationAuthorValue == null ? cache : cache.replace(combinationAuthorValue, "");
228
							cache = cache.replace("\\(|\\)", "");
229
							cache = cache.trim();
230
							if (! cache.equals("")){
231
								nonViralName.setAuthorshipCache(authorValue);
232
							}
233
						}
234
					}
235
				}
236
				ImportHelper.setOriginalSource(nameBase, config.getSourceReference(), nameAbout, idNamespace);
237
				
238
				checkAdditionalContents(elTaxonName, standardMappers, operationalMappers, unclearMappers);
239
				
240
				//nameId
241
				//TODO
242
				//ImportHelper.setOriginalSource(nameBase, tcsConfig.getSourceReference(), nameId);
243
				taxonNameMap.put(nameAbout, nameBase);
244
				
245
			} catch (UnknownCdmTypeException e) {
246
				//FIXME
247
				logger.warn("Name with id " + nameAbout + " has unknown rank " + strRank + " and could not be saved.");
248
				state.setUnsuccessfull();
249
			}
250
		}
251
		logger.info(i + " names handled");
252
		getNameService().save(taxonNameMap.objects());
253
//		makeNameSpecificData(nameMap);
254
		logger.info("end makeTaxonNames ...");
255
		return;
256

    
257
	}
258
	/* (non-Javadoc)
259
	 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
260
	 */
261
	protected boolean isIgnore(TcsRdfImportState state){
262
		return ! state.getConfig().isDoTaxonNames();
263
	}
264

    
265
}
(10-10/13)