Project

General

Profile

Download (4.39 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.cdm.io.common.mapping;
12

    
13
import java.sql.ResultSet;
14
import java.sql.SQLException;
15

    
16
import org.apache.commons.lang.StringUtils;
17
import org.apache.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.io.common.DbImportStateBase;
20
import eu.etaxonomy.cdm.model.common.IOriginalSource;
21
import eu.etaxonomy.cdm.model.common.ISourceable;
22
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
23
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
24
import eu.etaxonomy.cdm.model.common.VersionableEntity;
25
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
26
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
27
import eu.etaxonomy.cdm.model.reference.Reference;
28

    
29
/**
30
 * @author a.mueller
31
 * @created 12.05.2009
32
 * @version 1.0
33
 */
34
//TODO remove ANNOTATABLE by ISourcable (but this is not CDMBase yet therefore not trivial
35
public abstract class DbImportObjectCreationMapperBase<CREATE extends VersionableEntity, STATE extends DbImportStateBase<?,?>> extends DbImportMultiAttributeMapperBase<CREATE, STATE>  {
36
	private static final Logger logger = Logger.getLogger(DbImportObjectCreationMapperBase.class);
37
	
38
	
39
//******************************* ATTRIBUTES ***************************************/
40
	protected String dbIdAttribute;
41
	//TODO get standard namespace from mappingImport
42
	protected String objectToCreateNamespace;
43
	
44
	
45
//********************************* CONSTRUCTOR ****************************************/
46
	/**
47
	 * @param mappingImport
48
	 */
49
	protected DbImportObjectCreationMapperBase(String dbIdAttribute, String objectToCreateNamespace) {
50
		super();
51
		//TODO make it a single attribute mapper
52
		this.dbIdAttribute = dbIdAttribute;
53
		this.objectToCreateNamespace = objectToCreateNamespace;
54
	}
55

    
56
//************************************ METHODS *******************************************/
57

    
58
	/* (non-Javadoc)
59
	 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
60
	 */
61
	public CREATE invoke(ResultSet rs, CREATE noObject) throws SQLException {
62
		CREATE result = createObject(rs);
63
		result = doInvoke(rs, result);
64
		addOriginalSource(rs, result);
65
		return result;
66
	}
67

    
68
	/**
69
	 * @param rs
70
	 * @param result
71
	 * @throws SQLException 
72
	 */
73
	protected abstract CREATE doInvoke(ResultSet rs, CREATE createdObject) throws SQLException;
74

    
75
	/**
76
	 * This method creates the object to be created. It needs to be implemented by the concrete classes.
77
	 * E.g. if you have a TaxonNameCreation class which inherits from this class you need to implement
78
	 * createObject by creating an empty taxon name.
79
	 * @param rs The result set
80
	 * @return The object to be created
81
	 * @throws SQLException 
82
	 */
83
	protected abstract CREATE createObject(ResultSet rs) throws SQLException;
84

    
85
	/**
86
	 * TODO also implemented in CdmImportBase (reduce redundance)
87
	 * @throws SQLException 
88
	 */
89
	public void addOriginalSource(ResultSet rs, CREATE cdmBase) throws SQLException {
90
		if (cdmBase instanceof ISourceable ){
91
			if (StringUtils.isBlank(dbIdAttribute)){
92
				return;
93
			}
94
			IOriginalSource source;
95
			ISourceable sourceable = (ISourceable)cdmBase;
96
			Object id = rs.getObject(dbIdAttribute);
97
			String strId = String.valueOf(id);
98
			String idNamespace = this.objectToCreateNamespace;
99
			
100
			Reference<?> citation = getState().getTransactionalSourceReference();
101
			
102
			String microCitation = null;
103
			if (cdmBase instanceof IdentifiableEntity){
104
				source = IdentifiableSource.NewDataImportInstance(strId, idNamespace, citation);
105
			}else if (cdmBase instanceof DescriptionElementBase){
106
				source = DescriptionElementSource.NewDataImportInstance(strId, idNamespace, citation);
107
			}else{
108
				logger.warn("ISourceable not beeing identifiable entities or description element base are not yet supported. CdmBase is of type " + cdmBase.getClass().getSimpleName() + ". Original source not added.");
109
				return;
110
			}
111
			sourceable.addSource(source);
112
		}
113
	}
114
	
115

    
116
	
117
	
118
	/**
119
	 * Returns the transformer from the configuration
120
	 * @return
121
	 */
122
	protected IInputTransformer getTransformer(){
123
		return getState().getTransformer();
124
	}
125
	
126
}
(30-30/51)