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.common;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
|
17
|
import org.apache.log4j.Logger;
|
18
|
import org.springframework.beans.BeansException;
|
19
|
import org.springframework.context.ApplicationContext;
|
20
|
import org.springframework.context.ApplicationContextAware;
|
21
|
import org.springframework.stereotype.Component;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.service.IService;
|
24
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
25
|
import eu.etaxonomy.cdm.common.monitor.SubProgressMonitor;
|
26
|
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
|
27
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
28
|
|
29
|
/**
|
30
|
* This class is an default exporter class that is a spring bean and therefore it knows all other IO classes that are beans
|
31
|
*
|
32
|
* @author a.mueller
|
33
|
* @since 20.06.2008
|
34
|
*/
|
35
|
@Component("defaultExport")
|
36
|
public class CdmApplicationAwareDefaultExport<T extends IExportConfigurator>
|
37
|
implements ICdmExporter<T>, ApplicationContextAware {
|
38
|
|
39
|
private static final Logger logger = Logger.getLogger(CdmApplicationAwareDefaultExport.class);
|
40
|
|
41
|
protected ApplicationContext applicationContext;
|
42
|
|
43
|
@Override
|
44
|
public void setApplicationContext(ApplicationContext applicationContext)
|
45
|
throws BeansException {
|
46
|
this.applicationContext = applicationContext;
|
47
|
}
|
48
|
|
49
|
|
50
|
//Constants
|
51
|
final static boolean OBLIGATORY = true;
|
52
|
final static boolean FACULTATIVE = false;
|
53
|
final int modCount = 1000;
|
54
|
|
55
|
private final IService service = null;
|
56
|
|
57
|
//different type of stores that are used by the known imports
|
58
|
Map<String, MapWrapper<? extends CdmBase>> stores = new HashMap<>();
|
59
|
|
60
|
public CdmApplicationAwareDefaultExport(){
|
61
|
stores.put(ICdmIO.TEAM_STORE, new MapWrapper<>(service));
|
62
|
stores.put(ICdmIO.REFERENCE_STORE, new MapWrapper<>(service));
|
63
|
stores.put(ICdmIO.NOMREF_STORE, new MapWrapper<>(service));
|
64
|
stores.put(ICdmIO.TAXONNAME_STORE, new MapWrapper<>(service));
|
65
|
stores.put(ICdmIO.TAXON_STORE, new MapWrapper<>(service));
|
66
|
stores.put(ICdmIO.SPECIMEN_STORE, new MapWrapper<>(service));
|
67
|
}
|
68
|
|
69
|
|
70
|
@Override
|
71
|
public ExportResult invoke(T config){
|
72
|
ExportResult result;
|
73
|
if (config.getCheck().equals(IExportConfigurator.CHECK.CHECK_ONLY)){
|
74
|
result = ExportResult.NewInstance(config.getResultType());
|
75
|
boolean success = doCheck(config);
|
76
|
if (! success){
|
77
|
result.setAborted();
|
78
|
}
|
79
|
}else if (config.getCheck().equals(IExportConfigurator.CHECK.CHECK_AND_EXPORT)){
|
80
|
boolean success = doCheck(config);
|
81
|
if (success){
|
82
|
result = doExport(config);
|
83
|
}else{
|
84
|
result = ExportResult.NewInstance(config.getResultType());
|
85
|
result.setAborted();
|
86
|
}
|
87
|
}else if (config.getCheck().equals(IExportConfigurator.CHECK.EXPORT_WITHOUT_CHECK)){
|
88
|
result = doExport(config);
|
89
|
}else{
|
90
|
result = ExportResult.NewInstance(config.getResultType());
|
91
|
String message = "Unknown CHECK type";
|
92
|
logger.error(message);
|
93
|
result.addError(message);
|
94
|
}
|
95
|
return result;
|
96
|
}
|
97
|
|
98
|
|
99
|
public ExportResult execute(T config) {
|
100
|
ExportResult result = ExportResult.NewInstance(config.getResultType());
|
101
|
if (config.getCheck().equals(IExportConfigurator.CHECK.CHECK_ONLY)){
|
102
|
boolean success = doCheck(config);
|
103
|
if (! success){
|
104
|
result.setAborted();
|
105
|
}
|
106
|
} else if (config.getCheck().equals(IExportConfigurator.CHECK.CHECK_AND_EXPORT)){
|
107
|
boolean success = doCheck(config);
|
108
|
if (success){
|
109
|
result = doExport(config);
|
110
|
}else{
|
111
|
result.setAborted();
|
112
|
}
|
113
|
} else if (config.getCheck().equals(IExportConfigurator.CHECK.EXPORT_WITHOUT_CHECK)){
|
114
|
result = doExport(config);
|
115
|
} else{
|
116
|
String message = "Unknown CHECK type";
|
117
|
logger.error(message);
|
118
|
result.addError(message);
|
119
|
}
|
120
|
return result;
|
121
|
}
|
122
|
|
123
|
@SuppressWarnings("unchecked")
|
124
|
protected <S extends IExportConfigurator> boolean doCheck(S config){
|
125
|
|
126
|
boolean result = true;
|
127
|
|
128
|
//check
|
129
|
if (config == null){
|
130
|
logger.warn("CdmExportConfiguration is null");
|
131
|
// result.setState(ExportResultState.ABORTED);
|
132
|
return false;
|
133
|
}else if (! config.isValid()){
|
134
|
logger.warn("CdmExportConfiguration is not valid");
|
135
|
// result.setState(ExportResultState.ABORTED);
|
136
|
return false;
|
137
|
}
|
138
|
System.out.println("Start checking Source ("+ config.getSourceNameString() + ") ...");
|
139
|
|
140
|
ExportStateBase state = config.getNewState();
|
141
|
state.initialize(config);
|
142
|
|
143
|
//do check for each class
|
144
|
for (Class<ICdmExport> ioClass: config.getIoClassList()){
|
145
|
try {
|
146
|
String ioBeanName = getComponentBeanName(ioClass);
|
147
|
ICdmIO cdmIo = applicationContext.getBean(ioBeanName, ICdmIO.class);
|
148
|
if (cdmIo != null){
|
149
|
registerObservers(config, cdmIo);
|
150
|
result &= cdmIo.check(state);
|
151
|
unRegisterObservers(config, cdmIo);
|
152
|
}else{
|
153
|
String message = "cdmIO for class " + (ioClass == null ? "(null)" : ioClass.getSimpleName()) + " was null";
|
154
|
logger.error(message);
|
155
|
return false;
|
156
|
}
|
157
|
} catch (Exception e) {
|
158
|
logger.error(e);
|
159
|
e.printStackTrace();
|
160
|
return false;
|
161
|
}
|
162
|
}
|
163
|
|
164
|
//return
|
165
|
System.out.println("End checking Source ("+ config.getSourceNameString() + ") for export from Cdm");
|
166
|
return result;
|
167
|
|
168
|
}
|
169
|
|
170
|
|
171
|
private void registerObservers(IExportConfigurator config, ICdmIO io){
|
172
|
for (IIoObserver observer : config.getObservers()){
|
173
|
io.addObserver(observer);
|
174
|
}
|
175
|
}
|
176
|
|
177
|
private void unRegisterObservers(IExportConfigurator config, ICdmIO io){
|
178
|
for (IIoObserver observer : config.getObservers()){
|
179
|
io.removeObserver(observer);
|
180
|
}
|
181
|
}
|
182
|
|
183
|
|
184
|
|
185
|
/**
|
186
|
* Executes the whole
|
187
|
*/
|
188
|
protected <CONFIG extends T> ExportResult doExport(CONFIG config){
|
189
|
//validate
|
190
|
if (config == null){
|
191
|
ExportResult result = ExportResult.NewInstance(null);
|
192
|
String message = "Configuration is null";
|
193
|
logger.error(message);
|
194
|
result.addError(message);
|
195
|
result.setAborted();
|
196
|
return result;
|
197
|
}
|
198
|
ExportResult result = ExportResult.NewInstance(config.getResultType());
|
199
|
if (! config.isValid()){
|
200
|
String message = "Configuration is not valid";
|
201
|
logger.error(message);
|
202
|
result.addError(message);
|
203
|
result.setAborted();
|
204
|
return result;
|
205
|
}
|
206
|
|
207
|
ExportStateBase state = config.getNewState();
|
208
|
state.initialize(config);
|
209
|
state.setResult(result);
|
210
|
|
211
|
List<ICdmExport> ioList = makeIoList(state, config);
|
212
|
|
213
|
List<Integer> stepCounts = countSteps(state, ioList);
|
214
|
Integer totalCount = stepCounts.get(stepCounts.size()-1);
|
215
|
config.getProgressMonitor().beginTask(config.getUserFriendlyIOName() != null? config.getUserFriendlyIOName():"Start Export", totalCount);
|
216
|
config.getProgressMonitor().worked(1);
|
217
|
IProgressMonitor parentMonitor = SubProgressMonitor
|
218
|
.NewStarted(config.getProgressMonitor(), 99, "Process data", totalCount);
|
219
|
|
220
|
//do invoke for each class
|
221
|
for (int i = 0; i< ioList.size(); i++){
|
222
|
ICdmExport export = ioList.get(i);
|
223
|
Integer counts = stepCounts.get(i);
|
224
|
try {
|
225
|
String ioName = export.getClass().getSimpleName();
|
226
|
SubProgressMonitor ioMonitor = SubProgressMonitor
|
227
|
.NewStarted(parentMonitor, counts, ioName, counts );
|
228
|
// state.getConfig().setProgressMonitor(ioMonitor);
|
229
|
state.setCurrentIO(export);
|
230
|
export.invoke(state);
|
231
|
ioMonitor.done();
|
232
|
} catch (Exception e) {
|
233
|
String message = "Unexpected exception in " + export.getClass().getSimpleName()+ ": " + e.getMessage();
|
234
|
logger.error(message);
|
235
|
e.printStackTrace();
|
236
|
result.addException(e, message);
|
237
|
}
|
238
|
}
|
239
|
|
240
|
System.out.println("End export from source '" + config.getSourceNameString()
|
241
|
+ "' to destination '" + config.getDestinationNameString() + "' "
|
242
|
+ "("+ result.toString() + ")"
|
243
|
) ;
|
244
|
return result;
|
245
|
}
|
246
|
|
247
|
/**
|
248
|
* @param state
|
249
|
* @param ioList
|
250
|
* @return
|
251
|
*/
|
252
|
private List<Integer> countSteps(ExportStateBase state, List<ICdmExport> ioList) {
|
253
|
//do invoke for each class
|
254
|
List<Integer> result = new ArrayList<>();
|
255
|
int sum = 0;
|
256
|
for (ICdmExport export: ioList){
|
257
|
int count = 1;
|
258
|
try {
|
259
|
// state.setCurrentIO(export);
|
260
|
count = ((Long)export.countSteps(state)).intValue();
|
261
|
} catch (Exception e) {
|
262
|
String message = "Unexpected exception when count steps for progress monitoring " + export.getClass().getSimpleName()+ ": " + e.getMessage();
|
263
|
logger.error(message);
|
264
|
e.printStackTrace();
|
265
|
state.getResult().addException(e, message);
|
266
|
}
|
267
|
result.add(count);
|
268
|
sum += count;
|
269
|
}
|
270
|
result.add(sum);
|
271
|
return result;
|
272
|
}
|
273
|
|
274
|
|
275
|
/**
|
276
|
* @param state
|
277
|
* @param config
|
278
|
* @return
|
279
|
*/
|
280
|
private <CONFIG extends T> List<ICdmExport> makeIoList(ExportStateBase state, CONFIG config) {
|
281
|
|
282
|
List<ICdmExport> result = new ArrayList<>();
|
283
|
|
284
|
for (Class<ICdmExport> ioClass: config.getIoClassList()){
|
285
|
try {
|
286
|
String ioBeanName = getComponentBeanName(ioClass);
|
287
|
ICdmExport cdmIo = applicationContext.getBean(ioBeanName, ICdmExport.class);
|
288
|
if (cdmIo != null){
|
289
|
result.add(cdmIo);
|
290
|
}else{
|
291
|
String message = "cdmIO was null: " + ioBeanName;
|
292
|
logger.error(message);
|
293
|
state.getResult().addError(message);
|
294
|
}
|
295
|
} catch (Exception e) {
|
296
|
String message = "Unexpected exception in " + ioClass.getSimpleName()+ ": " + e.getMessage();
|
297
|
logger.error(message);
|
298
|
e.printStackTrace();
|
299
|
state.getResult().addException(e, message);
|
300
|
}
|
301
|
}
|
302
|
return result;
|
303
|
}
|
304
|
|
305
|
|
306
|
private String getComponentBeanName(Class<ICdmExport> ioClass){
|
307
|
Component component = ioClass.getAnnotation(Component.class);
|
308
|
String ioBean = component.value();
|
309
|
if ("".equals(ioBean)){
|
310
|
ioBean = ioClass.getSimpleName();
|
311
|
ioBean = ioBean.substring(0, 1).toLowerCase() + ioBean.substring(1); //make camelcase
|
312
|
}
|
313
|
return ioBean;
|
314
|
}
|
315
|
|
316
|
}
|