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