View Javadoc

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