1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
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
|
* The export class for NoteSources.
|
30
|
* Inserts into DataWarehouse database table <code>NoteSource</code>.
|
31
|
* @author e.-m.lee
|
32
|
* @date 03.03.2010
|
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
|
/* (non-Javadoc)
|
50
|
* @see eu.etaxonomy.cdm.io.common.DbExportBase#getStandardMethodParameter()
|
51
|
*/
|
52
|
@Override
|
53
|
public Class<? extends CdmBase> getStandardMethodParameter() {
|
54
|
return standardMethodParameter;
|
55
|
}
|
56
|
|
57
|
/* (non-Javadoc)
|
58
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IoStateBase)
|
59
|
*/
|
60
|
@Override
|
61
|
protected boolean doCheck(PesiExportState state) {
|
62
|
boolean result = true;
|
63
|
return result;
|
64
|
}
|
65
|
|
66
|
/* (non-Javadoc)
|
67
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#doInvoke(eu.etaxonomy.cdm.io.common.IoStateBase)
|
68
|
*/
|
69
|
@Override
|
70
|
protected void doInvoke(PesiExportState state) {
|
71
|
try {
|
72
|
logger.error("*** Started Making " + pluralString + " ...");
|
73
|
|
74
|
// Get the limit for objects to save within a single transaction.
|
75
|
// int pageSize = state.getConfig().getLimitSave();
|
76
|
int pageSize = 1000;
|
77
|
|
78
|
// pageNumber
|
79
|
int pageNumber = 1;
|
80
|
|
81
|
// Stores whether this invoke was successful or not.
|
82
|
boolean success = true;
|
83
|
|
84
|
// PESI: Clear the database table NoteSource.
|
85
|
doDelete(state);
|
86
|
|
87
|
// Get specific mappings: (CDM) ? -> (PESI) NoteSource
|
88
|
PesiExportMapping mapping = getMapping();
|
89
|
|
90
|
// Initialize the db mapper
|
91
|
mapping.initialize(state);
|
92
|
|
93
|
// PESI: Create the NoteSource
|
94
|
int count = 0;
|
95
|
int pastCount = 0;
|
96
|
TransactionStatus txStatus = null;
|
97
|
List<DescriptionElementBase> list = null;
|
98
|
|
99
|
// Start transaction
|
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
|
// Commit transaction
|
114
|
commitTransaction(txStatus);
|
115
|
logger.error("Committed transaction.");
|
116
|
logger.error("Exported " + (count - pastCount) + " " + pluralString + ". Total: " + count);
|
117
|
pastCount = count;
|
118
|
|
119
|
// Start transaction
|
120
|
txStatus = startTransaction(true);
|
121
|
logger.error("Started new transaction. Fetching some " + pluralString + " (max: " + pageSize + ") ...");
|
122
|
|
123
|
// Increment pageNumber
|
124
|
pageNumber++;
|
125
|
}
|
126
|
if (list.size() == 0) {
|
127
|
logger.error("No " + pluralString + " left to fetch.");
|
128
|
}
|
129
|
// Commit transaction
|
130
|
commitTransaction(txStatus);
|
131
|
logger.error("Committed transaction.");
|
132
|
|
133
|
logger.error("*** Finished Making " + pluralString + " ..." + getSuccessString(success));
|
134
|
|
135
|
if (!success){
|
136
|
state.setUnsuccessfull();
|
137
|
}
|
138
|
return;
|
139
|
} catch (SQLException e) {
|
140
|
e.printStackTrace();
|
141
|
logger.error(e.getMessage());
|
142
|
state.setUnsuccessfull();
|
143
|
return;
|
144
|
}
|
145
|
}
|
146
|
|
147
|
/**
|
148
|
* Checks whether needed values for an entity are NULL.
|
149
|
* @return
|
150
|
*/
|
151
|
private boolean neededValuesNotNull(DescriptionElementBase descriptionElement, PesiExportState state) {
|
152
|
boolean result = true;
|
153
|
if (getSourceFk(descriptionElement, state) == null) {
|
154
|
logger.error("SourceFk is NULL, but is not allowed to be. Therefore no record was written to export database for this descriptionElement: " + descriptionElement.getUuid());
|
155
|
result = false;
|
156
|
}
|
157
|
return result;
|
158
|
}
|
159
|
|
160
|
/**
|
161
|
* Deletes all entries of database tables related to <code>NoteSource</code>.
|
162
|
* @param state The {@link PesiExportState PesiExportState}.
|
163
|
* @return Whether the delete operation was successful or not.
|
164
|
*/
|
165
|
protected boolean doDelete(PesiExportState state) {
|
166
|
PesiExportConfigurator pesiConfig = (PesiExportConfigurator) state.getConfig();
|
167
|
|
168
|
String sql;
|
169
|
Source destination = pesiConfig.getDestination();
|
170
|
|
171
|
// Clear NoteSource
|
172
|
sql = "DELETE FROM " + dbTableName;
|
173
|
destination.setQuery(sql);
|
174
|
destination.update(sql);
|
175
|
return true;
|
176
|
}
|
177
|
|
178
|
/* (non-Javadoc)
|
179
|
* @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IoStateBase)
|
180
|
*/
|
181
|
@Override
|
182
|
protected boolean isIgnore(PesiExportState state) {
|
183
|
// TODO
|
184
|
// return ! state.getConfig().isDoNoteSource();
|
185
|
return false;
|
186
|
}
|
187
|
|
188
|
/**
|
189
|
* Returns the <code>NoteCategoryFk</code> attribute.
|
190
|
* @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
|
191
|
* @return The <code>NoteCategoryFk</code> attribute.
|
192
|
*/
|
193
|
private static Integer getNoteCategoryFk(DescriptionElementBase descriptionElement) {
|
194
|
Integer result = null;
|
195
|
result = PesiTransformer.textData2NodeCategoryFk(descriptionElement.getFeature());
|
196
|
return result;
|
197
|
}
|
198
|
|
199
|
/**
|
200
|
* Returns the <code>NoteFk</code> attribute.
|
201
|
* @param description The {@link TaxonDescription TaxonDescription}.
|
202
|
* @param state The {@link PesiExportState PesiExportState}.
|
203
|
* @return The <code>NoteFk</code> attribute.
|
204
|
* @see MethodMapper
|
205
|
*/
|
206
|
@SuppressWarnings("unused")
|
207
|
private static Integer getNoteFk(DescriptionElementBase descriptionElement, PesiExportState state) {
|
208
|
Integer result = null;
|
209
|
result = state.getDbId(descriptionElement);
|
210
|
return result;
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* Returns the <code>SourceFk</code> attribute.
|
215
|
* @param description The {@link TaxonDescription TaxonDescription}.
|
216
|
* @param state The {@link DbExportStateBase DbExportState}.
|
217
|
* @return The <code>SourceFk</code> attribute.
|
218
|
* @see MethodMapper
|
219
|
*/
|
220
|
private static Integer getSourceFk(DescriptionElementBase descriptionElement, PesiExportState state) {
|
221
|
Integer result = null;
|
222
|
result = state.getDbId(descriptionElement);
|
223
|
return result;
|
224
|
}
|
225
|
|
226
|
/**
|
227
|
* Returns the <code>SourceNameCache</code> attribute.
|
228
|
* @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
|
229
|
* @return The <code>SourceNameCache</code> attribute.
|
230
|
* @see MethodMapper
|
231
|
*/
|
232
|
@SuppressWarnings("unused")
|
233
|
private static String getSourceNameCache(DescriptionElementBase descriptionElement) {
|
234
|
String result = null;
|
235
|
|
236
|
DescriptionBase inDescription = descriptionElement.getInDescription();
|
237
|
if (inDescription != null && inDescription.isInstanceOf(TaxonDescription.class)) {
|
238
|
TaxonDescription taxonDescription = CdmBase.deproxy(inDescription, TaxonDescription.class);
|
239
|
Taxon taxon = taxonDescription.getTaxon();
|
240
|
result = taxon.getSec().getTitleCache();
|
241
|
}
|
242
|
|
243
|
return result;
|
244
|
}
|
245
|
|
246
|
/**
|
247
|
* Returns the <code>SourceDetail</code> attribute.
|
248
|
* @param descriptionElement The {@link DescriptionElementBase DescriptionElement}.
|
249
|
* @return The <code>SourceDetail</code> attribute.
|
250
|
* @see MethodMapper
|
251
|
*/
|
252
|
@SuppressWarnings("unused")
|
253
|
private static String getSourceDetail(DescriptionElementBase descriptionElement) {
|
254
|
//FIXME this is a replacement for the deprecated descriptionElement.getCitationMicroReference()
|
255
|
//it needs to be checked what should be done when multiple sources exist
|
256
|
if (descriptionElement.getSources().size() < 1){
|
257
|
return null;
|
258
|
}else{
|
259
|
if (descriptionElement.getSources().size() > 1){
|
260
|
logger.warn("Multiple sources exist");
|
261
|
}
|
262
|
return descriptionElement.getSources().iterator().next().getCitationMicroReference();
|
263
|
}
|
264
|
// return descriptionElement.getCitationMicroReference(); // TODO: What should be used instead?
|
265
|
}
|
266
|
|
267
|
/**
|
268
|
* Returns the CDM to PESI specific export mappings.
|
269
|
* @return The {@link PesiExportMapping PesiExportMapping}.
|
270
|
*/
|
271
|
private PesiExportMapping getMapping() {
|
272
|
PesiExportMapping mapping = new PesiExportMapping(dbTableName);
|
273
|
|
274
|
mapping.addMapper(MethodMapper.NewInstance("NoteFk", this.getClass(), "getNoteFk", standardMethodParameter, PesiExportState.class));
|
275
|
mapping.addMapper(MethodMapper.NewInstance("SourceFk", this.getClass(), "getSourceFk", standardMethodParameter, PesiExportState.class));
|
276
|
mapping.addMapper(MethodMapper.NewInstance("SourceNameCache", this));
|
277
|
mapping.addMapper(MethodMapper.NewInstance("SourceDetail", this));
|
278
|
|
279
|
return mapping;
|
280
|
}
|
281
|
|
282
|
}
|