View Javadoc

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.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.ReferenceBase;
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> extends IoConfiguratorBase implements IImportConfigurator{
36  	private static final Logger logger = Logger.getLogger(ImportConfiguratorBase.class);
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  	/**
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 taxonomic tree name for the first taxonomic tree.
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 taxonomicTreeName = "Taxon tree - no name";
67  	
68  	private UUID  taxonomicTreeUuid = UUID.randomUUID();
69  	//uuid of concept reference
70  	private UUID  secUuid = UUID.randomUUID();
71  	
72  	private Object sourceSecId = -1;
73  	
74  	private Object source;
75  	protected ReferenceBase sourceReference;
76  	private ICdmDataSource destination;
77  	private Person commentator =  Person.NewTitledInstance("automatic CDM importer");
78  	
79  	protected Class<ICdmIO>[] ioClassList;
80  	
81  	protected ICdmIO[] ioList;
82  	
83  	protected String[] ioBeans;
84  	
85  /* *****************CONSTRUCTOR *****************************/
86  	
87  	public ImportConfiguratorBase(IInputTransformer transformer){
88  		super();
89  		setDbSchemaValidation(DbSchemaValidation.UPDATE);
90  		this.transformer = transformer;
91  		
92  	}
93  	
94  	abstract protected void makeIoClassList();
95  	
96  	/* (non-Javadoc)
97  	 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getTransformer()
98  	 */
99  	public IInputTransformer getTransformer() {
100 		return this.transformer;
101 	}
102 	
103 	/* (non-Javadoc)
104 	 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#setTransformer(eu.etaxonomy.cdm.io.common.mapping.IInputTransformer)
105 	 */
106 	public void setTransformer(IInputTransformer transformer){
107 		this.transformer = transformer;
108 	}
109 
110 
111 	
112 	
113 	/**
114 	 * @param source the source to set
115 	 */
116 	public void setSource(Object source) {
117 		this.source = source;
118 	}
119 	
120 	
121 	/**
122 	 * @param source the source to get
123 	 */
124 	public Object getSource() {
125 		return source;
126 	}
127 	
128 
129 
130 	/* (non-Javadoc)
131 	 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#isValid()
132 	 */
133 	public boolean isValid(){
134 		boolean result = true;
135 		if (source == null){
136 			logger.warn("Connection to source could not be established");
137 			result = false;
138 		}
139 //Not valid any more as the importer may already have a destination		
140 //		if (destination == null ){
141 //			logger.warn("Connection to Cdm could not be established");
142 //			result = false;
143 //		}
144 		
145 		return result;
146 	}
147 	
148 	
149 	
150 /* ****************** GETTER/SETTER **************************/	
151 
152 //	/**
153 //	 * @return the state
154 //	 */
155 //	public STATE getState() {
156 //		return state;
157 //	}
158 //
159 //	/**
160 //	 * @param state the state to set
161 //	 */
162 //	public void setState(STATE state) {
163 //		this.state = state;
164 //	}
165 	
166 	public void setIoClassList(ICdmIO[] ioList){
167 		this.ioList = ioList;
168 	}
169 	
170 	public Class<ICdmIO>[] getIoClassList(){
171 		if (ioClassList == null){
172 			makeIoClassList();
173 		}
174 		return ioClassList;
175 	}
176 
177 	/**
178 	 * @param ioClassList
179 	 */
180 	public void setIoClassList(Class<ICdmIO>[] ioClassList){
181 		this.ioClassList = ioClassList;
182 	}
183 	
184 	/* (non-Javadoc)
185 	 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#isDeleteAll()
186 	 */
187 	public boolean isDeleteAll() {
188 		return deleteAll;
189 	}
190 	/* (non-Javadoc)
191 	 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#setDeleteAll(boolean)
192 	 */
193 	public void setDeleteAll(boolean deleteAll) {
194 		this.deleteAll = deleteAll;
195 	}
196 
197 	
198 	/* (non-Javadoc)
199 	 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#getCheck()
200 	 */
201 	public CHECK getCheck() {
202 		return this.check;
203 	}
204 	
205 	/* (non-Javadoc)
206 	 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#setCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator.CHECK)
207 	 */
208 	public void setCheck(CHECK check) {
209 		this.check = check;
210 	}
211 	
212 	
213 	/**
214 	 * @return the editor
215 	 */
216 	public EDITOR getEditor() {
217 		return editor;
218 	}
219 
220 	/**
221 	 * @param editor the editor to set
222 	 */
223 	public void setEditor(EDITOR editor) {
224 		ImportConfiguratorBase.editor = editor;
225 	}
226 
227 	/**
228 	 * If true, no errors occur if objects are not found that should exist. This may
229 	 * be needed e.g. when only subsets of the data are imported.
230 	 * Default value is <cod>false</code>.
231 	 * @return the ignoreNull
232 	 */
233 	public boolean isIgnoreNull() {
234 		return ignoreNull;
235 	}
236 
237 	/**
238 	 * @param ignoreNull the ignoreNull to set
239 	 */
240 	public void setIgnoreNull(boolean ignoreNull) {
241 		this.ignoreNull = ignoreNull;
242 	}
243 
244 	/* (non-Javadoc)
245 	 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#getDestination()
246 	 */
247 	public ICdmDataSource getDestination() {
248 		return destination;
249 	}
250 	/* (non-Javadoc)
251 	 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#setDestination(eu.etaxonomy.cdm.database.ICdmDataSource)
252 	 */
253 	public void setDestination(ICdmDataSource destination) {
254 		this.destination = destination;
255 	}
256 
257 
258 	/* (non-Javadoc)
259 	 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#getSourceReference()
260 	 */
261 	public abstract ReferenceBase getSourceReference();
262 	/* (non-Javadoc)
263 	 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#setSourceReference(eu.etaxonomy.cdm.model.reference.ReferenceBase)
264 	 */
265 	public void setSourceReference(ReferenceBase sourceReference) {
266 		this.sourceReference = sourceReference;
267 	}
268 	/* (non-Javadoc)
269 	 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#getSourceReferenceTitle()
270 	 */
271 	public String getSourceReferenceTitle() {
272 		return getSourceReference().getTitleCache();
273 	}
274 	/* (non-Javadoc)
275 	 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#setSourceReferenceTitle(java.lang.String)
276 	 */
277 	public void setSourceReferenceTitle(String sourceReferenceTitle) {
278 		getSourceReference().setTitleCache(sourceReferenceTitle, true);
279 	}
280 
281 
282 	/* (non-Javadoc)
283 	 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#getCommentator()
284 	 */
285 	public Person getCommentator() {
286 		return commentator;
287 	}
288 
289 	/* (non-Javadoc)
290 	 * @see eu.etaxonomy.cdm.io.tcsrdf.IImportConfigurator#setCommentator(eu.etaxonomy.cdm.model.agent.Person)
291 	 */
292 	public void setCommentator(Person commentator) {
293 		this.commentator = commentator;
294 	}
295 
296 	/**
297 	 * @return the nomenclaturalCode
298 	 */
299 	public NomenclaturalCode getNomenclaturalCode() {
300 		return nomenclaturalCode;
301 	}
302 
303 
304 	/**
305 	 * @param nomenclaturalCode the nomenclaturalCode to set
306 	 */
307 	public void setNomenclaturalCode(NomenclaturalCode nomenclaturalCode) {
308 		this.nomenclaturalCode = nomenclaturalCode;
309 	}
310 
311 
312 	/* (non-Javadoc)
313 	 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getTreeUuid()
314 	 */
315 	public UUID getTaxonomicTreeUuid() {
316 		return taxonomicTreeUuid;
317 	}
318 
319 
320 	public void setTaxonomicTreeUuid(UUID treeUuid) {
321 		this.taxonomicTreeUuid = treeUuid;
322 	}
323 	
324 	/* (non-Javadoc)
325 	 * @see eu.etaxonomy.cdm.io.common.IImportConfigurator#getSecUuid()
326 	 */
327 	public UUID getSecUuid() {
328 		return secUuid;
329 	}
330 	public void setSecUuid(UUID secUuid) {
331 		this.secUuid = secUuid;
332 	}
333 
334 	/**
335 	 * @return the sourceSecId
336 	 */
337 	public Object getSourceSecId() {
338 		return sourceSecId;
339 	}
340 
341 	/**
342 	 * @param sourceSecId the sourceSecId to set
343 	 */
344 	public void setSourceSecId(Object sourceSecId) {
345 		this.sourceSecId = sourceSecId;
346 	}
347 	
348 
349 	/**
350 	 * @return the featureMap
351 	 */
352 	public Map<Integer, Feature>  getFeatureMap() {
353 		return featureMap;
354 	}
355 
356 	/**
357 	 * @param featureMap the featureMap to set
358 	 */
359 	public void setFeatureMap(Map<Integer, Feature>  featureMap) {
360 		this.featureMap = featureMap;
361 	}
362 
363 	
364 	protected static Method getDefaultFunction(Class<?> clazz, String methodName){
365 		try {
366 			return clazz.getMethod(methodName, List.class) ;
367 		} catch (SecurityException e) {
368 			logger.error(e.getMessage());
369 			e.printStackTrace();
370 		} catch (NoSuchMethodException e) {
371 			logger.error(e.getMessage());
372 			e.printStackTrace();
373 		}
374 		return null;
375 	}
376 	
377 
378 	/* (non-Javadoc)
379 	 * @see eu.etaxonomy.cdm.io.common.IIoConfigurator#getDestinationNameString()
380 	 */
381 	public String getDestinationNameString() {
382 		if (this.getDestination() == null) {
383 			return null;
384 		} else {
385 			return (String)this.getDestination().getName();
386 		}
387 	}
388 
389 	/**
390 	 * The taxonomic tree name for the first taxonomic tree.
391 	 * Needs only to be defined if the import does not handle the naming 
392 	 * itself (e.g. by using the taxon sec. reference title cache)
393 	 * @param taxonomicTreeName the taxonomicTreeName to set
394 	 */
395 	public void setTaxonomicTreeName(String taxonomicTreeName) {
396 		this.taxonomicTreeName = taxonomicTreeName;
397 	}
398 
399 	/**
400 	 * @return the taxonomicTreeName
401 	 */
402 	public String getTaxonomicTreeName() {
403 		return taxonomicTreeName;
404 	}
405 
406 
407 }