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