1
2
3
4
5
6
7
8
9
10 package eu.etaxonomy.cdm.io.pesi.out;
11
12 import java.sql.SQLException;
13 import java.util.List;
14
15 import org.apache.log4j.Logger;
16 import org.springframework.stereotype.Component;
17 import org.springframework.transaction.TransactionStatus;
18
19 import eu.etaxonomy.cdm.io.berlinModel.out.mapper.MethodMapper;
20 import eu.etaxonomy.cdm.io.common.DbExportStateBase;
21 import eu.etaxonomy.cdm.io.common.Source;
22 import eu.etaxonomy.cdm.model.common.CdmBase;
23 import eu.etaxonomy.cdm.model.description.DescriptionBase;
24 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
25 import eu.etaxonomy.cdm.model.description.TaxonDescription;
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27
28
29
30
31
32
33
34
35 @Component
36 @SuppressWarnings("unchecked")
37 public class PesiNoteSourceExport extends PesiExportBase {
38 private static final Logger logger = Logger.getLogger(PesiNoteSourceExport.class);
39 private static final Class<? extends CdmBase> standardMethodParameter = DescriptionElementBase.class;
40
41 private static int modCount = 1000;
42 private static final String dbTableName = "NoteSource";
43 private static final String pluralString = "NoteSources";
44
45 public PesiNoteSourceExport() {
46 super();
47 }
48
49
50
51
52 @Override
53 public Class<? extends CdmBase> getStandardMethodParameter() {
54 return standardMethodParameter;
55 }
56
57
58
59
60 @Override
61 protected boolean doCheck(PesiExportState state) {
62 boolean result = true;
63 return result;
64 }
65
66
67
68
69 @Override
70 protected boolean doInvoke(PesiExportState state) {
71 try {
72 logger.error("*** Started Making " + pluralString + " ...");
73
74
75
76 int pageSize = 1000;
77
78
79 int pageNumber = 1;
80
81
82 boolean success = true;
83
84
85 doDelete(state);
86
87
88 PesiExportMapping mapping = getMapping();
89
90
91 mapping.initialize(state);
92
93
94 int count = 0;
95 int pastCount = 0;
96 TransactionStatus txStatus = null;
97 List<DescriptionElementBase> list = null;
98
99
100 txStatus = startTransaction(true);
101 logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
102 while ((list = getDescriptionService().listDescriptionElements(null, null, null, pageSize, pageNumber, null)).size() > 0) {
103
104 logger.error("Fetched " + list.size() + " " + pluralString + ". Exporting...");
105 for (DescriptionElementBase descriptionElement : list) {
106
107 if (getNoteCategoryFk(descriptionElement) != null && neededValuesNotNull(descriptionElement, state)) {
108 doCount(count++, modCount, pluralString);
109 success &= mapping.invoke(descriptionElement);
110 }
111 }
112
113
114 commitTransaction(txStatus);
115 logger.error("Committed transaction.");
116 logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
117 pastCount = count;
118
119
120 txStatus = startTransaction(true);
121 logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
122
123
124 pageNumber++;
125 }
126 if (list.size() == 0) {
127 logger.error("No " + pluralString + " left to fetch.");
128 }
129
130 commitTransaction(txStatus);
131 logger.error("Committed transaction.");
132
133 logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
134
135 return success;
136 } catch (SQLException e) {
137 e.printStackTrace();
138 logger.error(e.getMessage());
139 return false;
140 }
141 }
142
143
144
145
146
147 private boolean neededValuesNotNull(DescriptionElementBase descriptionElement, PesiExportState state) {
148 boolean result = true;
149 if (getSourceFk(descriptionElement, state) == null) {
150 logger.error("SourceFk is NULL, but is not allowed to be. Therefore no record was written to export database for this descriptionElement: " + descriptionElement.getUuid());
151 result = false;
152 }
153 return result;
154 }
155
156
157
158
159
160
161 protected boolean doDelete(PesiExportState state) {
162 PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
163
164 String sql;
165 Source destination = pesiConfig.getDestination();
166
167
168 sql = "DELETE FROM " + dbTableName;
169 destination.setQuery(sql);
170 destination.update(sql);
171 return true;
172 }
173
174
175
176
177 @Override
178 protected boolean isIgnore(PesiExportState state) {
179
180
181 return false;
182 }
183
184
185
186
187
188
189 private static Integer getNoteCategoryFk(DescriptionElementBase descriptionElement) {
190 Integer result = null;
191 result = PesiTransformer.textData2NodeCategoryFk(descriptionElement.getFeature());
192 return result;
193 }
194
195
196
197
198
199
200
201
202 @SuppressWarnings("unused")
203 private static Integer getNoteFk(DescriptionElementBase descriptionElement, PesiExportState state) {
204 Integer result = null;
205 result = state.getDbId(descriptionElement);
206 return result;
207 }
208
209
210
211
212
213
214
215
216 private static Integer getSourceFk(DescriptionElementBase descriptionElement, PesiExportState state) {
217 Integer result = null;
218 result = state.getDbId(descriptionElement);
219 return result;
220 }
221
222
223
224
225
226
227
228 @SuppressWarnings("unused")
229 private static String getSourceNameCache(DescriptionElementBase descriptionElement) {
230 String result = null;
231
232 DescriptionBase inDescription = descriptionElement.getInDescription();
233 if (inDescription != null && inDescription.isInstanceOf(TaxonDescription.class)) {
234 TaxonDescription taxonDescription = CdmBase.deproxy(inDescription, TaxonDescription.class);
235 Taxon taxon = taxonDescription.getTaxon();
236 result = taxon.getSec().getTitleCache();
237 }
238
239 return result;
240 }
241
242
243
244
245
246
247
248 @SuppressWarnings("unused")
249 private static String getSourceDetail(DescriptionElementBase descriptionElement) {
250 return descriptionElement.getCitationMicroReference();
251 }
252
253
254
255
256
257 private PesiExportMapping getMapping() {
258 PesiExportMapping mapping = new PesiExportMapping(dbTableName);
259
260 mapping.addMapper(MethodMapper.NewInstance("NoteFk", this.getClass(), "getNoteFk", standardMethodParameter, PesiExportState.class));
261 mapping.addMapper(MethodMapper.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter, PesiExportState.class));
262 mapping.addMapper(MethodMapper.NewInstance("SourceNameCache", this));
263 mapping.addMapper(MethodMapper.NewInstance("SourceDetail", this));
264
265 return mapping;
266 }
267
268 }