missing author fixed in tcsrdf
[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.Map;
14 import java.util.Set;
15
16 import org.apache.log4j.Logger;
17 import org.jdom.Attribute;
18 import org.jdom.Element;
19 import org.jdom.Namespace;
20 import org.springframework.stereotype.Component;
21
22 import eu.etaxonomy.cdm.common.XmlHelp;
23 import eu.etaxonomy.cdm.io.common.ICdmIO;
24 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
25 import eu.etaxonomy.cdm.io.common.ImportHelper;
26 import eu.etaxonomy.cdm.io.common.MapWrapper;
27 import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
28 import eu.etaxonomy.cdm.model.agent.Team;
29 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.common.TimePeriod;
32 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
33 import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
34 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
35 import eu.etaxonomy.cdm.model.name.NonViralName;
36 import eu.etaxonomy.cdm.model.name.Rank;
37 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
38 import eu.etaxonomy.cdm.model.reference.Generic;
39 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
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<IImportConfigurator> {
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(IImportConfigurator 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 CdmIoXmlMapperBase[] standardMappers = new CdmIoXmlMapperBase[]{
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 CdmIoXmlMapperBase[] operationalMappers = new CdmIoXmlMapperBase[]{
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 CdmIoXmlMapperBase[] unclearMappers = new CdmIoXmlMapperBase[]{
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 public boolean doInvoke(IImportConfigurator config, Map<String, MapWrapper<? extends CdmBase>> stores){
98
99 MapWrapper<TaxonNameBase> taxonNameMap = (MapWrapper<TaxonNameBase>)stores.get(ICdmIO.TAXONNAME_STORE);
100 MapWrapper<ReferenceBase> referenceMap = (MapWrapper<ReferenceBase>)stores.get(ICdmIO.REFERENCE_STORE);
101 MapWrapper<TeamOrPersonBase> authorMap = (MapWrapper<TeamOrPersonBase>)stores.get(ICdmIO.AUTHOR_STORE);
102
103 String tcsElementName;
104 Namespace tcsNamespace;
105 String value;
106
107 logger.info("start makeTaxonNames ...");
108 TcsRdfImportConfigurator tcsConfig = (TcsRdfImportConfigurator)config;
109 Element root = tcsConfig.getSourceRoot();
110 boolean success =true;
111
112 Namespace rdfNamespace = tcsConfig.getRdfNamespace();
113 Namespace taxonNameNamespace = tcsConfig.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 = tcsConfig.getCommonNamespace();
144 value = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, tcsNamespace);
145 if (value != null){
146 Generic nomRef = Generic.NewInstance(); //TODO
147 nomRef.setTitleCache(value);
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 }
165
166 //Status
167 tcsNamespace = taxonNameNamespace;
168 Element elAnnotation = elTaxonName.getChild("hasAnnotation", tcsNamespace);
169 if (elAnnotation != null){
170 Element elNomenclaturalNote = elAnnotation.getChild("NomenclaturalNote", tcsNamespace);
171 if (elNomenclaturalNote != null){
172 String statusValue = (String)ImportHelper.getXmlInputValue(elNomenclaturalNote, "note", tcsNamespace);
173 String type = XmlHelp.getChildAttributeValue(elNomenclaturalNote, "type", tcsNamespace, "resource", rdfNamespace);
174 String tdwgType = "http://rs.tdwg.org/ontology/voc/TaxonName#PublicationStatus";
175 if (tdwgType.equalsIgnoreCase(type)){
176 try {
177 NomenclaturalStatusType statusType = TcsRdfTransformer.nomStatusString2NomStatus(statusValue);
178 //NomenclaturalStatusType statusType = NomenclaturalStatusType.getNomenclaturalStatusTypeByAbbreviation(statusValue);
179 if (statusType != null){
180 nameBase.addStatus(NomenclaturalStatus.NewInstance(statusType));
181 }
182 } catch (UnknownCdmTypeException e) {
183 if (! statusValue.equals("valid")){
184 logger.warn("Unknown NomenclaturalStatusType: " + statusValue);
185 }
186 }
187 }
188 }
189 }
190
191 if (nameBase instanceof NonViralName){
192 NonViralName<?> nonViralName = (NonViralName<?>)nameBase;
193
194 //AuthorTeams
195 //TODO
196 tcsElementName = "basionymAuthorship";
197 String basionymAuthorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
198 if (basionymAuthorValue != null){
199 INomenclaturalAuthor basionymAuthor = Team.NewInstance();
200 basionymAuthor.setNomenclaturalTitle(basionymAuthorValue);
201 nonViralName.setBasionymAuthorTeam(basionymAuthor);
202 }
203
204 //TODO
205 tcsElementName = "combinationAuthorship";
206 String combinationAuthorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
207 if (combinationAuthorValue != null){
208 INomenclaturalAuthor combinationAuthor = Team.NewInstance();
209 combinationAuthor.setNomenclaturalTitle(combinationAuthorValue);
210 nonViralName.setCombinationAuthorTeam(combinationAuthor);
211 }
212
213 //set the authorshipCache
214 tcsElementName = "authorship";
215 String authorValue = (String)ImportHelper.getXmlInputValue(elTaxonName, tcsElementName, taxonNameNamespace);
216 String cache = nonViralName.getAuthorshipCache();
217 if ( authorValue != null){
218 //compare existing authorship cache with new one and check if it is necessary to
219 //make cache protected //TODO refinement
220 if (cache == null){
221 nonViralName.setAuthorshipCache(authorValue);
222 }else{
223 cache = basionymAuthorValue == null ? cache : cache.replace(basionymAuthorValue, "");
224 cache = combinationAuthorValue == null ? cache : cache.replace(combinationAuthorValue, "");
225 cache = cache.replace("\\(|\\)", "");
226 cache = cache.trim();
227 if (! cache.equals("")){
228 nonViralName.setAuthorshipCache(authorValue);
229 }
230 }
231 }
232 }
233 ImportHelper.setOriginalSource(nameBase, config.getSourceReference(), nameAbout, idNamespace);
234
235 checkAdditionalContents(elTaxonName, standardMappers, operationalMappers, unclearMappers);
236
237 //nameId
238 //TODO
239 //ImportHelper.setOriginalSource(nameBase, tcsConfig.getSourceReference(), nameId);
240 taxonNameMap.put(nameAbout, nameBase);
241
242 } catch (UnknownCdmTypeException e) {
243 //FIXME
244 logger.warn("Name with id " + nameAbout + " has unknown rank " + strRank + " and could not be saved.");
245 success = false;
246 }
247 }
248 logger.info(i + " names handled");
249 getNameService().saveTaxonNameAll(taxonNameMap.objects());
250 // makeNameSpecificData(nameMap);
251 logger.info("end makeTaxonNames ...");
252 return success;
253
254 }
255 /* (non-Javadoc)
256 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
257 */
258 protected boolean isIgnore(IImportConfigurator config){
259 return ! config.isDoTaxonNames();
260 }
261
262 }