Berlin Model import ->in and import state class
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / berlinModel / in / BerlinModelFactsImport.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.berlinModel.in;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.util.Collection;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.apache.log4j.Logger;
20 import org.springframework.stereotype.Component;
21
22 import eu.etaxonomy.cdm.common.CdmUtils;
23 import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
24 import eu.etaxonomy.cdm.io.common.ICdmIO;
25 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
26 import eu.etaxonomy.cdm.io.common.MapWrapper;
27 import eu.etaxonomy.cdm.io.common.Source;
28 import eu.etaxonomy.cdm.model.common.CdmBase;
29 import eu.etaxonomy.cdm.model.common.Language;
30 import eu.etaxonomy.cdm.model.common.TermVocabulary;
31 import eu.etaxonomy.cdm.model.description.Feature;
32 import eu.etaxonomy.cdm.model.description.TaxonDescription;
33 import eu.etaxonomy.cdm.model.description.TextData;
34 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
35 import eu.etaxonomy.cdm.model.taxon.Taxon;
36 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
37 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
38
39 /**
40 * @author a.mueller
41 * @created 20.03.2008
42 * @version 1.0
43 */
44 @Component
45 public class BerlinModelFactsImport extends BerlinModelImportBase {
46 private static final Logger logger = Logger.getLogger(BerlinModelFactsImport.class);
47
48 private int modCount = 10000;
49
50 public BerlinModelFactsImport(){
51 super();
52 }
53
54 /* (non-Javadoc)
55 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
56 */
57 @Override
58 protected boolean doCheck(IImportConfigurator config){
59 boolean result = true;
60 logger.warn("Checking for Facts not yet implemented");
61 //result &= checkArticlesWithoutJournal(bmiConfig);
62 //result &= checkPartOfJournal(bmiConfig);
63
64 return result;
65 }
66
67 private TermVocabulary<Feature> getFeatureVocabulary(){
68 try {
69 //TODO work around until service method works
70 TermVocabulary<Feature> featureVocabulary = BerlinModelTransformer.factCategory2Feature(1).getVocabulary();
71 //TermVocabulary<Feature> vocabulary = getTermService().getVocabulary(vocabularyUuid);
72 return featureVocabulary;
73 } catch (UnknownCdmTypeException e) {
74 logger.error("Feature vocabulary not available. New vocabulary created");
75 return new TermVocabulary<Feature>() ;
76 }
77 }
78
79 private MapWrapper<Feature> invokeFactCategories(BerlinModelImportConfigurator bmiConfig){
80
81 // Map<Integer, Feature> featureMap = new HashMap<Integer, Feature>();
82 MapWrapper<Feature> result = bmiConfig.getFeatureMap();
83
84 Source source = bmiConfig.getSource();
85
86
87 try {
88 //get data from database
89 String strQuery =
90 " SELECT FactCategory.* " +
91 " FROM FactCategory "+
92 " WHERE (1=1)";
93 ResultSet rs = source.getResultSet(strQuery) ;
94
95
96 TermVocabulary<Feature> featureVocabulary = getFeatureVocabulary();
97 int i = 0;
98 //for each reference
99 while (rs.next()){
100
101 if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("FactCategories handled: " + (i-1));}
102
103 int factCategoryId = rs.getInt("factCategoryId");
104 String factCategory = rs.getString("factCategory");
105
106
107 Feature feature;
108 try {
109 feature = BerlinModelTransformer.factCategory2Feature(factCategoryId);
110 } catch (UnknownCdmTypeException e) {
111 logger.warn("New Feature (FactCategoryId: " + factCategoryId + ")");
112 feature = Feature.NewInstance(factCategory, factCategory, null);
113 feature.setVocabulary(featureVocabulary);
114 feature.setSupportsTextData(true);
115 //TODO
116 // MaxFactNumber int Checked
117 // ExtensionTableName varchar(100) Checked
118 // Description nvarchar(1000) Checked
119 // locExtensionFormName nvarchar(80) Checked
120 // RankRestrictionFk int Checked
121 }
122
123 // featureMap.put(factCategoryId, feature);
124 result.put(factCategoryId, feature);
125
126 }
127 Collection<Feature> col = result.getAllValues();
128 getTermService().saveTermsAll(col);
129 return result;
130 } catch (SQLException e) {
131 logger.error("SQLException:" + e);
132 return null;
133 }
134
135 }
136
137
138 /* (non-Javadoc)
139 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
140 */
141 @Override
142 protected boolean doInvoke(BerlinModelImportState<BerlinModelImportConfigurator> state){
143 boolean result = true;
144
145 MapWrapper<TaxonBase> taxonMap = (MapWrapper<TaxonBase>)state.getStore(ICdmIO.TAXON_STORE);
146 MapWrapper<ReferenceBase> referenceMap = (MapWrapper<ReferenceBase>)state.getStore(ICdmIO.REFERENCE_STORE);
147 MapWrapper<ReferenceBase> nomRefMap = (MapWrapper<ReferenceBase>)state.getStore(ICdmIO.NOMREF_STORE);
148
149 Set<TaxonBase> taxonStore = new HashSet<TaxonBase>();
150
151 BerlinModelImportConfigurator config = state.getConfig();
152 Source source = config.getSource();
153
154 logger.info("start makeFacts ...");
155
156 MapWrapper<Feature> featureMap = invokeFactCategories(config);
157
158 //for testing only
159 //TaxonBase taxonBase = Taxon.NewInstance(BotanicalName.NewInstance(null), null);
160
161
162 try {
163 //get data from database
164 String strQuery =
165 " SELECT Fact.*, PTaxon.RIdentifier as taxonId, RefDetail.Details " +
166 " FROM Fact " +
167 " INNER JOIN PTaxon ON Fact.PTNameFk = PTaxon.PTNameFk AND Fact.PTRefFk = PTaxon.PTRefFk " +
168 " LEFT OUTER JOIN RefDetail ON Fact.FactRefDetailFk = RefDetail.RefDetailId AND Fact.FactRefFk = RefDetail.RefFk " +
169 " WHERE (1=1)";
170 ResultSet rs = source.getResultSet(strQuery) ;
171
172 int i = 0;
173 //for each reference
174 while (rs.next()){
175 try{
176 if ((i++ % modCount) == 0){ logger.info("Facts handled: " + (i-1));}
177
178 //Map<String, Object> valueMap = getValueMap(rs);
179
180 int factId = rs.getInt("factId");
181
182
183 Object taxonIdObj = rs.getObject("taxonId");
184 int taxonId = rs.getInt("taxonId");
185 Object factRefFkObj = rs.getObject("factRefFk");
186 int factRefFk = rs.getInt("factRefFk");
187 Object categoryFkObj = rs.getObject("factCategoryFk");
188 Integer categoryFk = rs.getInt("factCategoryFk");
189
190 String details = rs.getString("Details");
191 // int ptDesignationRefFk = rs.getInt("PTDesignationRefFk");
192 // String ptDesignation details = rs.getInt("PTDesignationRefDetailFk");
193 String fact = CdmUtils.Nz(rs.getString("Fact"));
194 String notes = CdmUtils.Nz(rs.getString("notes"));
195
196
197
198 TaxonBase taxonBase;
199 if (taxonIdObj != null){
200 taxonBase = taxonMap.get(taxonId);
201 }else{
202 taxonBase = null;
203 }
204 Feature feature;
205 if (categoryFkObj != null){
206 feature = featureMap.get(categoryFk);
207 }else{
208 feature = null;
209 }
210
211 if (taxonBase != null){
212 Taxon taxon;
213 if ( taxonBase instanceof Taxon ) {
214 taxon = (Taxon) taxonBase;
215 }else{
216 logger.warn("TaxonBase " + (taxonIdObj==null?"(null)":taxonIdObj) + " for Fact " + factId + " was not of type Taxon but: " + taxonBase.getClass().getSimpleName());
217 continue;
218 }
219
220 TaxonDescription taxonDescription;
221 Set<TaxonDescription> descriptionSet= taxon.getDescriptions();
222 if (descriptionSet.size() > 0) {
223 taxonDescription = descriptionSet.iterator().next();
224 }else{
225 taxonDescription = TaxonDescription.NewInstance();
226 taxon.addDescription(taxonDescription);
227 }
228
229 //textData
230 TextData textData = TextData.NewInstance();
231 //TODO textData.putText(fact, bmiConfig.getFactLanguage()); //doesn't work because bmiConfig.getFactLanguage() is not not a persistent Language Object
232 //throws in thread "main" org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: eu.etaxonomy.cdm.model.common.Language; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: eu.etaxonomy.cdm.model.common.Language
233
234 //for diptera database
235 if (categoryFk == 99 && notes.contains("<OriginalName>")){
236 notes = notes.replaceAll("<OriginalName>", "");
237 notes = notes.replaceAll("</OriginalName>", "");
238 fact = notes + ": " + fact ;
239 }
240 textData.putText(fact, Language.DEFAULT());
241 textData.setType(feature);
242
243 //
244 ReferenceBase citation;
245 if (factRefFkObj != null){
246 citation = referenceMap.get(factRefFk);
247 if (citation == null){
248 citation = nomRefMap.get(factRefFk);
249 }
250 if (citation == null && (factRefFk != 0)){
251 logger.warn("Citation not found in referenceMap: " + factRefFk);
252 }
253 }else{
254 citation = null;
255 }
256
257
258 textData.setCitation(citation);
259 textData.setCitationMicroReference(details);
260 taxonDescription.addElement(textData);
261
262
263 // if (categoryFkObj == FACT_DESCRIPTION){
264 // //;
265 // }else if (categoryFkObj == FACT_OBSERVATION){
266 // //;
267 // }else if (categoryFkObj == FACT_DISTRIBUTION_EM){
268 // //
269 // }else {
270 // //TODO
271 // //logger.warn("FactCategory " + categoryFk + " not yet implemented");
272 // }
273
274 //TODO
275 //References
276 //factId,
277
278 //etc.
279 doCreatedUpdatedNotes(config, textData, rs, "Fact");
280
281
282 taxonStore.add(taxon);
283 }else{
284 //TODO
285 logger.warn("Taxon for Fact " + factId + " does not exist in store");
286 }
287 } catch (RuntimeException re){
288 logger.error("A runtime exception occurred during the facts import");
289 result = false;
290 throw re;
291 }
292 //put
293 }
294 logger.info("Facts handled: " + (i-1));
295 logger.info("Taxa to save: " + taxonStore.size());
296 getTaxonService().saveTaxonAll(taxonStore);
297
298 logger.info("end makeFacts ...");
299 return result;
300 } catch (SQLException e) {
301 logger.error("SQLException:" + e);
302 return false;
303 }
304
305 }
306
307 /* (non-Javadoc)
308 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
309 */
310 protected boolean isIgnore(IImportConfigurator config){
311 return ! config.isDoFacts();
312 }
313
314 }