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