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.io.common.events.IIoObserver;
|
23
|
import eu.etaxonomy.cdm.model.agent.Person;
|
24
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
25
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
26
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
27
|
import eu.etaxonomy.cdm.model.occurrence.Specimen;
|
28
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
29
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
30
|
|
31
|
/**
|
32
|
* @author a.mueller
|
33
|
* @created 20.06.2008
|
34
|
* @version 1.0
|
35
|
*/
|
36
|
|
37
|
@Component("defaultImport")
|
38
|
public class CdmApplicationAwareDefaultImport<T extends IImportConfigurator> implements ICdmImporter<T>, ApplicationContextAware {
|
39
|
private static final Logger logger = Logger.getLogger(CdmApplicationAwareDefaultImport.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) throws BeansException {
|
47
|
this.applicationContext = applicationContext;
|
48
|
}
|
49
|
|
50
|
|
51
|
//Constants
|
52
|
final boolean OBLIGATORY = true;
|
53
|
final boolean FACULTATIVE = false;
|
54
|
final int modCount = 1000;
|
55
|
|
56
|
IService<CdmBase> service = null;
|
57
|
|
58
|
//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 CdmApplicationAwareDefaultImport(){
|
62
|
|
63
|
|
64
|
stores.put(ICdmIO.PERSON_STORE, new MapWrapper<Person>(service));
|
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
|
public boolean invoke(IImportConfigurator config){
|
76
|
if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_ONLY)){
|
77
|
return doCheck(config);
|
78
|
}else if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_AND_IMPORT)){
|
79
|
doCheck(config);
|
80
|
return doImport(config);
|
81
|
}else if (config.getCheck().equals(IImportConfigurator.CHECK.IMPORT_WITHOUT_CHECK)){
|
82
|
return doImport(config);
|
83
|
}else{
|
84
|
logger.error("Unknown CHECK type");
|
85
|
return false;
|
86
|
}
|
87
|
}
|
88
|
|
89
|
|
90
|
@SuppressWarnings("unchecked")
|
91
|
protected <S extends IImportConfigurator> boolean doCheck(S config){
|
92
|
boolean result = true;
|
93
|
|
94
|
//check
|
95
|
if (config == null){
|
96
|
logger.warn("CdmImportConfiguration is null");
|
97
|
return false;
|
98
|
}
|
99
|
System.out.println("Start checking Source ("+ config.getSourceNameString() + ") ...");
|
100
|
if (! config.isValid()){
|
101
|
logger.warn("CdmImportConfiguration is not valid");
|
102
|
return false;
|
103
|
}
|
104
|
|
105
|
ImportStateBase state = config.getNewState();
|
106
|
state.initialize(config);
|
107
|
|
108
|
//do check for each class
|
109
|
for (Class<ICdmIO> ioClass: config.getIoClassList()){
|
110
|
try {
|
111
|
String ioBeanName = getComponentBeanName(ioClass);
|
112
|
ICdmIO cdmIo = (ICdmIO)applicationContext.getBean(ioBeanName, ICdmIO.class);
|
113
|
if (cdmIo != null){
|
114
|
registerObservers(config, cdmIo);
|
115
|
state.setCurrentIO(cdmIo);
|
116
|
result &= cdmIo.check(state);
|
117
|
unRegisterObservers(config, cdmIo);
|
118
|
}else{
|
119
|
logger.error("cdmIO was null");
|
120
|
result = false;
|
121
|
}
|
122
|
} catch (Exception e) {
|
123
|
logger.error(e);
|
124
|
e.printStackTrace();
|
125
|
result = false;
|
126
|
}
|
127
|
}
|
128
|
|
129
|
//return
|
130
|
System.out.println("End checking Source ("+ config.getSourceNameString() + ") for import to Cdm");
|
131
|
return result;
|
132
|
|
133
|
}
|
134
|
|
135
|
private void registerObservers(IImportConfigurator config, ICdmIO io){
|
136
|
for (IIoObserver observer : config.getObservers()){
|
137
|
io.addObserver(observer);
|
138
|
}
|
139
|
}
|
140
|
|
141
|
private void unRegisterObservers(IImportConfigurator 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 <S extends IImportConfigurator> boolean doImport(S 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
|
Reference sourceReference = config.getSourceReference();
|
163
|
logger.info("Start import from Source '"+ config.getSourceNameString() + "' to destination '" + config.getDestinationNameString() + "'");
|
164
|
|
165
|
|
166
|
ImportStateBase state = config.getNewState();
|
167
|
state.initialize(config);
|
168
|
|
169
|
state.setSuccess(true);
|
170
|
//do invoke for each class
|
171
|
for (Class<ICdmIO> ioClass: config.getIoClassList()){
|
172
|
try {
|
173
|
String ioBeanName = getComponentBeanName(ioClass);
|
174
|
ICdmIO cdmIo = (ICdmIO)applicationContext.getBean(ioBeanName, ICdmIO.class);
|
175
|
if (cdmIo != null){
|
176
|
registerObservers(config, cdmIo);
|
177
|
state.setCurrentIO(cdmIo);
|
178
|
result &= cdmIo.invoke(state);
|
179
|
unRegisterObservers(config, cdmIo);
|
180
|
}else{
|
181
|
logger.error("cdmIO was null");
|
182
|
result = false;
|
183
|
}
|
184
|
} catch (Exception e) {
|
185
|
logger.error(e);
|
186
|
e.printStackTrace();
|
187
|
result = false;
|
188
|
}
|
189
|
}
|
190
|
|
191
|
//do invoke for each class
|
192
|
// for (String ioBean: config.getIoBeans()){
|
193
|
// try {
|
194
|
// ICdmIO<S> cdmIo = (ICdmIO<S>)applicationContext.getBean(ioBean, ICdmIO.class);
|
195
|
// if (cdmIo != null){
|
196
|
// result &= cdmIo.invoke(config, stores);
|
197
|
// }else{
|
198
|
// logger.error("cdmIO was null");
|
199
|
// result = false;
|
200
|
// }
|
201
|
// } catch (Exception e) {
|
202
|
// logger.error(e);
|
203
|
// e.printStackTrace();
|
204
|
// result = false;
|
205
|
// }
|
206
|
//
|
207
|
// }
|
208
|
|
209
|
logger.info("End import from source '" + config.getSourceNameString()
|
210
|
+ "' to destination '" + config.getDestinationNameString() + "'"+
|
211
|
(result? "(successful)":"(with errors)")) ;
|
212
|
return result;
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* Returns the name of a component bean. If the name is defined in the Component annotation this name is returned.
|
217
|
* Otherwise the class name is returned with starting lower case.
|
218
|
* @param ioClass
|
219
|
* @return
|
220
|
* @throws IllegalArgumentException if the class does not have a "Component" annotation
|
221
|
*/
|
222
|
public static String getComponentBeanName(Class<ICdmIO> ioClass) throws IllegalArgumentException {
|
223
|
Component component = ioClass.getAnnotation(Component.class);
|
224
|
if (component == null){
|
225
|
throw new IllegalArgumentException("Class " + ioClass.getName() + " is missing a @Component annotation." );
|
226
|
}
|
227
|
String ioBean = component.value();
|
228
|
if ("".equals(ioBean)){
|
229
|
ioBean = ioClass.getSimpleName();
|
230
|
ioBean = ioBean.substring(0, 1).toLowerCase() + ioBean.substring(1); //make camelcase
|
231
|
}
|
232
|
return ioBean;
|
233
|
}
|
234
|
|
235
|
}
|