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.security.authentication.AuthenticationManager;
|
20
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
21
|
import org.springframework.security.core.Authentication;
|
22
|
import org.springframework.security.core.context.SecurityContext;
|
23
|
import org.springframework.security.core.context.SecurityContextHolder;
|
24
|
import org.springframework.stereotype.Component;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.service.IService;
|
27
|
import eu.etaxonomy.cdm.io.common.events.IIoObserver;
|
28
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
29
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
30
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
31
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
|
32
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
33
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
34
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmPermissionEvaluator;
|
35
|
|
36
|
/**
|
37
|
* @author a.mueller
|
38
|
* @created 20.06.2008
|
39
|
* @version 1.0
|
40
|
*/
|
41
|
|
42
|
@Component("defaultImport")
|
43
|
public class CdmApplicationAwareDefaultImport<T extends IImportConfigurator> implements ICdmImporter<T>, ApplicationContextAware {
|
44
|
private static final Logger logger = Logger.getLogger(CdmApplicationAwareDefaultImport.class);
|
45
|
|
46
|
protected ApplicationContext applicationContext;
|
47
|
|
48
|
|
49
|
@Override
|
50
|
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
51
|
this.applicationContext = applicationContext;
|
52
|
}
|
53
|
|
54
|
|
55
|
//Constants
|
56
|
final boolean OBLIGATORY = true;
|
57
|
final boolean FACULTATIVE = false;
|
58
|
final int modCount = 1000;
|
59
|
|
60
|
IService<CdmBase> service = null;
|
61
|
|
62
|
//different type of stores that are used by the known imports
|
63
|
Map<String, MapWrapper<? extends CdmBase>> stores = new HashMap<String, MapWrapper<? extends CdmBase>>();
|
64
|
|
65
|
public CdmApplicationAwareDefaultImport(){
|
66
|
|
67
|
stores.put(ICdmIO.TEAM_STORE, new MapWrapper<TeamOrPersonBase<?>>(service));
|
68
|
stores.put(ICdmIO.REFERENCE_STORE, new MapWrapper<Reference>(service));
|
69
|
stores.put(ICdmIO.NOMREF_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<DerivedUnit>(service));
|
73
|
}
|
74
|
|
75
|
|
76
|
@Override
|
77
|
public boolean invoke(IImportConfigurator config){
|
78
|
if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_ONLY)){
|
79
|
return doCheck(config);
|
80
|
}else if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_AND_IMPORT)){
|
81
|
doCheck(config);
|
82
|
return doImport(config);
|
83
|
}else if (config.getCheck().equals(IImportConfigurator.CHECK.IMPORT_WITHOUT_CHECK)){
|
84
|
return doImport(config);
|
85
|
}else{
|
86
|
logger.error("Unknown CHECK type");
|
87
|
return false;
|
88
|
}
|
89
|
}
|
90
|
|
91
|
|
92
|
public ImportResult execute(IImportConfigurator config){
|
93
|
ImportResult result = new ImportResult();
|
94
|
if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_ONLY)){
|
95
|
result.setSuccess(doCheck(config));
|
96
|
}else if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_AND_IMPORT)){
|
97
|
boolean success = doCheck(config);
|
98
|
if(success) {
|
99
|
success = doImport(config, result);
|
100
|
}
|
101
|
result.setSuccess(success);
|
102
|
} else if (config.getCheck().equals(IImportConfigurator.CHECK.IMPORT_WITHOUT_CHECK)){
|
103
|
doImport(config, result);
|
104
|
} else{
|
105
|
logger.error("Unknown CHECK type");
|
106
|
return null;
|
107
|
}
|
108
|
return result;
|
109
|
}
|
110
|
|
111
|
|
112
|
@SuppressWarnings("unchecked")
|
113
|
protected <S extends IImportConfigurator> boolean doCheck(S config){
|
114
|
boolean result = true;
|
115
|
|
116
|
//check
|
117
|
if (config == null){
|
118
|
logger.warn("CdmImportConfiguration is null");
|
119
|
return false;
|
120
|
}
|
121
|
System.out.println("Start checking Source ("+ config.getSourceNameString() + ") ...");
|
122
|
if (! config.isValid()){
|
123
|
logger.warn("CdmImportConfiguration is not valid");
|
124
|
return false;
|
125
|
}
|
126
|
|
127
|
ImportStateBase state = config.getNewState();
|
128
|
state.initialize(config);
|
129
|
|
130
|
//do check for each class
|
131
|
for (Class<ICdmIO> 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
|
state.setCurrentIO(cdmIo);
|
138
|
result &= cdmIo.check(state);
|
139
|
unRegisterObservers(config, cdmIo);
|
140
|
}else{
|
141
|
logger.error("cdmIO was null");
|
142
|
result = false;
|
143
|
}
|
144
|
} catch (Exception e) {
|
145
|
logger.error(e);
|
146
|
e.printStackTrace();
|
147
|
result = false;
|
148
|
}
|
149
|
}
|
150
|
|
151
|
//return
|
152
|
System.out.println("End checking Source ("+ config.getSourceNameString() + ") for import to Cdm");
|
153
|
return result;
|
154
|
|
155
|
}
|
156
|
|
157
|
private void registerObservers(IImportConfigurator config, ICdmIO io){
|
158
|
for (IIoObserver observer : config.getObservers()){
|
159
|
io.addObserver(observer);
|
160
|
}
|
161
|
}
|
162
|
|
163
|
private void unRegisterObservers(IImportConfigurator config, ICdmIO io){
|
164
|
for (IIoObserver observer : config.getObservers()){
|
165
|
io.removeObserver(observer);
|
166
|
}
|
167
|
}
|
168
|
|
169
|
protected <S extends IImportConfigurator> boolean doImport(S config){
|
170
|
ImportResult result = new ImportResult();
|
171
|
return doImport(config, result);
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Executes the whole
|
176
|
*/
|
177
|
protected <S extends IImportConfigurator> boolean doImport(S config, ImportResult importResult){
|
178
|
boolean result = true;
|
179
|
//validate
|
180
|
if (config == null){
|
181
|
logger.warn("Configuration is null");
|
182
|
importResult.setSuccess(false);
|
183
|
return false;
|
184
|
}else if (! config.isValid()){
|
185
|
logger.warn("Configuration is not valid");
|
186
|
importResult.setSuccess(false);
|
187
|
return false;
|
188
|
}
|
189
|
|
190
|
config.getSourceReference();
|
191
|
logger.info("Start import from Source '"+ config.getSourceNameString() + "' to destination '" + config.getDestinationNameString() + "'");
|
192
|
|
193
|
ImportStateBase state = config.getNewState();
|
194
|
state.initialize(config);
|
195
|
|
196
|
CdmPermissionEvaluator permissionEval = applicationContext.getBean("cdmPermissionEvaluator", CdmPermissionEvaluator.class);
|
197
|
|
198
|
state.setSuccess(true);
|
199
|
//do invoke for each class
|
200
|
for (Class<ICdmIO> ioClass: config.getIoClassList()){
|
201
|
try {
|
202
|
String ioBeanName = getComponentBeanName(ioClass);
|
203
|
ICdmIO cdmIo = applicationContext.getBean(ioBeanName, ICdmIO.class);
|
204
|
if (cdmIo != null){
|
205
|
registerObservers(config, cdmIo);
|
206
|
state.setCurrentIO(cdmIo);
|
207
|
result &= cdmIo.invoke(state);
|
208
|
//importResult.addReport(cdmIo.getByteArray());
|
209
|
unRegisterObservers(config, cdmIo);
|
210
|
}else{
|
211
|
logger.error("cdmIO was null");
|
212
|
result = false;
|
213
|
}
|
214
|
} catch (Exception e) {
|
215
|
logger.error(e);
|
216
|
e.printStackTrace();
|
217
|
result = false;
|
218
|
}
|
219
|
}
|
220
|
|
221
|
//do invoke for each class
|
222
|
// for (String ioBean: config.getIoBeans()){
|
223
|
// try {
|
224
|
// ICdmIO<S> cdmIo = (ICdmIO<S>)applicationContext.getBean(ioBean, ICdmIO.class);
|
225
|
// if (cdmIo != null){
|
226
|
// result &= cdmIo.invoke(config, stores);
|
227
|
// }else{
|
228
|
// logger.error("cdmIO was null");
|
229
|
// result = false;
|
230
|
// }
|
231
|
// } catch (Exception e) {
|
232
|
// logger.error(e);
|
233
|
// e.printStackTrace();
|
234
|
// result = false;
|
235
|
// }
|
236
|
//
|
237
|
// }
|
238
|
|
239
|
logger.info("End import from source '" + config.getSourceNameString()
|
240
|
+ "' to destination '" + config.getDestinationNameString() + "'"+
|
241
|
(result? "(successful)":"(with errors)")) ;
|
242
|
importResult.setSuccess(result);
|
243
|
return result;
|
244
|
}
|
245
|
|
246
|
/**
|
247
|
* Returns the name of a component bean. If the name is defined in the Component annotation this name is returned.
|
248
|
* Otherwise the class name is returned with starting lower case.
|
249
|
* @param ioClass
|
250
|
* @return
|
251
|
* @throws IllegalArgumentException if the class does not have a "Component" annotation
|
252
|
*/
|
253
|
public static String getComponentBeanName(Class<ICdmIO> ioClass) throws IllegalArgumentException {
|
254
|
Component component = ioClass.getAnnotation(Component.class);
|
255
|
if (component == null){
|
256
|
throw new IllegalArgumentException("Class " + ioClass.getName() + " is missing a @Component annotation." );
|
257
|
}
|
258
|
String ioBean = component.value();
|
259
|
if ("".equals(ioBean)){
|
260
|
ioBean = ioClass.getSimpleName();
|
261
|
ioBean = ioBean.substring(0, 1).toLowerCase() + ioBean.substring(1); //make camelcase
|
262
|
}
|
263
|
return ioBean;
|
264
|
}
|
265
|
|
266
|
public void authenticate(IImportConfigurator config) {
|
267
|
UsernamePasswordAuthenticationToken token = config.getAuthenticationToken();
|
268
|
if (token != null){
|
269
|
SecurityContext context = SecurityContextHolder.getContext();
|
270
|
|
271
|
AuthenticationManager authenticationManager = applicationContext.getBean("authenticationManager", AuthenticationManager.class);;
|
272
|
Authentication authentication = authenticationManager.authenticate(token);
|
273
|
context.setAuthentication(authentication);
|
274
|
}else{
|
275
|
logger.warn("No authentication token available");
|
276
|
}
|
277
|
|
278
|
}
|
279
|
|
280
|
}
|