new source SQLServer2008 added
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / tcsrdf / TcsRdfTaxonRelationsImport.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.HashSet;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.apache.log4j.Logger;
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.MapWrapper;
24 import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
25 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
26 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
27 import eu.etaxonomy.cdm.model.taxon.Synonym;
28 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
29 import eu.etaxonomy.cdm.model.taxon.Taxon;
30 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
32 import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
33 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
34
35
36 /**
37 * @author a.mueller
38 * @created 29.05.2008
39 * @version 1.0
40 */
41 @Component
42 public class TcsRdfTaxonRelationsImport extends TcsRdfImportBase implements ICdmIO<TcsRdfImportState> {
43 private static final Logger logger = Logger.getLogger(TcsRdfTaxonRelationsImport.class);
44
45 private static int modCount = 30000;
46
47 public TcsRdfTaxonRelationsImport(){
48 super();
49 }
50
51 @Override
52 public boolean doCheck(TcsRdfImportState state){
53 boolean result = true;
54 logger.warn("Checking for TaxonRelations not yet implemented");
55 logger.warn("Creation of homotypic relations is still problematic");
56 //result &= checkArticlesWithoutJournal(bmiConfig);
57 //result &= checkPartOfJournal(bmiConfig);
58
59 return result;
60 }
61
62 @Override
63 public boolean doInvoke(TcsRdfImportState state){
64
65 MapWrapper<TaxonBase> taxonMap = (MapWrapper<TaxonBase>)state.getStore(ICdmIO.TAXON_STORE);
66 MapWrapper<ReferenceBase> referenceMap = (MapWrapper<ReferenceBase>)state.getStore(ICdmIO.REFERENCE_STORE);
67 logger.info("start makeTaxonRelationships ...");
68 boolean success =true;
69
70 String xmlElementName;
71 Namespace elementNamespace;
72
73 Set<TaxonBase> taxonStore = new HashSet<TaxonBase>();
74
75 TcsRdfImportConfigurator config = state.getConfig();
76 Element root = config.getSourceRoot();
77 Namespace taxonConceptNamespace = config.getTcNamespace();
78
79 xmlElementName = "TaxonConcept";
80 elementNamespace = taxonConceptNamespace;
81 List<Element> elTaxonConcepts = root.getChildren(xmlElementName, elementNamespace);
82
83 int i = 0;
84 //for each taxonConcept
85 for (Element elTaxonConcept : elTaxonConcepts){
86 try {
87 if ((i++ % modCount) == 0){ logger.info("Taxa handled: " + (i-1));}
88
89 //TaxonConcept about
90 xmlElementName = "about";
91 elementNamespace = config.getRdfNamespace();
92 String strTaxonAbout = elTaxonConcept.getAttributeValue(xmlElementName, elementNamespace);
93
94 TaxonBase aboutTaxon = taxonMap.get(strTaxonAbout);
95
96 if (aboutTaxon instanceof Taxon){
97 success &= makeHomotypicSynonymRelations((Taxon)aboutTaxon);
98 }
99
100 xmlElementName = "hasRelationship";
101 elementNamespace = taxonConceptNamespace;
102 List<Element> elHasRelationships = elTaxonConcept.getChildren(xmlElementName, elementNamespace);
103
104
105 for (Element elHasRelationship: elHasRelationships){
106 xmlElementName = "relationship";
107 elementNamespace = taxonConceptNamespace;
108 List<Element> elRelationships = elHasRelationship.getChildren(xmlElementName, elementNamespace);
109
110 for (Element elRelationship: elRelationships){
111 success &= makeRelationship(elRelationship, strTaxonAbout, taxonMap, state, taxonStore);
112 }//relationship
113 }//hasRelationships
114 } catch (Exception e) {
115 e.printStackTrace();
116 logger.error("Error. Taxon relationship could not be imported");
117 success = false;
118 }
119 }//elTaxonConcept
120 logger.info("Taxa to save: " + taxonStore.size());
121 getTaxonService().save(taxonStore);
122
123 logger.info("end makeRelTaxa ...");
124 return success;
125
126 }
127
128 private boolean makeRelationship(
129 Element elRelationship,
130 String strTaxonAbout,
131 MapWrapper<TaxonBase> taxonMap,
132 TcsRdfImportState state,
133 Set<TaxonBase> taxonStore){
134 boolean success = true;
135 String xmlElementName;
136 String xmlAttributeName;
137 Namespace elementNamespace;
138 Namespace attributeNamespace;
139 TcsRdfImportConfigurator tcsConfig = state.getConfig();
140 //relationship
141 xmlElementName = "relationshipCategory";
142 elementNamespace = tcsConfig.getTcNamespace();
143 xmlAttributeName = "resource";
144 attributeNamespace = tcsConfig.getRdfNamespace();
145 String strRelCategory = XmlHelp.getChildAttributeValue(elRelationship, xmlElementName, elementNamespace, xmlAttributeName, attributeNamespace);
146 try {
147 RelationshipTermBase relType = TcsRdfTransformer.tcsRelationshipCategory2Relationship(strRelCategory);
148 boolean isReverse = TcsRdfTransformer.isReverseRelationshipCategory(strRelCategory);
149 //toTaxon
150 xmlElementName = "toTaxon";
151 elementNamespace = tcsConfig.getTcNamespace();
152 xmlAttributeName = "resource";
153 attributeNamespace = tcsConfig.getRdfNamespace();
154 String strToTaxon = XmlHelp.getChildAttributeValue(elRelationship, xmlElementName, elementNamespace, xmlAttributeName, attributeNamespace);
155 TaxonBase toTaxon = taxonMap.get(strToTaxon);
156 TaxonBase fromTaxon = taxonMap.get(strTaxonAbout);
157 if (toTaxon != null && fromTaxon != null){
158 //reverse
159 if (isReverse == true ){
160 TaxonBase tmp = toTaxon;
161 toTaxon = fromTaxon;
162 fromTaxon = tmp;
163 }
164
165 //Create relationship
166 if (! (toTaxon instanceof Taxon)){
167 logger.warn("TaxonBase toTaxon is not of Type 'Taxon'. Relationship is not added.");
168 success = false;
169 }else{
170 Taxon taxonTo = (Taxon)toTaxon;
171 ReferenceBase citation = null;
172 String microReference = null;
173 if (relType instanceof SynonymRelationshipType){
174 success &= makeSynRelType((SynonymRelationshipType)relType, taxonTo, fromTaxon, citation, microReference);
175 }else if (relType instanceof TaxonRelationshipType){
176 success &= makeTaxonRelType((TaxonRelationshipType)relType, state, taxonTo, fromTaxon, strTaxonAbout , citation, microReference);
177 }else{
178 logger.warn("Unknown Relationshiptype");
179 success = false;
180 }
181 taxonStore.add(toTaxon);
182 }
183 }else{
184 if (toTaxon == null){
185 logger.warn("toTaxon (" + strToTaxon + ") could not be found in taxonMap. Relationship of type " + strRelCategory + " was not added to CDM");
186 }
187 if (fromTaxon == null){
188 logger.warn("fromTaxon (" + strTaxonAbout + ") could not be found in taxonMap. Relationship was not added to CDM");
189 }
190 success = false;
191 }
192
193 } catch (UnknownCdmTypeException e) {
194 //TODO
195 logger.warn("tc:relationshipCategory " + strRelCategory + " not yet implemented");
196 return false;
197 }
198 return success;
199 }
200
201
202 private boolean makeSynRelType(SynonymRelationshipType synRelType, Taxon taxonTo, TaxonBase fromTaxon, ReferenceBase citation, String microReference){
203 boolean success = true;
204 if (! (fromTaxon instanceof Synonym )){
205 logger.warn("TaxonBase fromTaxon is not of Type 'Synonym'. Relationship is not added.");
206 success = false;
207 }else{
208 Synonym synonym = (Synonym)fromTaxon;
209 TaxonNameBase synName = synonym.getName();
210 TaxonNameBase accName = taxonTo.getName();
211 if (synName != null && accName != null && synName.isHomotypic(accName)
212 && ( synRelType.equals(SynonymRelationshipType.SYNONYM_OF()))){
213 synRelType = SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF();
214 }
215 if (! relationExists(taxonTo, synonym, synRelType)){
216 taxonTo.addSynonym(synonym, synRelType, citation, microReference);
217 }else{
218 //TODO citation, microReference
219 //TODO different synRelTypes -> warning
220 success = false;
221 }
222 }
223 return success;
224 }
225
226 private boolean makeTaxonRelType(TaxonRelationshipType relType, TcsRdfImportState state, Taxon taxonTo, TaxonBase fromTaxon, String strTaxonAbout, ReferenceBase citation, String microReference){
227 boolean success = true;
228 if (! (fromTaxon instanceof Taxon )){
229 logger.warn("TaxonBase fromTaxon " + strTaxonAbout + " is not of Type 'Taxon'. Relationship is not added.");
230 success = false;
231 }else{
232 Taxon taxonFrom = (Taxon)fromTaxon;
233 if (state.getConfig().isUseTaxonomicTree() && relType.equals(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN())){
234 success &= makeTaxonomicallyIncluded(state, taxonTo, taxonFrom, citation, microReference);
235 }else{
236 taxonFrom.addTaxonRelation(taxonTo, relType, citation, microReference);
237 }
238 }
239 return success;
240 }
241
242 private boolean makeTaxonomicallyIncluded(TcsRdfImportState state, Taxon toTaxon, Taxon fromTaxon, ReferenceBase citation, String microCitation){
243 boolean success = true;
244 ReferenceBase sec = toTaxon.getSec();
245 TaxonomicTree tree = state.getTree(sec);
246 if (tree == null){
247 tree = makeTree(state, sec);
248 }
249 success = tree.addParentChild(toTaxon, fromTaxon, citation, microCitation);
250 return success;
251 }
252
253 private boolean relationExists(Taxon taxonTo, Synonym synonym, SynonymRelationshipType synRelType){
254 if (synonym == null){
255 return false;
256 }
257 if (synonym.getRelationType(taxonTo).size() > 0){
258 Set<SynonymRelationshipType> relTypeList = synonym.getRelationType(taxonTo);
259 if (relTypeList.contains(synRelType)){
260 return true;
261 }else{
262 logger.warn("Taxon-Synonym pair has 2 different SynonymRelationships. This is against the rules");
263 return false;
264 }
265 }else{
266 return false;
267 }
268 }
269
270 private boolean makeHomotypicSynonymRelations(Taxon aboutTaxon){
271 if (true)return false;
272 TaxonNameBase aboutName = aboutTaxon.getName();
273 if (aboutName != null){
274 Set<TaxonNameBase> typifiedNames = aboutName.getHomotypicalGroup().getTypifiedNames();
275 for (TaxonNameBase typifiedName : typifiedNames){
276 //TODO check if name is part of this tcs file
277 if (typifiedName.equals(aboutName)){
278 continue;
279 }
280 Set<Synonym> syns = typifiedName.getSynonyms();
281 for(Synonym syn:syns){
282 aboutTaxon.addSynonym(syn, SynonymRelationshipType.HOMOTYPIC_SYNONYM_OF());
283 }
284 }
285
286
287 }
288 return true;
289 }
290
291 /* (non-Javadoc)
292 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
293 */
294 protected boolean isIgnore(TcsRdfImportState state){
295 return ! state.getConfig().isDoRelTaxa();
296 }
297
298 }