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.HashMap;
|
13
|
import java.util.Map;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.springframework.beans.BeansException;
|
17
|
import org.springframework.context.ApplicationContext;
|
18
|
import org.springframework.context.ApplicationContextAware;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
|
21
|
import eu.etaxonomy.cdm.api.service.IService;
|
22
|
import eu.etaxonomy.cdm.io.common.IExportConfigurator.TARGET;
|
23
|
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
|
24
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
25
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
27
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
28
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
29
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
30
|
|
31
|
/**
|
32
|
* This class is an default exporter class that is a spring bean and therefore it knows all other IO classes that are beans
|
33
|
*
|
34
|
* @author a.mueller
|
35
|
* @created 20.06.2008
|
36
|
* @version 1.0
|
37
|
*/
|
38
|
|
39
|
@Component("defaultExport")
|
40
|
public class CdmApplicationAwareDefaultExport<T extends IExportConfigurator> implements ICdmExporter<T>, ApplicationContextAware {
|
41
|
private static final Logger logger = Logger.getLogger(CdmApplicationAwareDefaultExport.class);
|
42
|
|
43
|
protected ApplicationContext applicationContext;
|
44
|
|
45
|
/* (non-Javadoc)
|
46
|
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext)
|
47
|
*/
|
48
|
@Override
|
49
|
public void setApplicationContext(ApplicationContext applicationContext)
|
50
|
throws BeansException {
|
51
|
this.applicationContext = applicationContext;
|
52
|
}
|
53
|
|
54
|
// DbExportStateBase<T> state;
|
55
|
|
56
|
|
57
|
//Constants
|
58
|
final static boolean OBLIGATORY = true;
|
59
|
final static boolean FACULTATIVE = false;
|
60
|
final int modCount = 1000;
|
61
|
|
62
|
private final IService service = null;
|
63
|
|
64
|
//different type of stores that are used by the known imports
|
65
|
Map<String, MapWrapper<? extends CdmBase>> stores = new HashMap<String, MapWrapper<? extends CdmBase>>();
|
66
|
|
67
|
public CdmApplicationAwareDefaultExport(){
|
68
|
stores.put(ICdmIO.TEAM_STORE, new MapWrapper<TeamOrPersonBase>(service));
|
69
|
stores.put(ICdmIO.REFERENCE_STORE, new MapWrapper<Reference>(service));
|
70
|
stores.put(ICdmIO.NOMREF_STORE, new MapWrapper<Reference>(service));
|
71
|
stores.put(ICdmIO.TAXONNAME_STORE, new MapWrapper<TaxonNameBase>(service));
|
72
|
stores.put(ICdmIO.TAXON_STORE, new MapWrapper<TaxonBase>(service));
|
73
|
stores.put(ICdmIO.SPECIMEN_STORE, new MapWrapper<DerivedUnit>(service));
|
74
|
}
|
75
|
|
76
|
|
77
|
@Override
|
78
|
public boolean invoke(IExportConfigurator config){
|
79
|
if (config.getCheck().equals(IExportConfigurator.CHECK.CHECK_ONLY)){
|
80
|
return doCheck(config);
|
81
|
}else if (config.getCheck().equals(IExportConfigurator.CHECK.CHECK_AND_EXPORT)){
|
82
|
doCheck(config);
|
83
|
return doExport(config);
|
84
|
}else if (config.getCheck().equals(IExportConfigurator.CHECK.EXPORT_WITHOUT_CHECK)){
|
85
|
return doExport(config);
|
86
|
}else{
|
87
|
logger.error("Unknown CHECK type");
|
88
|
return false;
|
89
|
}
|
90
|
}
|
91
|
|
92
|
|
93
|
public ExportResult execute(IExportConfigurator config) {
|
94
|
ExportResult result = new ExportResult();
|
95
|
if (config.getCheck().equals(IExportConfigurator.CHECK.CHECK_ONLY)){
|
96
|
result.setSuccess(doCheck(config));
|
97
|
} else if (config.getCheck().equals(IExportConfigurator.CHECK.CHECK_AND_EXPORT)){
|
98
|
boolean success = doCheck(config);
|
99
|
if(success) {
|
100
|
success = doExport(config, result);
|
101
|
}
|
102
|
result.setSuccess(success);
|
103
|
|
104
|
} else if (config.getCheck().equals(IExportConfigurator.CHECK.EXPORT_WITHOUT_CHECK)){
|
105
|
doExport(config, result);
|
106
|
} else{
|
107
|
logger.error("Unknown CHECK type");
|
108
|
return null;
|
109
|
}
|
110
|
return result;
|
111
|
}
|
112
|
|
113
|
@SuppressWarnings("unchecked")
|
114
|
protected <S extends IExportConfigurator> boolean doCheck(S config){
|
115
|
boolean result = true;
|
116
|
|
117
|
//check
|
118
|
if (config == null){
|
119
|
logger.warn("CdmExportConfiguration is null");
|
120
|
return false;
|
121
|
}else if (! config.isValid()){
|
122
|
logger.warn("CdmExportConfiguration is not valid");
|
123
|
return false;
|
124
|
}
|
125
|
System.out.println("Start checking Source ("+ config.getSourceNameString() + ") ...");
|
126
|
|
127
|
ExportStateBase state = config.getNewState();
|
128
|
state.initialize(config);
|
129
|
|
130
|
//do check for each class
|
131
|
for (Class<ICdmExport> ioClass: config.getIoClassList()){
|
132
|
try {
|
133
|
String ioBeanName = getComponentBeanName(ioClass);
|
134
|
ICdmIO cdmIo = applicationContext.getBean(ioBeanName, ICdmIO.class);
|
135
|
if (cdmIo != null){
|
136
|
registerObservers(config, cdmIo);
|
137
|
result &= cdmIo.check(state);
|
138
|
unRegisterObservers(config, cdmIo);
|
139
|
}else{
|
140
|
logger.error("cdmIO for class " + (ioClass == null ? "(null)" : ioClass.getSimpleName()) + " was null");
|
141
|
result = false;
|
142
|
}
|
143
|
} catch (Exception e) {
|
144
|
logger.error(e);
|
145
|
e.printStackTrace();
|
146
|
result = false;
|
147
|
}
|
148
|
}
|
149
|
|
150
|
//return
|
151
|
System.out.println("End checking Source ("+ config.getSourceNameString() + ") for export from Cdm");
|
152
|
return result;
|
153
|
|
154
|
}
|
155
|
|
156
|
|
157
|
private void registerObservers(IExportConfigurator config, ICdmIO io){
|
158
|
for (IIoObserver observer : config.getObservers()){
|
159
|
io.addObserver(observer);
|
160
|
}
|
161
|
}
|
162
|
|
163
|
private void unRegisterObservers(IExportConfigurator config, ICdmIO io){
|
164
|
for (IIoObserver observer : config.getObservers()){
|
165
|
io.removeObserver(observer);
|
166
|
}
|
167
|
}
|
168
|
|
169
|
protected <CONFIG extends IExportConfigurator> boolean doExport(CONFIG config) {
|
170
|
ExportResult exportResult = new ExportResult();
|
171
|
return doExport(config, exportResult);
|
172
|
|
173
|
}
|
174
|
|
175
|
/**
|
176
|
* Executes the whole
|
177
|
*/
|
178
|
protected <CONFIG extends IExportConfigurator> boolean doExport(CONFIG config, ExportResult exportResult){
|
179
|
boolean result = true;
|
180
|
//validate
|
181
|
if (config == null){
|
182
|
logger.warn("Configuration is null");
|
183
|
exportResult.setSuccess(false);
|
184
|
return false;
|
185
|
}else if (! config.isValid()){
|
186
|
logger.warn("Configuration is not valid");
|
187
|
exportResult.setSuccess(false);
|
188
|
return false;
|
189
|
}
|
190
|
|
191
|
System.out.println("Start export from source '" + config.getSourceNameString()
|
192
|
+ "' to destination '" + config.getDestinationNameString() + "'");
|
193
|
|
194
|
ExportStateBase state = config.getNewState();
|
195
|
state.initialize(config);
|
196
|
|
197
|
//do invoke for each class
|
198
|
for (Class<ICdmExport> ioClass: config.getIoClassList()){
|
199
|
try {
|
200
|
String ioBeanName = getComponentBeanName(ioClass);
|
201
|
ICdmExport cdmIo = applicationContext.getBean(ioBeanName, ICdmExport.class);
|
202
|
if (cdmIo != null){
|
203
|
//result &= cdmIo.invoke(config, stores);
|
204
|
state.setCurrentIO(cdmIo);
|
205
|
result &= cdmIo.invoke(state);
|
206
|
if (config.getTarget().equals(TARGET.EXPORT_DATA)){
|
207
|
exportResult.addExportData(cdmIo.getByteArray());
|
208
|
}
|
209
|
// IoState<S> state = null;
|
210
|
// result &= cdmIo.invoke(state);
|
211
|
}else{
|
212
|
logger.error("cdmIO was null");
|
213
|
result = false;
|
214
|
}
|
215
|
} catch (Exception e) {
|
216
|
logger.error(e);
|
217
|
e.printStackTrace();
|
218
|
result = false;
|
219
|
}
|
220
|
}
|
221
|
|
222
|
System.out.println("End export from source '" + config.getSourceNameString()
|
223
|
+ "' to destination '" + config.getDestinationNameString() + "' " +
|
224
|
(result? "(successful)":"(with errors)")) ;
|
225
|
exportResult.setSuccess(result);
|
226
|
return result;
|
227
|
}
|
228
|
|
229
|
private String getComponentBeanName(Class<ICdmExport> ioClass){
|
230
|
Component component = ioClass.getAnnotation(Component.class);
|
231
|
String ioBean = component.value();
|
232
|
if ("".equals(ioBean)){
|
233
|
ioBean = ioClass.getSimpleName();
|
234
|
ioBean = ioBean.substring(0, 1).toLowerCase() + ioBean.substring(1); //make camelcase
|
235
|
}
|
236
|
return ioBean;
|
237
|
}
|
238
|
|
239
|
}
|