merge trunk to cdm-3.3 branch
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / tcsrdf / TcsRdfTaxonNameImport.java
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 IGeneric nomRef = ReferenceFactory.newGeneric(); //TODO
146 nomRef.setTitleCache(value, true);
147 nameBase.setNomenclaturalReference(nomRef);
148
149 //TODO
150 tcsElementName = "year";
151 tcsNamespace = taxonNameNamespace;
152 Integer year = null;
153 value = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, tcsNamespace);
154 if (value != null){
155 try {
156 year = Integer.valueOf(value);
157 TimePeriod timeP = TimePeriod.NewInstance(year);
158 nomRef.setDatePublished(timeP);
159 } catch (RuntimeException e) {
160 logger.warn("year could not be parsed");
161 }
162 }
163 if (state.getConfig().isPublishReferences()){
164 ((Reference<?>)nomRef).addMarker(Marker.NewInstance(MarkerType.PUBLISH(), false));
165 }
166 }
167
168 //Status
169 tcsNamespace = taxonNameNamespace;
170 Element elAnnotation = elTaxonName.getChild("hasAnnotation", tcsNamespace);
171 if (elAnnotation != null){
172 Element elNomenclaturalNote = elAnnotation.getChild("NomenclaturalNote", tcsNamespace);
173 if (elNomenclaturalNote != null){
174 String statusValue = (String)ImportHelper.getXmlInputValue(elNomenclaturalNote, "note", tcsNamespace);
175 String type = XmlHelp.getChildAttributeValue(elNomenclaturalNote, "type", tcsNamespace, "resource", rdfNamespace);
176 String tdwgType = "http://rs.tdwg.org/ontology/voc/TaxonName#PublicationStatus";
177 if (tdwgType.equalsIgnoreCase(type)){
178 try {
179 NomenclaturalStatusType statusType = TcsRdfTransformer.nomStatusString2NomStatus(statusValue);
180 //NomenclaturalStatusType statusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusValue);
181 if (statusType != null){
182 nameBase.addStatus(NomenclaturalStatus.NewInstance(statusType));
183 }
184 } catch (UnknownCdmTypeException e) {
185 if (! statusValue.equals("valid")){
186 logger.warn("Unknown NomenclaturalStatusType: " + statusValue);
187 }
188 }
189 }
190 }
191 }
192
193 if (nameBase instanceof NonViralName){
194 NonViralName<?> nonViralName = (NonViralName<?>)nameBase;
195
196 //AuthorTeams
197 //TODO
198 tcsElementName = "basionymAuthorship";
199 String basionymAuthorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
200 if (basionymAuthorValue != null){
201 INomenclaturalAuthor basionymAuthor = Team.NewInstance();
202 basionymAuthor.setNomenclaturalTitle(basionymAuthorValue);
203 nonViralName.setBasionymAuthorTeam(basionymAuthor);
204 }
205
206 //TODO
207 tcsElementName = "combinationAuthorship";
208 String combinationAuthorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
209 if (combinationAuthorValue != null){
210 INomenclaturalAuthor combinationAuthor = Team.NewInstance();
211 combinationAuthor.setNomenclaturalTitle(combinationAuthorValue);
212 nonViralName.setCombinationAuthorTeam(combinationAuthor);
213 }
214
215 //set the authorshipCache
216 tcsElementName = "authorship";
217 String authorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
218 String cache = nonViralName.getAuthorshipCache();
219 if ( authorValue != null){
220 //compare existing authorship cache with new one and check if it is necessary to
221 //make cache protected //TODO refinement
222 if (cache == null){
223 nonViralName.setAuthorshipCache(authorValue);
224 }else{
225 cache = basionymAuthorValue == null ? cache : cache.replace(basionymAuthorValue, "");
226 cache = combinationAuthorValue == null ? cache : cache.replace(combinationAuthorValue, "");
227 cache = cache.replace("\\(|\\)", "");
228 cache = cache.trim();
229 if (! cache.equals("")){
230 nonViralName.setAuthorshipCache(authorValue);
231 }
232 }
233 }
234 }
235 ImportHelper.setOriginalSource(nameBase, config.getSourceReference(), nameAbout, idNamespace);
236
237 checkAdditionalContents(elTaxonName, standardMappers, operationalMappers, unclearMappers);
238
239 //nameId
240 //TODO
241 //ImportHelper.setOriginalSource(nameBase, tcsConfig.getSourceReference(), nameId);
242 taxonNameMap.put(nameAbout, nameBase);
243
244 } catch (UnknownCdmTypeException e) {
245 //FIXME
246 logger.warn("Name with id " + nameAbout + " has unknown rank " + strRank + " and could not be saved.");
247 state.setUnsuccessfull();
248 }
249 }
250 logger.info(i + " names handled");
251 getNameService().save(taxonNameMap.objects());
252 // makeNameSpecificData(nameMap);
253 logger.info("end makeTaxonNames ...");
254 return;
255
256 }
257 /* (non-Javadoc)
258 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
259 */
260 @Override
261 protected boolean isIgnore(TcsRdfImportState state){
262 return ! state.getConfig().isDoTaxonNames();
263 }
264
265 }