6ca2342ad7f2372d3b956c9f3e1115662676be04
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / berlinModel / BerlinModelTaxonImport.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;
11
12 import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.T_STATUS_ACCEPTED;
13 import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.T_STATUS_PARTIAL_SYN;
14 import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.T_STATUS_PRO_PARTE_SYN;
15 import static eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer.T_STATUS_SYNONYM;
16
17 import java.sql.ResultSet;
18 import java.sql.SQLException;
19 import java.util.Map;
20 import java.util.UUID;
21
22 import org.apache.log4j.Logger;
23 import org.springframework.stereotype.Component;
24
25 import eu.etaxonomy.cdm.io.common.ICdmIO;
26 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
27 import eu.etaxonomy.cdm.io.common.ImportHelper;
28 import eu.etaxonomy.cdm.io.common.MapWrapper;
29 import eu.etaxonomy.cdm.io.common.Source;
30 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
31 import eu.etaxonomy.cdm.model.common.CdmBase;
32 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
33 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
34 import eu.etaxonomy.cdm.model.taxon.Synonym;
35 import eu.etaxonomy.cdm.model.taxon.Taxon;
36 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
37
38
39 /**
40 * @author a.mueller
41 * @created 20.03.2008
42 * @version 1.0
43 */
44 @Component
45 public class BerlinModelTaxonImport extends BerlinModelImportBase {
46 private static final Logger logger = Logger.getLogger(BerlinModelTaxonImport.class);
47
48 private int modCount = 10000;
49
50 public BerlinModelTaxonImport(){
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 BerlinModelImportConfigurator bmiConfig = (BerlinModelImportConfigurator)config;
61 logger.warn("Checking for Taxa not yet fully implemented");
62 result &= checkTaxonStatus(bmiConfig);
63 result &= checkInactivated(bmiConfig);
64
65 return result;
66 }
67
68 private boolean checkTaxonStatus(BerlinModelImportConfigurator bmiConfig){
69 try {
70 boolean result = true;
71 Source source = bmiConfig.getSource();
72 String strSQL = " SELECT RelPTaxon.RelQualifierFk, RelPTaxon.relPTaxonId, PTaxon.PTNameFk, PTaxon.PTRefFk, PTaxon_1.PTNameFk AS Expr1, PTaxon.RIdentifier, PTaxon_1.RIdentifier AS Expr3, Name.FullNameCache " +
73 " FROM RelPTaxon " +
74 " INNER JOIN PTaxon ON RelPTaxon.PTNameFk1 = PTaxon.PTNameFk AND RelPTaxon.PTRefFk1 = PTaxon.PTRefFk " +
75 " INNER JOIN PTaxon AS PTaxon_1 ON RelPTaxon.PTNameFk2 = PTaxon_1.PTNameFk AND RelPTaxon.PTRefFk2 = PTaxon_1.PTRefFk " +
76 " INNER JOIN Name ON PTaxon.PTNameFk = Name.NameId " +
77 " WHERE (dbo.PTaxon.StatusFk = 1) AND ((RelPTaxon.RelQualifierFk = 7) OR (RelPTaxon.RelQualifierFk = 6) OR (RelPTaxon.RelQualifierFk = 2)) ";
78 ResultSet rs = source.getResultSet(strSQL);
79 boolean firstRow = true;
80 int i = 0;
81 while (rs.next()){
82 i++;
83 if (firstRow){
84 System.out.println("========================================================");
85 logger.warn("There are taxa that have a 'is synonym of' - relationship but having taxon status 'accepted'!");
86 System.out.println("========================================================");
87 }
88 int rIdentifier = rs.getInt("RIdentifier");
89 int nameFk = rs.getInt("PTNameFk");
90 int refFk = rs.getInt("PTRefFk");
91 int relPTaxonId = rs.getInt("relPTaxonId");
92 String taxonName = rs.getString("FullNameCache");
93
94 System.out.println("RIdentifier:" + rIdentifier + "\n name: " + nameFk +
95 "\n taxonName: " + taxonName + "\n refId: " + refFk + "\n RelPTaxonId: " + relPTaxonId );
96 result = firstRow = false;
97 }
98 if (i > 0){
99 System.out.println(" ");
100 }
101
102 return result;
103 } catch (SQLException e) {
104 e.printStackTrace();
105 return false;
106 }
107 }
108
109 private boolean checkInactivated(BerlinModelImportConfigurator bmiConfig){
110 try {
111 boolean result = true;
112 Source source = bmiConfig.getSource();
113 String strSQL = " SELECT * " +
114 " FROM PTaxon " +
115 " INNER JOIN Name ON PTaxon.PTNameFk = Name.NameId " +
116 " WHERE (PTaxon.DoubtfulFlag = 'i') ";
117 ResultSet rs = source.getResultSet(strSQL);
118 boolean firstRow = true;
119 int i = 0;
120 while (rs.next()){
121 i++;
122 if (firstRow){
123 System.out.println("========================================================");
124 logger.warn("There are taxa that have a doubtful flag 'i'(inactivated). Inactivated is not supported by CDM!");
125 System.out.println("========================================================");
126 }
127 int rIdentifier = rs.getInt("RIdentifier");
128 int nameFk = rs.getInt("PTNameFk");
129 int refFk = rs.getInt("PTRefFk");
130 String taxonName = rs.getString("FullNameCache");
131
132 System.out.println("RIdentifier:" + rIdentifier + "\n nameId: " + nameFk +
133 "\n taxonName: " + taxonName + "\n refId: " + refFk );
134 result = firstRow = false;
135 }
136 if (i > 0){
137 System.out.println(" ");
138 }
139
140 return result;
141 } catch (SQLException e) {
142 e.printStackTrace();
143 return false;
144 }
145 }
146
147 /* (non-Javadoc)
148 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
149 */
150 @Override
151 protected boolean doInvoke(IImportConfigurator config,
152 Map<String, MapWrapper<? extends CdmBase>> stores){
153
154 //make not needed maps empty
155 String teamStore = ICdmIO.TEAM_STORE;
156 MapWrapper<? extends CdmBase> store = stores.get(teamStore);
157 MapWrapper<TeamOrPersonBase> teamMap = (MapWrapper<TeamOrPersonBase>)store;
158 teamMap.makeEmpty();
159
160
161 MapWrapper<TaxonNameBase<?,?>> taxonNameMap = (MapWrapper<TaxonNameBase<?,?>>)stores.get(ICdmIO.TAXONNAME_STORE);
162 MapWrapper<ReferenceBase> referenceMap = (MapWrapper<ReferenceBase>)stores.get(ICdmIO.REFERENCE_STORE);
163 MapWrapper<ReferenceBase> nomRefMap = (MapWrapper<ReferenceBase>)stores.get(ICdmIO.NOMREF_STORE);
164 MapWrapper<TaxonBase> taxonMap = (MapWrapper<TaxonBase>)stores.get(ICdmIO.TAXON_STORE);
165
166 BerlinModelImportConfigurator bmiConfig = (BerlinModelImportConfigurator)config;
167 Source source = bmiConfig.getSource();
168
169 logger.info("start makeTaxa ...");
170
171 String namespace = "PTaxon";
172
173 try {
174 //get data from database
175 String strQuery =
176 " SELECT * " +
177 " FROM PTaxon " +
178 " WHERE (1=1)";
179
180 ResultSet rs = source.getResultSet(strQuery) ;
181
182 int i = 0;
183 //for each reference
184 while (rs.next()){
185
186 if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("PTaxa handled: " + (i-1));}
187
188 //create TaxonName element
189 int taxonId = rs.getInt("RIdentifier");
190 int statusFk = rs.getInt("statusFk");
191
192 int nameFk = rs.getInt("PTNameFk");
193 int refFk = rs.getInt("PTRefFk");
194 String doubtful = rs.getString("DoubtfulFlag");
195 String uuid = null;
196 if (resultSetHasColumn(rs,"UUID")){
197 uuid = rs.getString("UUID");
198 }
199
200 TaxonNameBase<?,?> taxonName = null;
201 if (taxonNameMap != null){
202 taxonName = taxonNameMap.get(nameFk);
203 }
204
205 ReferenceBase reference = null;
206 if (referenceMap != null){
207 reference = referenceMap.get(refFk);
208 if (reference == null){
209 reference = nomRefMap.get(refFk);
210 }
211 }
212
213 if(! config.isIgnoreNull()){
214 if (taxonName == null ){
215 logger.warn("TaxonName belonging to taxon (RIdentifier = " + taxonId + ") could not be found in store. Taxon will not be transported");
216 continue; //next taxon
217 }else if (reference == null ){
218 logger.warn("Reference belonging to taxon could not be found in store. Taxon will not be imported");
219 continue; //next taxon
220 }
221 }
222 TaxonBase taxonBase;
223 Synonym synonym;
224 Taxon taxon;
225 try {
226 logger.debug(statusFk);
227 if (statusFk == T_STATUS_ACCEPTED){
228 taxon = Taxon.NewInstance(taxonName, reference);
229 taxonBase = taxon;
230 }else if (statusFk == T_STATUS_SYNONYM || statusFk == T_STATUS_PRO_PARTE_SYN || statusFk == T_STATUS_PARTIAL_SYN){
231 synonym = Synonym.NewInstance(taxonName, reference);
232 taxonBase = synonym;
233 if (statusFk == T_STATUS_PRO_PARTE_SYN){
234 bmiConfig.addProParteSynonym(synonym);
235 }
236 if (statusFk == T_STATUS_PARTIAL_SYN){
237 bmiConfig.addPartialSynonym(synonym);
238 }
239 }else{
240 logger.warn("TaxonStatus " + statusFk + " not yet implemented. Taxon (RIdentifier = " + taxonId + ") left out.");
241 continue;
242 }
243 if (uuid != null){
244 taxonBase.setUuid(UUID.fromString(uuid));
245 }
246
247 //TODO
248 // dbAttrName = "Detail";
249 // cdmAttrName = "Micro";
250 // ImportHelper.addStringValue(rs, taxonBase, dbAttrName, cdmAttrName);
251
252 if (doubtful.equals("a")){
253 taxonBase.setDoubtful(false);
254 }else if(doubtful.equals("d")){
255 taxonBase.setDoubtful(true);
256 }else if(doubtful.equals("i")){
257 //TODO
258 logger.warn("Doubtful = i (inactivated) not yet implemented. Doubtful set to false");
259 }
260
261 //nameId
262 ImportHelper.setOriginalSource(taxonBase, bmiConfig.getSourceReference(), taxonId, namespace);
263
264
265 //TODO
266 //
267 //Created
268 //Note
269 //ALL
270
271 taxonMap.put(taxonId, taxonBase);
272 } catch (Exception e) {
273 logger.warn("An exception occurred when creating taxon with id " + taxonId + ". Taxon could not be saved.");
274 }
275 }
276 //invokeRelations(source, cdmApp, deleteAll, taxonMap, referenceMap);
277 logger.info("saving taxa ...");
278 getTaxonService().saveTaxonAll(taxonMap.objects());
279
280 logger.info("end makeTaxa ...");
281
282 return true;
283 } catch (SQLException e) {
284 logger.error("SQLException:" + e);
285 return false;
286 }
287
288 }
289
290
291
292 /* (non-Javadoc)
293 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
294 */
295 protected boolean isIgnore(IImportConfigurator config){
296 return ! config.isDoTaxa();
297 }
298
299 }