1
2
3
4
5
6
7
8
9
10 package eu.etaxonomy.cdm.io.pesi.out;
11
12 import java.sql.Connection;
13 import java.sql.PreparedStatement;
14 import java.sql.ResultSet;
15 import java.sql.SQLException;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20 import org.apache.log4j.Logger;
21 import org.springframework.stereotype.Component;
22 import org.springframework.transaction.TransactionStatus;
23
24 import eu.etaxonomy.cdm.io.berlinModel.out.mapper.MethodMapper;
25 import eu.etaxonomy.cdm.io.common.Source;
26 import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.common.DescriptionElementSource;
29 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
30 import eu.etaxonomy.cdm.model.description.TaxonDescription;
31 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
32 import eu.etaxonomy.cdm.model.taxon.Taxon;
33 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
34
35
36
37
38
39
40
41
42 @Component
43 @SuppressWarnings("unchecked")
44 public class PesiOccurrenceSourceExport extends PesiExportBase {
45 private static final Logger logger = Logger.getLogger(PesiOccurrenceSourceExport.class);
46 private static final Class<? extends CdmBase> standardMethodParameter = AnnotatableEntity.class;
47
48 private static int modCount = 1000;
49 private static final String dbTableName = "OccurrenceSource";
50 private static final String pluralString = "OccurrenceSources";
51 private static final String parentPluralString = "Taxa";
52 private static Taxon taxon = null;
53
54 public PesiOccurrenceSourceExport() {
55 super();
56 }
57
58
59
60
61 @Override
62 public Class<? extends CdmBase> getStandardMethodParameter() {
63 return standardMethodParameter;
64 }
65
66
67
68
69 @Override
70 protected boolean doCheck(PesiExportState state) {
71 boolean result = true;
72 return result;
73 }
74
75
76
77
78 @Override
79 protected boolean doInvoke(PesiExportState state) {
80 try {
81 logger.error("*** Started Making " + pluralString + " ...");
82
83 String occurrenceSql = "Insert into OccurrenceSource (OccurrenceFk, SourceFk, SourceNameCache, OldTaxonName) " +
84 "values (?, ?, ?, ?)";
85 Connection con = state.getConfig().getDestination().getConnection();
86 PreparedStatement stmt = con.prepareStatement(occurrenceSql);
87
88
89 int limit = state.getConfig().getLimitSave();
90
91
92 boolean success = true;
93
94
95 doDelete(state);
96
97
98 PesiExportMapping mapping = getMapping();
99
100
101 mapping.initialize(state);
102
103
104 int count = 0;
105 int taxonCount = 0;
106 int pastCount = 0;
107 TransactionStatus txStatus = null;
108 List<TaxonBase> list = null;
109
110
111 txStatus = startTransaction(true);
112 logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
113 while ((list = getTaxonService().list(null, limit, taxonCount, null, null)).size() > 0) {
114
115 logger.error("Fetched " + list.size() + " " + parentPluralString + ".");
116 taxonCount += list.size();
117 for (TaxonBase taxonBase : list) {
118 if (taxonBase.isInstanceOf(Taxon.class)) {
119
120
121 taxon = CdmBase.deproxy(taxonBase, Taxon.class);
122
123
124 Set<TaxonDescription> taxonDescriptions = taxon.getDescriptions();
125
126
127 for (TaxonDescription taxonDescription : taxonDescriptions) {
128 Set<DescriptionElementBase> descriptionElements = taxonDescription.getElements();
129 for (DescriptionElementBase descriptionElement : descriptionElements) {
130 Set<DescriptionElementSource> elementSources = descriptionElement.getSources();
131
132
133 if (elementSources.size() > 0) {
134 for (DescriptionElementSource elementSource : elementSources) {
135 ReferenceBase reference = elementSource.getCitation();
136
137
138 if (reference != null) {
139
140
141 Integer sourceFk = getSourceFk(reference, state);
142
143 if (sourceFk != null && ! state.alreadyProcessedSource(sourceFk)) {
144 doCount(count++, modCount, pluralString);
145
146
147 state.addToProcessedSources(sourceFk);
148
149
150 Set<Integer> occurrenceIds = getOccurrenceIds(sourceFk, state);
151
152
153 insertColumns(stmt, reference, sourceFk, occurrenceIds);
154 }
155
156 }
157 }
158 }
159
160 }
161 }
162 }
163 }
164
165 state.clearAlreadyProcessedSources();
166
167
168 commitTransaction(txStatus);
169 logger.error("Committed transaction.");
170 logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
171 pastCount = count;
172
173
174 txStatus = startTransaction(true);
175 logger.error("Started new transaction. Fetching some " + parentPluralString + " first (max: " + limit + ") ...");
176 }
177 if (list.size() == 0) {
178 logger.error("No " + pluralString + " left to fetch.");
179 }
180
181 commitTransaction(txStatus);
182 logger.error("Committed transaction.");
183
184 logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
185
186 return success;
187 } catch (SQLException e) {
188 e.printStackTrace();
189 logger.error(e.getMessage());
190 return false;
191 }
192 }
193
194
195
196
197
198
199
200
201 private void insertColumns(PreparedStatement stmt, ReferenceBase reference,
202 Integer sourceFk, Set<Integer> occurrenceIds) {
203 for (Integer occurrenceId : occurrenceIds) {
204 try {
205 stmt.setInt(1, occurrenceId);
206 stmt.setInt(2, sourceFk);
207 stmt.setString(3, getSourceNameCache(reference));
208 stmt.setString(4, null);
209 stmt.execute();
210 } catch (SQLException e) {
211 logger.error("SQLException during getOccurrenceId invoke.");
212 e.printStackTrace();
213 }
214 }
215 }
216
217
218
219
220
221
222 private static Set<Integer> getOccurrenceIds(Integer sourceFk, PesiExportState state) {
223 String occurrenceSql = "Select OccurrenceId From Occurrence where SourceFk = ?";
224 Connection con = state.getConfig().getDestination().getConnection();
225 PreparedStatement stmt = null;
226 Set<Integer> occurrenceIds = new HashSet();
227
228 try {
229 stmt = con.prepareStatement(occurrenceSql);
230 stmt.setInt(1, sourceFk);
231 ResultSet resultSet = stmt.executeQuery();
232 while (resultSet.next()) {
233 occurrenceIds.add(resultSet.getInt(1));
234 }
235 } catch (SQLException e) {
236 logger.error("SQLException during getOccurrenceId invoke. (2)");
237 e.printStackTrace();
238 }
239
240 return occurrenceIds;
241 }
242
243
244
245
246
247
248 protected boolean doDelete(PesiExportState state) {
249 PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
250
251 String sql;
252 Source destination = pesiConfig.getDestination();
253
254
255 sql = "DELETE FROM " + dbTableName;
256 destination.setQuery(sql);
257 destination.update(sql);
258 return true;
259 }
260
261
262
263
264 @Override
265 protected boolean isIgnore(PesiExportState state) {
266
267
268 return false;
269 }
270
271
272
273
274
275
276
277
278 @SuppressWarnings("unused")
279 private static Integer getOccurrenceFk(AnnotatableEntity entity, PesiExportState state) {
280 Integer result = null;
281 return result;
282 }
283
284
285
286
287
288
289
290
291 private static Integer getSourceFk(AnnotatableEntity entity, PesiExportState state) {
292 Integer result = null;
293 if (state != null && entity != null && entity.isInstanceOf(ReferenceBase.class)) {
294 ReferenceBase reference = CdmBase.deproxy(entity, ReferenceBase.class);
295 result = state.getDbId(reference);
296 }
297 return result;
298 }
299
300
301
302
303
304
305
306 private static String getSourceNameCache(AnnotatableEntity entity) {
307 String result = null;
308 if (entity != null && entity.isInstanceOf(ReferenceBase.class)) {
309 ReferenceBase reference = CdmBase.deproxy(entity, ReferenceBase.class);
310 result = reference.getTitle();
311 }
312 return result;
313 }
314
315
316
317
318
319
320
321 @SuppressWarnings("unused")
322 private static String getOldTaxonName(AnnotatableEntity entity) {
323
324 return null;
325 }
326
327
328
329
330
331 private PesiExportMapping getMapping() {
332 PesiExportMapping mapping = new PesiExportMapping(dbTableName);
333
334
335 mapping.addMapper(MethodMapper.NewInstance("OccurrenceFk", this.getClass(), "getOccurrenceFk", standardMethodParameter, PesiExportState.class));
336 mapping.addMapper(MethodMapper.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter, PesiExportState.class));
337 mapping.addMapper(MethodMapper.NewInstance("SourceNameCache", this));
338 mapping.addMapper(MethodMapper.NewInstance("OldTaxonName", this));
339
340 return mapping;
341 }
342
343 }