3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.io
.common
.mapping
;
13 import java
.sql
.ResultSet
;
14 import java
.sql
.SQLException
;
16 import org
.apache
.commons
.lang
.StringUtils
;
17 import org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.io
.common
.DbImportStateBase
;
20 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
21 import eu
.etaxonomy
.cdm
.model
.common
.DescriptionElementSource
;
22 import eu
.etaxonomy
.cdm
.model
.common
.IOriginalSource
;
23 import eu
.etaxonomy
.cdm
.model
.common
.ISourceable
;
24 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
25 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableSource
;
26 import eu
.etaxonomy
.cdm
.model
.common
.VersionableEntity
;
27 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
28 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
35 //TODO remove ANNOTATABLE by ISourcable (but this is not CDMBase yet therefore not trivial
36 public abstract class DbImportObjectCreationMapperBase
<CREATE
extends VersionableEntity
, STATE
extends DbImportStateBase
<?
,?
>> extends DbImportMultiAttributeMapperBase
<CREATE
, STATE
> {
37 private static final Logger logger
= Logger
.getLogger(DbImportObjectCreationMapperBase
.class);
40 //******************************* ATTRIBUTES ***************************************/
41 protected String dbIdAttribute
;
42 //TODO get standard namespace from mappingImport
43 protected String objectToCreateNamespace
;
46 //********************************* CONSTRUCTOR ****************************************/
48 * @param mappingImport
50 protected DbImportObjectCreationMapperBase(String dbIdAttribute
, String objectToCreateNamespace
) {
52 //TODO make it a single attribute mapper
53 this.dbIdAttribute
= dbIdAttribute
;
54 this.objectToCreateNamespace
= objectToCreateNamespace
;
57 //************************************ METHODS *******************************************/
60 * @see eu.etaxonomy.cdm.io.common.mapping.IDbImportMapper#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
62 public CREATE
invoke(ResultSet rs
, CREATE noObject
) throws SQLException
{
63 CREATE result
= createObject(rs
);
64 result
= doInvoke(rs
, result
);
65 addOriginalSource(rs
, result
);
72 * @throws SQLException
74 protected abstract CREATE
doInvoke(ResultSet rs
, CREATE createdObject
) throws SQLException
;
77 * This method creates the object to be created. It needs to be implemented by the concrete classes.
78 * E.g. if you have a TaxonNameCreation class which inherits from this class you need to implement
79 * createObject by creating an empty taxon name.
80 * @param rs The result set
81 * @return The object to be created
82 * @throws SQLException
84 protected abstract CREATE
createObject(ResultSet rs
) throws SQLException
;
87 * TODO also implemented in CdmImportBase (reduce redundance)
88 * @throws SQLException
90 public void addOriginalSource(ResultSet rs
, CREATE cdmBase
) throws SQLException
{
91 if (cdmBase
instanceof ISourceable
){
92 if (StringUtils
.isBlank(dbIdAttribute
)){
95 IOriginalSource source
;
96 ISourceable sourceable
= (ISourceable
)cdmBase
;
97 Object id
= rs
.getObject(dbIdAttribute
);
98 String strId
= String
.valueOf(id
);
99 String idNamespace
= this.objectToCreateNamespace
;
101 Reference
<?
> citation
= getState().getTransactionalSourceReference();
103 String microCitation
= null;
104 if (cdmBase
instanceof IdentifiableEntity
){
105 source
= IdentifiableSource
.NewDataImportInstance(strId
, idNamespace
, citation
);
106 }else if (cdmBase
instanceof DescriptionElementBase
){
107 source
= DescriptionElementSource
.NewDataImportInstance(strId
, idNamespace
, citation
);
109 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.");
112 sourceable
.addSource(source
);
120 * Returns the transformer from the configuration
123 protected IInputTransformer
getTransformer(){
124 return getState().getTransformer();