adding Vaadin compatibility to trunk
[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 (getSource() == 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 @Override
195 public CHECK getCheck() {
196 return this.check;
197 }
198
199 @Override
200 public void setCheck(CHECK check) {
201 this.check = check;
202 }
203
204
205 /**
206 * @return the editor
207 */
208 @Override
209 public EDITOR getEditor() {
210 return editor;
211 }
212
213 /**
214 * @param editor the editor to set
215 */
216 @Override
217 public void setEditor(EDITOR editor) {
218 ImportConfiguratorBase.editor = editor;
219 }
220
221 /**
222 * If true, no errors occur if objects are not found that should exist. This may
223 * be needed e.g. when only subsets of the data are imported.
224 * Default value is <cod>false</code>.
225 * @return the ignoreNull
226 */
227 @Override
228 public boolean isIgnoreNull() {
229 return ignoreNull;
230 }
231
232 /**
233 * @param ignoreNull the ignoreNull to set
234 */
235 @Override
236 public void setIgnoreNull(boolean ignoreNull) {
237 this.ignoreNull = ignoreNull;
238 }
239
240 @Override
241 public ICdmDataSource getDestination() {
242 return destination;
243 }
244 @Override
245 public void setDestination(ICdmDataSource destination) {
246 this.destination = destination;
247 }
248
249 @Override
250 public abstract Reference getSourceReference();
251 @Override
252 public void setSourceReference(Reference sourceReference) {
253 this.sourceReference = sourceReference;
254 }
255 @Override
256 public String getSourceReferenceTitle() {
257 return getSourceReference().getTitleCache();
258 }
259 @Override
260 public void setSourceReferenceTitle(String sourceReferenceTitle) {
261 getSourceReference().setTitleCache(sourceReferenceTitle, true);
262 }
263
264 @Override
265 public Person getCommentator() {
266 return commentator;
267 }
268
269 @Override
270 public void setCommentator(Person commentator) {
271 this.commentator = commentator;
272 }
273
274 /**
275 * @return the nomenclaturalCode
276 */
277 @Override
278 public NomenclaturalCode getNomenclaturalCode() {
279 return nomenclaturalCode;
280 }
281
282
283 /**
284 * @param nomenclaturalCode the nomenclaturalCode to set
285 */
286 @Override
287 public void setNomenclaturalCode(NomenclaturalCode nomenclaturalCode) {
288 this.nomenclaturalCode = nomenclaturalCode;
289 }
290
291
292 /* (non-Javadoc)
293 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getTreeUuid()
294 */
295 @Override
296 public UUID getClassificationUuid() {
297 return classificationUuid;
298 }
299
300
301 @Override
302 public void setClassificationUuid(UUID classificationUuid) {
303 this.classificationUuid = classificationUuid;
304 }
305
306
307 @Override
308 public UUID getSecUuid() {
309 return secUuid;
310 }
311 @Override
312 public void setSecUuid(UUID secUuid) {
313 this.secUuid = secUuid;
314 }
315
316 /**
317 * @return the sourceSecId
318 */
319 @Override
320 public Object getSourceSecId() {
321 return sourceSecId;
322 }
323
324 /**
325 * @param sourceSecId the sourceSecId to set
326 */
327 public void setSourceSecId(Object sourceSecId) {
328 this.sourceSecId = sourceSecId;
329 }
330
331
332 /**
333 * @return the featureMap
334 */
335 public Map<Integer, Feature> getFeatureMap() {
336 return featureMap;
337 }
338
339 /**
340 * @param featureMap the featureMap to set
341 */
342 public void setFeatureMap(Map<Integer, Feature> featureMap) {
343 this.featureMap = featureMap;
344 }
345
346
347 protected static Method getDefaultFunction(Class<?> clazz, String methodName){
348 try {
349 return clazz.getMethod(methodName, List.class) ;
350 } catch (SecurityException e) {
351 logger.error(e.getMessage());
352 e.printStackTrace();
353 } catch (NoSuchMethodException e) {
354 logger.error(e.getMessage());
355 e.printStackTrace();
356 }
357 return null;
358 }
359
360
361 /* (non-Javadoc)
362 * @see eu.etaxonomy.cdm.io.common.IIoConfigurator#getDestinationNameString()
363 */
364 @Override
365 public String getDestinationNameString() {
366 if (this.getDestination() == null) {
367 return null;
368 } else {
369 return this.getDestination().getName().toString();
370 }
371 }
372
373 /* (non-Javadoc)
374 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getSourceNameString()
375 */
376 @Override
377 public String getSourceNameString() {
378 if (this.getSource() == null){
379 return null;
380 }else{
381 return this.getSource().toString();
382 }
383 }
384
385 /**
386 * The classification name for the first classification.
387 * Needs only to be defined if the import does not handle the naming
388 * itself (e.g. by using the taxon sec. reference title cache)
389 * @param classificationName the classificationName to set
390 */
391 public void setClassificationName(String classificationName) {
392 this.classificationName = classificationName;
393 }
394
395 /**
396 * @return the classificationName
397 */
398 public String getClassificationName() {
399 return classificationName;
400 }
401
402
403 public UUID getSourceRefUuid() {
404 return sourceRefUuid;
405 }
406
407
408
409 public void setSourceRefUuid(UUID sourceRefUuid) {
410 this.sourceRefUuid = sourceRefUuid;
411 }
412
413 /* (non-Javadoc)
414 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#isOmitTermLoading()
415 */
416 @Override
417 public boolean isOmitTermLoading() {
418 return false;
419 }
420
421 /* (non-Javadoc)
422 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#isCreateNew()
423 */
424 @Override
425 public boolean isCreateNew(){
426 return false;
427 }
428
429 @Override
430 public UsernamePasswordAuthenticationToken getAuthenticationToken(){
431 return this.authenticationToken;
432 }
433
434 /*user interaction*/
435 public boolean isInteractWithUser() {
436 return askUserForHelp;
437 }
438
439 public void setInteractWithUser (boolean interaction){
440 askUserForHelp=interaction;
441 }
442
443 }