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.IOriginalSource;
|
20
|
import eu.etaxonomy.cdm.model.common.ISourceable;
|
21
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
22
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
23
|
import eu.etaxonomy.cdm.model.common.VersionableEntity;
|
24
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
25
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
26
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
27
|
|
28
|
/**
|
29
|
* @author a.mueller
|
30
|
* @created 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
|
/* (non-Javadoc)
|
57
|
* @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
|
58
|
*/
|
59
|
@Override
|
60
|
public CREATE invoke(ResultSet rs, CREATE noObject) throws SQLException {
|
61
|
CREATE result = createObject(rs);
|
62
|
result = doInvoke(rs, result);
|
63
|
addOriginalSource(rs, result);
|
64
|
return result;
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* @param rs
|
69
|
* @param result
|
70
|
* @throws SQLException
|
71
|
*/
|
72
|
protected abstract CREATE doInvoke(ResultSet rs, CREATE createdObject) throws SQLException;
|
73
|
|
74
|
/**
|
75
|
* This method creates the object to be created. It needs to be implemented by the concrete classes.
|
76
|
* E.g. if you have a TaxonNameCreation class which inherits from this class you need to implement
|
77
|
* createObject by creating an empty taxon name.
|
78
|
* @param rs The result set
|
79
|
* @return The object to be created
|
80
|
* @throws SQLException
|
81
|
*/
|
82
|
protected abstract CREATE createObject(ResultSet rs) throws SQLException;
|
83
|
|
84
|
/**
|
85
|
* TODO also implemented in CdmImportBase (reduce redundance)
|
86
|
* @throws SQLException
|
87
|
*/
|
88
|
public void addOriginalSource(ResultSet rs, CREATE cdmBase) throws SQLException {
|
89
|
if (cdmBase instanceof ISourceable ){
|
90
|
if (StringUtils.isBlank(dbIdAttribute)){
|
91
|
return;
|
92
|
}
|
93
|
IOriginalSource source;
|
94
|
ISourceable sourceable = (ISourceable)cdmBase;
|
95
|
Object id = rs.getObject(dbIdAttribute);
|
96
|
String strId = String.valueOf(id);
|
97
|
String idNamespace = this.objectToCreateNamespace;
|
98
|
|
99
|
Reference citation = getState().getTransactionalSourceReference();
|
100
|
|
101
|
String microCitation = null;
|
102
|
if (cdmBase instanceof IdentifiableEntity){
|
103
|
source = IdentifiableSource.NewDataImportInstance(strId, idNamespace, citation);
|
104
|
}else if (cdmBase instanceof DescriptionElementBase){
|
105
|
source = DescriptionElementSource.NewDataImportInstance(strId, idNamespace, citation);
|
106
|
}else{
|
107
|
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.");
|
108
|
return;
|
109
|
}
|
110
|
sourceable.addSource(source);
|
111
|
}
|
112
|
}
|
113
|
|
114
|
|
115
|
|
116
|
|
117
|
/**
|
118
|
* Returns the transformer from the configuration
|
119
|
* @return
|
120
|
*/
|
121
|
protected IInputTransformer getTransformer(){
|
122
|
return getState().getTransformer();
|
123
|
}
|
124
|
|
125
|
}
|