merge trunk to branch cdm-3.3
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / ImportConfiguratorBase.java
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.lang.reflect.Method;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.UUID;
17
18 import org.apache.log4j.Logger;
19 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
20 import org.springframework.stereotype.Component;
21
22 import eu.etaxonomy.cdm.database.DbSchemaValidation;
23 import eu.etaxonomy.cdm.database.ICdmDataSource;
24 import eu.etaxonomy.cdm.io.common.mapping.IInputTransformer;
25 import eu.etaxonomy.cdm.model.agent.Person;
26 import eu.etaxonomy.cdm.model.description.Feature;
27 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
28 import eu.etaxonomy.cdm.model.reference.Reference;
29
30 /**
31 * @author a.mueller
32 * @created 20.06.2008
33 * @param <STATE>
34 */
35 @Component
36 public abstract class ImportConfiguratorBase<STATE extends ImportStateBase, SOURCE> extends IoConfiguratorBase implements IImportConfigurator{
37 private static final Logger logger = Logger.getLogger(ImportConfiguratorBase.class);
38
39 //check
40 private CHECK check = CHECK.CHECK_AND_IMPORT;
41
42 //editor
43 static EDITOR editor = EDITOR.EDITOR_AS_ANNOTATION;
44
45 /**
46 * The transformer class to be used for Input
47 */
48 private IInputTransformer transformer;
49
50 //
51 // //TODO
52 // private boolean deleteAll = false;
53
54 //nullValues
55 private boolean ignoreNull = false;
56
57 //Nomenclatural Code
58 private NomenclaturalCode nomenclaturalCode = null;
59
60 private Map<Integer, Feature> featureMap = new HashMap<Integer, Feature>();
61
62 /* The classification name for the first classification.
63 * Needs only to be defined if the import does not handle the naming
64 * itself (e.g. by using the taxon sec. reference title cache)
65 */
66 private String classificationName = "Classification - no name";
67
68 private UUID classificationUuid = UUID.randomUUID();
69 //uuid of concept reference
70 private UUID secUuid = UUID.randomUUID();
71
72 private Object sourceSecId = -1;
73
74 private SOURCE source;
75 protected Reference<?> sourceReference;
76 private UUID sourceRefUuid;
77 private ICdmDataSource destination;
78 private Person commentator = Person.NewTitledInstance("automatic CDM importer");
79
80 protected Class<ICdmIO>[] ioClassList;
81
82 protected ICdmIO[] ioList;
83
84 protected String[] ioBeans;
85
86 /*user interaction*/
87 private boolean askUserForHelp =false;
88
89
90 /* *****************CONSTRUCTOR *****************************/
91
92 public ImportConfiguratorBase(IInputTransformer transformer){
93 super();
94 setDbSchemaValidation(DbSchemaValidation.UPDATE);
95 this.transformer = transformer;
96
97 }
98
99 abstract protected void makeIoClassList();
100
101 /* (non-Javadoc)
102 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getTransformer()
103 */
104 @Override
105 public IInputTransformer getTransformer() {
106 return this.transformer;
107 }
108
109 /* (non-Javadoc)
110 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#setTransformer(eu.etaxonomy.cdm.io.common.mapping.IInputTransformer)
111 */
112 @Override
113 public void setTransformer(IInputTransformer transformer){
114 this.transformer = transformer;
115 }
116
117
118
119
120 /**
121 * @param source the source to set
122 */
123 public void setSource(SOURCE source) {
124 this.source = source;
125 }
126
127
128 /**
129 * @param source the source to get
130 */
131 @Override
132 public SOURCE getSource() {
133 return source;
134 }
135
136
137
138 /* (non-Javadoc)
139 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#isValid()
140 */
141 @Override
142 public boolean isValid(){
143 boolean result = true;
144 if (source == null){
145 logger.warn("Connection to source could not be established");
146 result = false;
147 }
148 //Not valid any more as the importer may already have a destination
149 // if (destination == null ){
150 // logger.warn("Connection to Cdm could not be established");
151 // result = false;
152 // }
153
154 return result;
155 }
156
157
158
159 /* ****************** GETTER/SETTER **************************/
160
161 // /**
162 // * @return the state
163 // */
164 // public STATE getState() {
165 // return state;
166 // }
167 //
168 // /**
169 // * @param state the state to set
170 // */
171 // public void setState(STATE state) {
172 // this.state = state;
173 // }
174
175 public void setIoClassList(ICdmIO[] ioList){
176 this.ioList = ioList;
177 }
178
179 @Override
180 public Class<ICdmIO>[] getIoClassList(){
181 if (ioClassList == null){
182 makeIoClassList();
183 }
184 return ioClassList;
185 }
186
187 /**
188 * @param ioClassList
189 */
190 public void setIoClassList(Class<ICdmIO>[] ioClassList){
191 this.ioClassList = ioClassList;
192 }
193
194
195 /* (non-Javadoc)
196 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#getCheck()
197 */
198 @Override
199 public CHECK getCheck() {
200 return this.check;
201 }
202
203 /* (non-Javadoc)
204 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#setCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK)
205 */
206 @Override
207 public void setCheck(CHECK check) {
208 this.check = check;
209 }
210
211
212 /**
213 * @return the editor
214 */
215 @Override
216 public EDITOR getEditor() {
217 return editor;
218 }
219
220 /**
221 * @param editor the editor to set
222 */
223 @Override
224 public void setEditor(EDITOR editor) {
225 ImportConfiguratorBase.editor = editor;
226 }
227
228 /**
229 * If true, no errors occur if objects are not found that should exist. This may
230 * be needed e.g. when only subsets of the data are imported.
231 * Default value is <cod>false</code>.
232 * @return the ignoreNull
233 */
234 @Override
235 public boolean isIgnoreNull() {
236 return ignoreNull;
237 }
238
239 /**
240 * @param ignoreNull the ignoreNull to set
241 */
242 @Override
243 public void setIgnoreNull(boolean ignoreNull) {
244 this.ignoreNull = ignoreNull;
245 }
246
247 /* (non-Javadoc)
248 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#getDestination()
249 */
250 @Override
251 public ICdmDataSource getDestination() {
252 return destination;
253 }
254 /* (non-Javadoc)
255 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#setDestination(eu.etaxonomy.cdm.database.ICdmDataSource)
256 */
257 @Override
258 public void setDestination(ICdmDataSource destination) {
259 this.destination = destination;
260 }
261
262
263 /* (non-Javadoc)
264 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#getSourceReference()
265 */
266 @Override
267 public abstract Reference getSourceReference();
268 /* (non-Javadoc)
269 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#setSourceReference(eu.etaxonomy.cdm.model.reference.Reference)
270 */
271 @Override
272 public void setSourceReference(Reference sourceReference) {
273 this.sourceReference = sourceReference;
274 }
275 /* (non-Javadoc)
276 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#getSourceReferenceTitle()
277 */
278 @Override
279 public String getSourceReferenceTitle() {
280 return getSourceReference().getTitleCache();
281 }
282 /* (non-Javadoc)
283 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#setSourceReferenceTitle(java.lang.String)
284 */
285 @Override
286 public void setSourceReferenceTitle(String sourceReferenceTitle) {
287 getSourceReference().setTitleCache(sourceReferenceTitle, true);
288 }
289
290
291 /* (non-Javadoc)
292 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#getCommentator()
293 */
294 @Override
295 public Person getCommentator() {
296 return commentator;
297 }
298
299 /* (non-Javadoc)
300 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#setCommentator(eu.etaxonomy.cdm.model.agent.Person)
301 */
302 @Override
303 public void setCommentator(Person commentator) {
304 this.commentator = commentator;
305 }
306
307 /**
308 * @return the nomenclaturalCode
309 */
310 @Override
311 public NomenclaturalCode getNomenclaturalCode() {
312 return nomenclaturalCode;
313 }
314
315
316 /**
317 * @param nomenclaturalCode the nomenclaturalCode to set
318 */
319 @Override
320 public void setNomenclaturalCode(NomenclaturalCode nomenclaturalCode) {
321 this.nomenclaturalCode = nomenclaturalCode;
322 }
323
324
325 /* (non-Javadoc)
326 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getTreeUuid()
327 */
328 @Override
329 public UUID getClassificationUuid() {
330 return classificationUuid;
331 }
332
333
334 @Override
335 public void setClassificationUuid(UUID classificationUuid) {
336 this.classificationUuid = classificationUuid;
337 }
338
339 /* (non-Javadoc)
340 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getSecUuid()
341 */
342 @Override
343 public UUID getSecUuid() {
344 return secUuid;
345 }
346 @Override
347 public void setSecUuid(UUID secUuid) {
348 this.secUuid = secUuid;
349 }
350
351 /**
352 * @return the sourceSecId
353 */
354 @Override
355 public Object getSourceSecId() {
356 return sourceSecId;
357 }
358
359 /**
360 * @param sourceSecId the sourceSecId to set
361 */
362 public void setSourceSecId(Object sourceSecId) {
363 this.sourceSecId = sourceSecId;
364 }
365
366
367 /**
368 * @return the featureMap
369 */
370 public Map<Integer, Feature> getFeatureMap() {
371 return featureMap;
372 }
373
374 /**
375 * @param featureMap the featureMap to set
376 */
377 public void setFeatureMap(Map<Integer, Feature> featureMap) {
378 this.featureMap = featureMap;
379 }
380
381
382 protected static Method getDefaultFunction(Class<?> clazz, String methodName){
383 try {
384 return clazz.getMethod(methodName, List.class) ;
385 } catch (SecurityException e) {
386 logger.error(e.getMessage());
387 e.printStackTrace();
388 } catch (NoSuchMethodException e) {
389 logger.error(e.getMessage());
390 e.printStackTrace();
391 }
392 return null;
393 }
394
395
396 /* (non-Javadoc)
397 * @see eu.etaxonomy.cdm.io.common.IIoConfigurator#getDestinationNameString()
398 */
399 @Override
400 public String getDestinationNameString() {
401 if (this.getDestination() == null) {
402 return null;
403 } else {
404 return this.getDestination().getName().toString();
405 }
406 }
407
408 /* (non-Javadoc)
409 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getSourceNameString()
410 */
411 @Override
412 public String getSourceNameString() {
413 if (this.getSource() == null){
414 return null;
415 }else{
416 return this.getSource().toString();
417 }
418 }
419
420 /**
421 * The classification name for the first classification.
422 * Needs only to be defined if the import does not handle the naming
423 * itself (e.g. by using the taxon sec. reference title cache)
424 * @param classificationName the classificationName to set
425 */
426 public void setClassificationName(String classificationName) {
427 this.classificationName = classificationName;
428 }
429
430 /**
431 * @return the classificationName
432 */
433 public String getClassificationName() {
434 return classificationName;
435 }
436
437
438 public UUID getSourceRefUuid() {
439 return sourceRefUuid;
440 }
441
442
443
444 public void setSourceRefUuid(UUID sourceRefUuid) {
445 this.sourceRefUuid = sourceRefUuid;
446 }
447
448 /* (non-Javadoc)
449 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#isOmitTermLoading()
450 */
451 @Override
452 public boolean isOmitTermLoading() {
453 return false;
454 }
455
456 /* (non-Javadoc)
457 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#isCreateNew()
458 */
459 @Override
460 public boolean isCreateNew(){
461 return false;
462 }
463
464 @Override
465 public UsernamePasswordAuthenticationToken getAuthenticationToken(){
466 return this.authenticationToken;
467 }
468
469 /*user interaction*/
470 public boolean isInteractWithUser() {
471 return askUserForHelp;
472 }
473
474 public void setInteractWithUser (boolean interaction){
475 askUserForHelp=interaction;
476 }
477
478 }