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