move berlin model import to io.berlinmodel.in
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / berlinModel / in / BerlinModelCommonNamesImport.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.HashSet;
15 import java.util.Map;
16 import java.util.Set;
17
18 import org.apache.log4j.Logger;
19 import org.springframework.stereotype.Component;
20
21 import eu.etaxonomy.cdm.io.berlinModel.BerlinModelTransformer;
22 import eu.etaxonomy.cdm.io.common.ICdmIO;
23 import eu.etaxonomy.cdm.io.common.IImportConfigurator;
24 import eu.etaxonomy.cdm.io.common.MapWrapper;
25 import eu.etaxonomy.cdm.io.common.Source;
26 import eu.etaxonomy.cdm.model.common.CdmBase;
27 import eu.etaxonomy.cdm.model.description.Distribution;
28 import eu.etaxonomy.cdm.model.description.PresenceAbsenceTermBase;
29 import eu.etaxonomy.cdm.model.description.TaxonDescription;
30 import eu.etaxonomy.cdm.model.location.NamedArea;
31 import eu.etaxonomy.cdm.model.location.TdwgArea;
32 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
33 import eu.etaxonomy.cdm.model.taxon.Taxon;
34 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
35 import eu.etaxonomy.cdm.strategy.exceptions.UnknownCdmTypeException;
36
37 /**
38 * @author a.mueller
39 * @created 20.03.2008
40 * @version 1.0
41 */
42 @Component
43 public class BerlinModelCommonNamesImport extends BerlinModelImportBase {
44 private static final Logger logger = Logger.getLogger(BerlinModelCommonNamesImport.class);
45
46 private static int modCount = 10000;
47
48 public BerlinModelCommonNamesImport(){
49 super();
50 }
51
52 /* (non-Javadoc)
53 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
54 */
55 @Override
56 protected boolean doCheck(IImportConfigurator config){
57 boolean result = true;
58 logger.warn("Checking for Occurrence not yet implemented");
59 //result &= checkArticlesWithoutJournal(bmiConfig);
60 //result &= checkPartOfJournal(bmiConfig);
61
62 return result;
63 }
64
65 /* (non-Javadoc)
66 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IImportConfigurator, eu.etaxonomy.cdm.api.application.CdmApplicationController, java.util.Map)
67 */
68 @Override
69 protected boolean doInvoke(IImportConfigurator config,
70 Map<String, MapWrapper<? extends CdmBase>> stores){
71
72 MapWrapper<TaxonBase> taxonMap = (MapWrapper<TaxonBase>)stores.get(ICdmIO.TAXON_STORE);
73 MapWrapper<ReferenceBase> referenceMap = (MapWrapper<ReferenceBase>)stores.get(ICdmIO.REFERENCE_STORE);
74 BerlinModelImportConfigurator bmiConfig = (BerlinModelImportConfigurator)config;
75
76 if (true){
77 return false;
78 }
79
80 Set<TaxonBase> taxonStore = new HashSet<TaxonBase>();
81 Source source = bmiConfig.getSource();
82
83 String dbAttrName;
84 String cdmAttrName;
85
86 logger.info("start make occurrences ...");
87
88 boolean delete = bmiConfig.isDeleteAll();
89
90 try {
91 //get data from database
92 String strQuery =
93 " SELECT PTaxon.RIdentifier, emOccurrence.OccurrenceId, emOccurSumCat.emOccurSumCatId, emOccurSumCat.Short, emOccurSumCat.Description, " +
94 " emOccurSumCat.OutputCode, emArea.AreaId, emArea.EMCode, emArea.ISOCode, emArea.TDWGCode, emArea.Unit, " +
95 " emArea.Status, emArea.OutputOrder, emArea.eur, emArea.EuroMedArea " +
96 " FROM emOccurrence INNER JOIN " +
97 " emArea ON emOccurrence.AreaFk = emArea.AreaId INNER JOIN " +
98 " PTaxon ON emOccurrence.PTNameFk = PTaxon.PTNameFk AND emOccurrence.PTRefFk = PTaxon.PTRefFk LEFT OUTER JOIN " +
99 " emOccurSumCat ON emOccurrence.SummaryStatus = emOccurSumCat.emOccurSumCatId LEFT OUTER JOIN " +
100 " emOccurrenceSource ON emOccurrence.OccurrenceId = emOccurrenceSource.OccurrenceFk " +
101 " WHERE (1=1)";
102 ResultSet rs = source.getResultSet(strQuery) ;
103
104 int i = 0;
105 //for each reference
106 while (rs.next()){
107
108 if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("Facts handled: " + (i-1));}
109
110 int occurrenceId = rs.getInt("OccurrenceId");
111 int taxonId = rs.getInt("RIdentifier");
112 String tdwgCode = rs.getString("TDWGCode");
113 Integer emStatusId = (Integer)rs.getObject("emOccurSumCatId");
114
115
116 TaxonBase taxonBase = taxonMap.get(taxonId);
117 if (taxonBase != null){
118 try {
119
120 PresenceAbsenceTermBase<?> status = null;
121 if (emStatusId != null){
122 status = BerlinModelTransformer.occStatus2PresenceAbsence(emStatusId);
123 }
124
125 NamedArea tdwgArea = TdwgArea.getAreaByTdwgLabel(tdwgCode);
126
127 Taxon taxon;
128 if ( taxonBase instanceof Taxon ) {
129 taxon = (Taxon) taxonBase;
130 }else{
131 logger.warn("TaxonBase for Occurrence " + occurrenceId + " was not of type Taxon but: " + taxonBase.getClass().getSimpleName());
132 continue;
133 }
134
135 if (tdwgArea != null){
136 Distribution distribution = Distribution.NewInstance(tdwgArea, status);
137 //TODO only one description per taxon (for all EM distributions)
138 TaxonDescription taxonDescription = TaxonDescription.NewInstance();
139
140 taxon.addDescription(taxonDescription);
141 taxonStore.add(taxon);
142 }
143
144 } catch (UnknownCdmTypeException e) {
145 // TODO Auto-generated catch block
146 e.printStackTrace();
147 }
148
149 // TODO
150 // sources
151 // references
152
153
154
155 // //commonNames -> TODO move to separate IO
156 // String commonNameString;
157 // if (taxon.getName() != null){
158 // commonNameString = "Common " + taxon.getName().getTitleCache();
159 // }else{
160 // commonNameString = "Common (null)";
161 // }
162 // Language language = bmiConfig.getFactLanguage();
163 // language = null;
164 // CommonTaxonName commonName = CommonTaxonName.NewInstance(commonNameString, language);
165 // taxonDescription.addElement(commonName);
166
167
168
169 }else{
170 //TODO
171 logger.warn("Taxon for Fact " + occurrenceId + " does not exist in store");
172 }
173 //put
174 }
175 logger.info("Taxa to save: " + taxonStore.size());
176 getTaxonService().saveTaxonAll(taxonStore);
177
178 logger.info("end make occurrences ...");
179 return true;
180 } catch (SQLException e) {
181 logger.error("SQLException:" + e);
182 return false;
183 }
184
185 }
186
187 /* (non-Javadoc)
188 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
189 */
190 protected boolean isIgnore(IImportConfigurator config){
191 return ! config.isDoOccurrence();
192 }
193
194 }