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 ImportResult invoke(IImportConfigurator config){
|
78
|
if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_ONLY)){
|
79
|
ImportResult result = new ImportResult();
|
80
|
result.setSuccess(doCheck(config));
|
81
|
return result;
|
82
|
}else if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_AND_IMPORT)){
|
83
|
doCheck(config);
|
84
|
return doImport(config);
|
85
|
}else if (config.getCheck().equals(IImportConfigurator.CHECK.IMPORT_WITHOUT_CHECK)){
|
86
|
return doImport(config);
|
87
|
}else{
|
88
|
logger.error("Unknown CHECK type");
|
89
|
ImportResult result = new ImportResult();
|
90
|
result.setSuccess(false);
|
91
|
return result;
|
92
|
}
|
93
|
}
|
94
|
|
95
|
|
96
|
public ImportResult execute(IImportConfigurator config){
|
97
|
ImportResult result = new ImportResult();
|
98
|
if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_ONLY)){
|
99
|
result.setSuccess(doCheck(config));
|
100
|
}else if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_AND_IMPORT)){
|
101
|
boolean success = doCheck(config);
|
102
|
if(success) {
|
103
|
result = doImport(config);
|
104
|
}
|
105
|
result.setSuccess(success);
|
106
|
} else if (config.getCheck().equals(IImportConfigurator.CHECK.IMPORT_WITHOUT_CHECK)){
|
107
|
result = doImport(config);
|
108
|
} else{
|
109
|
logger.error("Unknown CHECK type");
|
110
|
return null;
|
111
|
}
|
112
|
return result;
|
113
|
}
|
114
|
|
115
|
|
116
|
@SuppressWarnings("unchecked")
|
117
|
protected <S extends IImportConfigurator> boolean doCheck(S config){
|
118
|
boolean result = true;
|
119
|
|
120
|
//check
|
121
|
if (config == null){
|
122
|
logger.warn("CdmImportConfiguration is null");
|
123
|
return false;
|
124
|
}
|
125
|
System.out.println("Start checking Source ("+ config.getSourceNameString() + ") ...");
|
126
|
if (! config.isValid()){
|
127
|
logger.warn("CdmImportConfiguration is not valid");
|
128
|
return false;
|
129
|
}
|
130
|
|
131
|
ImportStateBase state = config.getNewState();
|
132
|
state.initialize(config);
|
133
|
|
134
|
//do check for each class
|
135
|
for (Class<ICdmImport> ioClass: config.getIoClassList()){
|
136
|
try {
|
137
|
String ioBeanName = getComponentBeanName(ioClass);
|
138
|
ICdmIO cdmIo = applicationContext.getBean(ioBeanName, ICdmIO.class);
|
139
|
if (cdmIo != null){
|
140
|
registerObservers(config, cdmIo);
|
141
|
state.setCurrentIO(cdmIo);
|
142
|
result &= cdmIo.check(state);
|
143
|
unRegisterObservers(config, cdmIo);
|
144
|
}else{
|
145
|
logger.error("cdmIO was null");
|
146
|
result = false;
|
147
|
}
|
148
|
} catch (Exception e) {
|
149
|
logger.error(e);
|
150
|
e.printStackTrace();
|
151
|
result = false;
|
152
|
}
|
153
|
}
|
154
|
|
155
|
//return
|
156
|
System.out.println("End checking Source ("+ config.getSourceNameString() + ") for import to Cdm");
|
157
|
return result;
|
158
|
|
159
|
}
|
160
|
|
161
|
private void registerObservers(IImportConfigurator config, ICdmIO io){
|
162
|
for (IIoObserver observer : config.getObservers()){
|
163
|
io.addObserver(observer);
|
164
|
}
|
165
|
}
|
166
|
|
167
|
private void unRegisterObservers(IImportConfigurator config, ICdmIO io){
|
168
|
for (IIoObserver observer : config.getObservers()){
|
169
|
io.removeObserver(observer);
|
170
|
}
|
171
|
}
|
172
|
|
173
|
/**
|
174
|
* Executes the whole
|
175
|
*/
|
176
|
protected <S extends IImportConfigurator> ImportResult doImport(S config){
|
177
|
ImportResult importResult = new ImportResult();
|
178
|
//validate
|
179
|
if (config == null){
|
180
|
logger.warn("Configuration is null");
|
181
|
importResult.setSuccess(false);
|
182
|
return importResult;
|
183
|
}else if (! config.isValid()){
|
184
|
logger.warn("Configuration is not valid");
|
185
|
importResult.setSuccess(false);
|
186
|
return importResult;
|
187
|
}
|
188
|
|
189
|
config.getSourceReference();
|
190
|
logger.info("Start import from Source '"+ config.getSourceNameString() + "' to destination '" + config.getDestinationNameString() + "'");
|
191
|
|
192
|
ImportStateBase state = config.getNewState();
|
193
|
state.initialize(config);
|
194
|
|
195
|
CdmPermissionEvaluator permissionEval = applicationContext.getBean("cdmPermissionEvaluator", CdmPermissionEvaluator.class);
|
196
|
|
197
|
state.setSuccess(true);
|
198
|
//do invoke for each class
|
199
|
for (Class<ICdmImport> ioClass: config.getIoClassList()){
|
200
|
try {
|
201
|
String ioBeanName = getComponentBeanName(ioClass);
|
202
|
ICdmImport cdmIo = applicationContext.getBean(ioBeanName, ICdmImport.class);
|
203
|
if (cdmIo != null){
|
204
|
registerObservers(config, cdmIo);
|
205
|
state.setCurrentIO(cdmIo);
|
206
|
importResult.setSuccess(cdmIo.invoke(state));
|
207
|
importResult.addReport(state.getReportAsByteArray());
|
208
|
unRegisterObservers(config, cdmIo);
|
209
|
}else{
|
210
|
logger.error("cdmIO was null");
|
211
|
importResult.setSuccess(false);
|
212
|
}
|
213
|
} catch (Exception e) {
|
214
|
logger.error(e);
|
215
|
e.printStackTrace();
|
216
|
importResult.setSuccess(false);
|
217
|
}
|
218
|
}
|
219
|
|
220
|
//do invoke for each class
|
221
|
// for (String ioBean: config.getIoBeans()){
|
222
|
// try {
|
223
|
// ICdmIO<S> cdmIo = (ICdmIO<S>)applicationContext.getBean(ioBean, ICdmIO.class);
|
224
|
// if (cdmIo != null){
|
225
|
// result &= cdmIo.invoke(config, stores);
|
226
|
// }else{
|
227
|
// logger.error("cdmIO was null");
|
228
|
// result = false;
|
229
|
// }
|
230
|
// } catch (Exception e) {
|
231
|
// logger.error(e);
|
232
|
// e.printStackTrace();
|
233
|
// result = false;
|
234
|
// }
|
235
|
//
|
236
|
// }
|
237
|
|
238
|
logger.info("End import from source '" + config.getSourceNameString()
|
239
|
+ "' to destination '" + config.getDestinationNameString() + "'"+
|
240
|
(importResult.isSuccess()? "(successful)":"(with errors)")) ;
|
241
|
return importResult;
|
242
|
}
|
243
|
|
244
|
/**
|
245
|
* Returns the name of a component bean. If the name is defined in the Component annotation this name is returned.
|
246
|
* Otherwise the class name is returned with starting lower case.
|
247
|
* @param ioClass
|
248
|
* @return
|
249
|
* @throws IllegalArgumentException if the class does not have a "Component" annotation
|
250
|
*/
|
251
|
public static String getComponentBeanName(Class<ICdmImport> ioClass) throws IllegalArgumentException {
|
252
|
Component component = ioClass.getAnnotation(Component.class);
|
253
|
if (component == null){
|
254
|
throw new IllegalArgumentException("Class " + ioClass.getName() + " is missing a @Component annotation." );
|
255
|
}
|
256
|
String ioBean = component.value();
|
257
|
if ("".equals(ioBean)){
|
258
|
ioBean = ioClass.getSimpleName();
|
259
|
ioBean = ioBean.substring(0, 1).toLowerCase() + ioBean.substring(1); //make camelcase
|
260
|
}
|
261
|
return ioBean;
|
262
|
}
|
263
|
|
264
|
public void authenticate(IImportConfigurator config) {
|
265
|
UsernamePasswordAuthenticationToken token = config.getAuthenticationToken();
|
266
|
if (token != null){
|
267
|
SecurityContext context = SecurityContextHolder.getContext();
|
268
|
|
269
|
AuthenticationManager authenticationManager = applicationContext.getBean("authenticationManager", AuthenticationManager.class);;
|
270
|
Authentication authentication = authenticationManager.authenticate(token);
|
271
|
context.setAuthentication(authentication);
|
272
|
}else{
|
273
|
logger.warn("No authentication token available");
|
274
|
}
|
275
|
|
276
|
}
|
277
|
|
278
|
}
|