Revision 532683bd
Added by Patrick Plitzner almost 12 years ago
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/common/CdmApplicationAwareDefaultImport.java | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* Copyright (C) 2007 EDIT |
3 |
* European Distributed Institute of Taxonomy
|
|
3 |
* European Distributed Institute of Taxonomy |
|
4 | 4 |
* http://www.e-taxonomy.eu |
5 |
*
|
|
5 |
* |
|
6 | 6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
7 | 7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
8 | 8 |
*/ |
... | ... | |
42 | 42 |
|
43 | 43 |
@Component("defaultImport") |
44 | 44 |
public class CdmApplicationAwareDefaultImport<T extends IImportConfigurator> implements ICdmImporter<T>, ApplicationContextAware { |
45 |
private static final Logger logger = Logger.getLogger(CdmApplicationAwareDefaultImport.class); |
|
46 |
|
|
47 |
protected ApplicationContext applicationContext; |
|
48 |
|
|
49 |
/* (non-Javadoc) |
|
50 |
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) |
|
51 |
*/ |
|
52 |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
|
53 |
this.applicationContext = applicationContext; |
|
54 |
} |
|
55 |
|
|
56 |
|
|
57 |
//Constants |
|
58 |
final boolean OBLIGATORY = true; |
|
59 |
final boolean FACULTATIVE = false; |
|
60 |
final int modCount = 1000; |
|
61 |
|
|
62 |
IService<CdmBase> service = null; |
|
63 |
|
|
64 |
//different type of stores that are used by the known imports |
|
65 |
Map<String, MapWrapper<? extends CdmBase>> stores = new HashMap<String, MapWrapper<? extends CdmBase>>(); |
|
66 |
|
|
67 |
public CdmApplicationAwareDefaultImport(){ |
|
68 |
|
|
69 |
|
|
70 |
stores.put(ICdmIO.PERSON_STORE, new MapWrapper<Person>(service)); |
|
71 |
stores.put(ICdmIO.TEAM_STORE, new MapWrapper<TeamOrPersonBase<?>>(service)); |
|
72 |
stores.put(ICdmIO.REFERENCE_STORE, new MapWrapper<Reference>(service)); |
|
73 |
stores.put(ICdmIO.NOMREF_STORE, new MapWrapper<Reference>(service)); |
|
74 |
stores.put(ICdmIO.NOMREF_DETAIL_STORE, new MapWrapper<Reference>(service)); |
|
75 |
stores.put(ICdmIO.REF_DETAIL_STORE, new MapWrapper<Reference>(service)); |
|
76 |
stores.put(ICdmIO.TAXONNAME_STORE, new MapWrapper<TaxonNameBase<?,?>>(service)); |
|
77 |
stores.put(ICdmIO.TAXON_STORE, new MapWrapper<TaxonBase>(service)); |
|
78 |
stores.put(ICdmIO.SPECIMEN_STORE, new MapWrapper<Specimen>(service)); |
|
79 |
} |
|
80 |
|
|
81 |
public boolean invoke(IImportConfigurator config){ |
|
82 |
if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_ONLY)){ |
|
83 |
return doCheck(config); |
|
84 |
}else if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_AND_IMPORT)){ |
|
85 |
doCheck(config); |
|
86 |
return doImport(config); |
|
87 |
}else if (config.getCheck().equals(IImportConfigurator.CHECK.IMPORT_WITHOUT_CHECK)){ |
|
88 |
return doImport(config); |
|
89 |
}else{ |
|
90 |
logger.error("Unknown CHECK type"); |
|
91 |
return false; |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
|
|
96 |
@SuppressWarnings("unchecked") |
|
97 |
protected <S extends IImportConfigurator> boolean doCheck(S config){ |
|
98 |
boolean result = true; |
|
99 |
|
|
100 |
//check |
|
101 |
if (config == null){ |
|
102 |
logger.warn("CdmImportConfiguration is null"); |
|
103 |
return false; |
|
104 |
} |
|
105 |
System.out.println("Start checking Source ("+ config.getSourceNameString() + ") ..."); |
|
106 |
if (! config.isValid()){ |
|
107 |
logger.warn("CdmImportConfiguration is not valid"); |
|
108 |
return false; |
|
109 |
} |
|
110 |
|
|
111 |
ImportStateBase state = config.getNewState(); |
|
112 |
state.initialize(config); |
|
113 |
|
|
114 |
//do check for each class |
|
115 |
for (Class<ICdmIO> ioClass: config.getIoClassList()){ |
|
116 |
try { |
|
117 |
String ioBeanName = getComponentBeanName(ioClass); |
|
118 |
ICdmIO cdmIo = (ICdmIO)applicationContext.getBean(ioBeanName, ICdmIO.class); |
|
119 |
if (cdmIo != null){ |
|
120 |
registerObservers(config, cdmIo); |
|
121 |
state.setCurrentIO(cdmIo); |
|
122 |
result &= cdmIo.check(state); |
|
123 |
unRegisterObservers(config, cdmIo); |
|
124 |
}else{ |
|
125 |
logger.error("cdmIO was null"); |
|
126 |
result = false; |
|
127 |
} |
|
128 |
} catch (Exception e) { |
|
129 |
logger.error(e); |
|
130 |
e.printStackTrace(); |
|
131 |
result = false; |
|
132 |
} |
|
133 |
} |
|
134 |
|
|
135 |
//return |
|
136 |
System.out.println("End checking Source ("+ config.getSourceNameString() + ") for import to Cdm"); |
|
137 |
return result; |
|
138 |
|
|
139 |
} |
|
140 |
|
|
141 |
private void registerObservers(IImportConfigurator config, ICdmIO io){ |
|
142 |
for (IIoObserver observer : config.getObservers()){ |
|
143 |
io.addObserver(observer); |
|
144 |
} |
|
145 |
} |
|
146 |
|
|
147 |
private void unRegisterObservers(IImportConfigurator config, ICdmIO io){ |
|
148 |
for (IIoObserver observer : config.getObservers()){ |
|
149 |
io.removeObserver(observer); |
|
150 |
} |
|
151 |
} |
|
152 |
|
|
153 |
|
|
154 |
/** |
|
155 |
* Executes the whole |
|
156 |
*/ |
|
157 |
protected <S extends IImportConfigurator> boolean doImport(S config){ |
|
158 |
boolean result = true; |
|
159 |
//validate |
|
160 |
if (config == null){ |
|
161 |
logger.warn("Configuration is null"); |
|
162 |
return false; |
|
163 |
}else if (! config.isValid()){ |
|
164 |
logger.warn("Configuration is not valid"); |
|
165 |
return false; |
|
166 |
} |
|
167 |
|
|
168 |
config.getSourceReference(); |
|
169 |
logger.info("Start import from Source '"+ config.getSourceNameString() + "' to destination '" + config.getDestinationNameString() + "'"); |
|
170 |
|
|
171 |
ImportStateBase state = config.getNewState(); |
|
172 |
state.initialize(config); |
|
173 |
|
|
174 |
CdmPermissionEvaluator permissionEval = applicationContext.getBean("cdmPermissionEvaluator", CdmPermissionEvaluator.class); |
|
175 |
|
|
176 |
state.setSuccess(true); |
|
177 |
//do invoke for each class |
|
178 |
for (Class<ICdmIO> ioClass: config.getIoClassList()){ |
|
179 |
try { |
|
180 |
String ioBeanName = getComponentBeanName(ioClass); |
|
181 |
ICdmIO cdmIo = (ICdmIO)applicationContext.getBean(ioBeanName, ICdmIO.class); |
|
182 |
if (cdmIo != null){ |
|
183 |
registerObservers(config, cdmIo); |
|
184 |
state.setCurrentIO(cdmIo); |
|
185 |
result &= cdmIo.invoke(state); |
|
186 |
unRegisterObservers(config, cdmIo); |
|
187 |
}else{ |
|
188 |
logger.error("cdmIO was null"); |
|
189 |
result = false; |
|
190 |
} |
|
191 |
} catch (Exception e) { |
|
192 |
logger.error(e); |
|
193 |
e.printStackTrace(); |
|
194 |
result = false; |
|
195 |
} |
|
196 |
} |
|
197 |
|
|
198 |
//do invoke for each class |
|
45 |
private static final Logger logger = Logger.getLogger(CdmApplicationAwareDefaultImport.class); |
|
46 |
|
|
47 |
protected ApplicationContext applicationContext; |
|
48 |
|
|
49 |
/* (non-Javadoc) |
|
50 |
* @see org.springframework.context.ApplicationContextAware#setApplicationContext(org.springframework.context.ApplicationContext) |
|
51 |
*/ |
|
52 |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
|
53 |
this.applicationContext = applicationContext; |
|
54 |
} |
|
55 |
|
|
56 |
|
|
57 |
//Constants |
|
58 |
final boolean OBLIGATORY = true; |
|
59 |
final boolean FACULTATIVE = false; |
|
60 |
final int modCount = 1000; |
|
61 |
|
|
62 |
IService<CdmBase> service = null; |
|
63 |
|
|
64 |
//different type of stores that are used by the known imports |
|
65 |
Map<String, MapWrapper<? extends CdmBase>> stores = new HashMap<String, MapWrapper<? extends CdmBase>>(); |
|
66 |
|
|
67 |
public CdmApplicationAwareDefaultImport(){ |
|
68 |
|
|
69 |
|
|
70 |
stores.put(ICdmIO.PERSON_STORE, new MapWrapper<Person>(service)); |
|
71 |
stores.put(ICdmIO.TEAM_STORE, new MapWrapper<TeamOrPersonBase<?>>(service)); |
|
72 |
stores.put(ICdmIO.REFERENCE_STORE, new MapWrapper<Reference>(service)); |
|
73 |
stores.put(ICdmIO.NOMREF_STORE, new MapWrapper<Reference>(service)); |
|
74 |
stores.put(ICdmIO.NOMREF_DETAIL_STORE, new MapWrapper<Reference>(service)); |
|
75 |
stores.put(ICdmIO.REF_DETAIL_STORE, new MapWrapper<Reference>(service)); |
|
76 |
stores.put(ICdmIO.TAXONNAME_STORE, new MapWrapper<TaxonNameBase<?,?>>(service)); |
|
77 |
stores.put(ICdmIO.TAXON_STORE, new MapWrapper<TaxonBase>(service)); |
|
78 |
stores.put(ICdmIO.SPECIMEN_STORE, new MapWrapper<Specimen>(service)); |
|
79 |
} |
|
80 |
|
|
81 |
/* (non-Javadoc) |
|
82 |
* @see eu.etaxonomy.cdm.io.common.ICdmImporter#invoke(eu.etaxonomy.cdm.io.common.IImportConfigurator) |
|
83 |
*/ |
|
84 |
@Override |
|
85 |
public boolean invoke(IImportConfigurator config){ |
|
86 |
if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_ONLY)){ |
|
87 |
return doCheck(config); |
|
88 |
}else if (config.getCheck().equals(IImportConfigurator.CHECK.CHECK_AND_IMPORT)){ |
|
89 |
doCheck(config); |
|
90 |
return doImport(config); |
|
91 |
}else if (config.getCheck().equals(IImportConfigurator.CHECK.IMPORT_WITHOUT_CHECK)){ |
|
92 |
return doImport(config); |
|
93 |
}else{ |
|
94 |
logger.error("Unknown CHECK type"); |
|
95 |
return false; |
|
96 |
} |
|
97 |
} |
|
98 |
|
|
99 |
|
|
100 |
@SuppressWarnings("unchecked") |
|
101 |
protected <S extends IImportConfigurator> boolean doCheck(S config){ |
|
102 |
boolean result = true; |
|
103 |
|
|
104 |
//check |
|
105 |
if (config == null){ |
|
106 |
logger.warn("CdmImportConfiguration is null"); |
|
107 |
return false; |
|
108 |
} |
|
109 |
System.out.println("Start checking Source ("+ config.getSourceNameString() + ") ..."); |
|
110 |
if (! config.isValid()){ |
|
111 |
logger.warn("CdmImportConfiguration is not valid"); |
|
112 |
return false; |
|
113 |
} |
|
114 |
|
|
115 |
ImportStateBase state = config.getNewState(); |
|
116 |
state.initialize(config); |
|
117 |
|
|
118 |
//do check for each class |
|
119 |
for (Class<ICdmIO> ioClass: config.getIoClassList()){ |
|
120 |
try { |
|
121 |
String ioBeanName = getComponentBeanName(ioClass); |
|
122 |
ICdmIO cdmIo = (ICdmIO)applicationContext.getBean(ioBeanName, ICdmIO.class); |
|
123 |
if (cdmIo != null){ |
|
124 |
registerObservers(config, cdmIo); |
|
125 |
state.setCurrentIO(cdmIo); |
|
126 |
result &= cdmIo.check(state); |
|
127 |
unRegisterObservers(config, cdmIo); |
|
128 |
}else{ |
|
129 |
logger.error("cdmIO was null"); |
|
130 |
result = false; |
|
131 |
} |
|
132 |
} catch (Exception e) { |
|
133 |
logger.error(e); |
|
134 |
e.printStackTrace(); |
|
135 |
result = false; |
|
136 |
} |
|
137 |
} |
|
138 |
|
|
139 |
//return |
|
140 |
System.out.println("End checking Source ("+ config.getSourceNameString() + ") for import to Cdm"); |
|
141 |
return result; |
|
142 |
|
|
143 |
} |
|
144 |
|
|
145 |
private void registerObservers(IImportConfigurator config, ICdmIO io){ |
|
146 |
for (IIoObserver observer : config.getObservers()){ |
|
147 |
io.addObserver(observer); |
|
148 |
} |
|
149 |
} |
|
150 |
|
|
151 |
private void unRegisterObservers(IImportConfigurator config, ICdmIO io){ |
|
152 |
for (IIoObserver observer : config.getObservers()){ |
|
153 |
io.removeObserver(observer); |
|
154 |
} |
|
155 |
} |
|
156 |
|
|
157 |
|
|
158 |
/** |
|
159 |
* Executes the whole |
|
160 |
*/ |
|
161 |
protected <S extends IImportConfigurator> boolean doImport(S config){ |
|
162 |
boolean result = true; |
|
163 |
//validate |
|
164 |
if (config == null){ |
|
165 |
logger.warn("Configuration is null"); |
|
166 |
return false; |
|
167 |
}else if (! config.isValid()){ |
|
168 |
logger.warn("Configuration is not valid"); |
|
169 |
return false; |
|
170 |
} |
|
171 |
|
|
172 |
config.getSourceReference(); |
|
173 |
logger.info("Start import from Source '"+ config.getSourceNameString() + "' to destination '" + config.getDestinationNameString() + "'"); |
|
174 |
|
|
175 |
ImportStateBase state = config.getNewState(); |
|
176 |
state.initialize(config); |
|
177 |
|
|
178 |
CdmPermissionEvaluator permissionEval = applicationContext.getBean("cdmPermissionEvaluator", CdmPermissionEvaluator.class); |
|
179 |
|
|
180 |
state.setSuccess(true); |
|
181 |
//do invoke for each class |
|
182 |
for (Class<ICdmIO> ioClass: config.getIoClassList()){ |
|
183 |
try { |
|
184 |
String ioBeanName = getComponentBeanName(ioClass); |
|
185 |
ICdmIO cdmIo = (ICdmIO)applicationContext.getBean(ioBeanName, ICdmIO.class); |
|
186 |
if (cdmIo != null){ |
|
187 |
registerObservers(config, cdmIo); |
|
188 |
state.setCurrentIO(cdmIo); |
|
189 |
result &= cdmIo.invoke(state); |
|
190 |
unRegisterObservers(config, cdmIo); |
|
191 |
}else{ |
|
192 |
logger.error("cdmIO was null"); |
|
193 |
result = false; |
|
194 |
} |
|
195 |
} catch (Exception e) { |
|
196 |
logger.error(e); |
|
197 |
e.printStackTrace(); |
|
198 |
result = false; |
|
199 |
} |
|
200 |
} |
|
201 |
|
|
202 |
//do invoke for each class |
|
199 | 203 |
// for (String ioBean: config.getIoBeans()){ |
200 | 204 |
// try { |
201 | 205 |
// ICdmIO<S> cdmIo = (ICdmIO<S>)applicationContext.getBean(ioBean, ICdmIO.class); |
... | ... | |
210 | 214 |
// e.printStackTrace(); |
211 | 215 |
// result = false; |
212 | 216 |
// } |
213 |
//
|
|
217 |
// |
|
214 | 218 |
// } |
215 |
|
|
216 |
logger.info("End import from source '" + config.getSourceNameString()
|
|
217 |
+ "' to destination '" + config.getDestinationNameString() + "'"+
|
|
218 |
(result? "(successful)":"(with errors)")) ;
|
|
219 |
return result;
|
|
220 |
}
|
|
221 |
|
|
222 |
/**
|
|
223 |
* Returns the name of a component bean. If the name is defined in the Component annotation this name is returned.
|
|
224 |
* Otherwise the class name is returned with starting lower case.
|
|
225 |
* @param ioClass
|
|
226 |
* @return
|
|
227 |
* @throws IllegalArgumentException if the class does not have a "Component" annotation
|
|
228 |
*/
|
|
229 |
public static String getComponentBeanName(Class<ICdmIO> ioClass) throws IllegalArgumentException {
|
|
230 |
Component component = ioClass.getAnnotation(Component.class);
|
|
231 |
if (component == null){
|
|
232 |
throw new IllegalArgumentException("Class " + ioClass.getName() + " is missing a @Component annotation." );
|
|
233 |
}
|
|
234 |
String ioBean = component.value();
|
|
235 |
if ("".equals(ioBean)){
|
|
236 |
ioBean = ioClass.getSimpleName();
|
|
237 |
ioBean = ioBean.substring(0, 1).toLowerCase() + ioBean.substring(1); //make camelcase
|
|
238 |
}
|
|
239 |
return ioBean;
|
|
240 |
}
|
|
241 |
|
|
242 |
public void authenticate(IImportConfigurator config) {
|
|
243 |
UsernamePasswordAuthenticationToken token = config.getAuthenticationToken();
|
|
244 |
if (token != null){
|
|
245 |
SecurityContext context = SecurityContextHolder.getContext();
|
|
246 |
|
|
247 |
AuthenticationManager authenticationManager = applicationContext.getBean("authenticationManager", AuthenticationManager.class);;
|
|
248 |
Authentication authentication = authenticationManager.authenticate(token);
|
|
249 |
context.setAuthentication(authentication);
|
|
250 |
}
|
|
251 |
|
|
252 |
}
|
|
219 |
|
|
220 |
logger.info("End import from source '" + config.getSourceNameString()
|
|
221 |
+ "' to destination '" + config.getDestinationNameString() + "'"+
|
|
222 |
(result? "(successful)":"(with errors)")) ;
|
|
223 |
return result;
|
|
224 |
}
|
|
225 |
|
|
226 |
/**
|
|
227 |
* Returns the name of a component bean. If the name is defined in the Component annotation this name is returned.
|
|
228 |
* Otherwise the class name is returned with starting lower case.
|
|
229 |
* @param ioClass
|
|
230 |
* @return
|
|
231 |
* @throws IllegalArgumentException if the class does not have a "Component" annotation
|
|
232 |
*/
|
|
233 |
public static String getComponentBeanName(Class<ICdmIO> ioClass) throws IllegalArgumentException {
|
|
234 |
Component component = ioClass.getAnnotation(Component.class);
|
|
235 |
if (component == null){
|
|
236 |
throw new IllegalArgumentException("Class " + ioClass.getName() + " is missing a @Component annotation." );
|
|
237 |
}
|
|
238 |
String ioBean = component.value();
|
|
239 |
if ("".equals(ioBean)){
|
|
240 |
ioBean = ioClass.getSimpleName();
|
|
241 |
ioBean = ioBean.substring(0, 1).toLowerCase() + ioBean.substring(1); //make camelcase
|
|
242 |
}
|
|
243 |
return ioBean;
|
|
244 |
}
|
|
245 |
|
|
246 |
public void authenticate(IImportConfigurator config) {
|
|
247 |
UsernamePasswordAuthenticationToken token = config.getAuthenticationToken();
|
|
248 |
if (token != null){
|
|
249 |
SecurityContext context = SecurityContextHolder.getContext();
|
|
250 |
|
|
251 |
AuthenticationManager authenticationManager = applicationContext.getBean("authenticationManager", AuthenticationManager.class);;
|
|
252 |
Authentication authentication = authenticationManager.authenticate(token);
|
|
253 |
context.setAuthentication(authentication);
|
|
254 |
}
|
|
255 |
|
|
256 |
}
|
|
253 | 257 |
|
254 | 258 |
} |
Also available in: Unified diff
Merged latest trunk updates to branch