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