Project

General

Profile

Download (4.21 KB) Statistics
| Branch: | Tag: | Revision:
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.mapping;
11

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

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

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

    
28
/**
29
 * @author a.mueller
30
 * @since 12.05.2009
31
 */
32
//TODO remove ANNOTATABLE by ISourcable (but this is not CDMBase yet therefore not trivial
33
public abstract class DbImportObjectCreationMapperBase<CREATE extends VersionableEntity, STATE extends DbImportStateBase<?,?>> extends DbImportMultiAttributeMapperBase<CREATE, STATE>  {
34
	private static final Logger logger = Logger.getLogger(DbImportObjectCreationMapperBase.class);
35

    
36

    
37
//******************************* ATTRIBUTES ***************************************/
38
	protected String dbIdAttribute;
39
	//TODO get standard namespace from mappingImport
40
	protected String objectToCreateNamespace;
41

    
42

    
43
//********************************* CONSTRUCTOR ****************************************/
44
	/**
45
	 * @param mappingImport
46
	 */
47
	protected DbImportObjectCreationMapperBase(String dbIdAttribute, String objectToCreateNamespace) {
48
		super();
49
		//TODO make it a single attribute mapper
50
		this.dbIdAttribute = dbIdAttribute;
51
		this.objectToCreateNamespace = objectToCreateNamespace;
52
	}
53

    
54
//************************************ METHODS *******************************************/
55

    
56
	@Override
57
    public CREATE invoke(ResultSet rs, CREATE noObject) throws SQLException {
58
		CREATE result = createObject(rs);
59
		result = doInvoke(rs, result);
60
		addOriginalSource(rs, result);
61
		return result;
62
	}
63

    
64
	/**
65
	 * @param rs
66
	 * @param result
67
	 * @throws SQLException
68
	 */
69
	protected abstract CREATE doInvoke(ResultSet rs, CREATE createdObject) throws SQLException;
70

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

    
81
	/**
82
	 * TODO also implemented in CdmImportBase (reduce redundance)
83
	 * @throws SQLException
84
	 */
85
	public void addOriginalSource(ResultSet rs, CREATE cdmBase) throws SQLException {
86
		if (cdmBase instanceof ISourceable ){
87
			if (StringUtils.isBlank(dbIdAttribute)){
88
				return;
89
			}
90
			IOriginalSource source;
91
			ISourceable sourceable = (ISourceable<?>)cdmBase;
92
			Object id = rs.getObject(dbIdAttribute);
93
			String strId = String.valueOf(id);
94
			String idNamespace = this.objectToCreateNamespace;
95

    
96
			Reference citation = getState().getTransactionalSourceReference();
97

    
98
			String microCitation = null;
99
			if (cdmBase instanceof IdentifiableEntity){
100
				source = IdentifiableSource.NewDataImportInstance(strId, idNamespace, citation);
101
			}else if (cdmBase instanceof DescriptionElementBase){
102
				source = DescriptionElementSource.NewDataImportInstance(strId, idNamespace, citation);
103
			}else{
104
				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.");
105
				return;
106
			}
107
			sourceable.addSource(source);
108
		}
109
	}
110

    
111
	/**
112
	 * Returns the transformer from the configuration
113
	 * @return
114
	 */
115
	protected IInputTransformer getTransformer(){
116
		return getState().getTransformer();
117
	}
118

    
119
}
(30-30/51)