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 java
.util
.UUID
;
18 import org
.apache
.commons
.lang
.StringUtils
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.transaction
.TransactionStatus
;
22 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
23 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
24 import eu
.etaxonomy
.cdm
.io
.common
.CdmImportBase
;
25 import eu
.etaxonomy
.cdm
.io
.common
.DbImportStateBase
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Extension
;
28 import eu
.etaxonomy
.cdm
.model
.common
.ExtensionType
;
29 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
30 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
33 * This class maps a database attribute to CDM extension added to the target class
34 * TODO maybe this class should not inherit from DbSingleAttributeImportMapperBase
35 * as it does not map to a single attribute
40 public class DbImportExtensionMapper
extends DbSingleAttributeImportMapperBase
<DbImportStateBase
<?
,?
>, IdentifiableEntity
> implements IDbImportMapper
<DbImportStateBase
<?
,?
>,IdentifiableEntity
>{
41 private static final Logger logger
= Logger
.getLogger(DbImportExtensionMapper
.class);
43 //************************** FACTORY METHODS ***************************************************************/
46 * @param dbAttributeString
54 public static DbImportExtensionMapper
NewInstance(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
){
55 return new DbImportExtensionMapper(dbAttributeString
, uuid
, label
, text
, labelAbbrev
);
58 public static DbImportExtensionMapper
NewInstance(String dbAttributeString
, ExtensionType extensionType
){
59 return new DbImportExtensionMapper(dbAttributeString
, extensionType
);
62 //***************** VARIABLES **********************************************************/
64 private ExtensionType extensionType
;
67 private String labelAbbrev
;
70 //******************************** CONSTRUCTOR *****************************************************************/
72 * @param dbAttributeString
78 private DbImportExtensionMapper(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
) {
79 super(dbAttributeString
, dbAttributeString
);
83 this.labelAbbrev
= labelAbbrev
;
87 * @param dbAttributeString
88 * @param extensionType
90 private DbImportExtensionMapper(String dbAttributeString
, ExtensionType extensionType
) {
91 super(dbAttributeString
, dbAttributeString
);
92 this.extensionType
= extensionType
;
95 //****************************** METHODS ***************************************************/
98 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
101 public void initialize(DbImportStateBase
<?
,?
> state
, Class
<?
extends CdmBase
> destinationClass
) {
102 importMapperHelper
.initialize(state
, destinationClass
);
103 CdmImportBase
<?
, ?
> currentImport
= state
.getCurrentIO();
104 if (currentImport
== null){
105 throw new IllegalStateException("Current import is not available. Please make sure the the state knows about the current import (state.setCurrentImport())) !");
109 if ( checkDbColumnExists()){
110 if (this.extensionType
== null){
111 this.extensionType
= getExtensionType(currentImport
, uuid
, label
, text
, labelAbbrev
);
116 } catch (NoSuchMethodException e
) {
117 //do nothing - checkDbColumnExists is not possible
127 public boolean invoke(Map
<String
, Object
> valueMap
, CdmBase cdmBase
){
128 Object dbValueObject
= valueMap
.get(this.getSourceAttribute().toLowerCase());
129 String dbValue
= dbValueObject
== null?
null: dbValueObject
.toString();
130 return invoke(dbValue
, cdmBase
);
138 private boolean invoke(String dbValue
, CdmBase cdmBase
){
142 if (cdmBase
instanceof IdentifiableEntity
){
143 IdentifiableEntity identifiableEntity
= (IdentifiableEntity
) cdmBase
;
144 invoke(dbValue
, identifiableEntity
);
147 throw new IllegalArgumentException("extended object must be of type identifiable entity.");
153 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
155 public IdentifiableEntity
invoke(ResultSet rs
, IdentifiableEntity identifiableEntity
) throws SQLException
{
156 String dbValue
= rs
.getString(getSourceAttribute());
157 return invoke(dbValue
, identifiableEntity
);
162 * @param identifiableEntity
165 private IdentifiableEntity
invoke(String dbValue
, IdentifiableEntity identifiableEntity
){
167 return identifiableEntity
;
169 if (StringUtils
.isNotBlank(dbValue
)){
170 Extension
.NewInstance(identifiableEntity
, dbValue
, extensionType
);
171 if (extensionType
== null){
172 logger
.warn("No extension type available for extension");
175 return identifiableEntity
;
187 protected ExtensionType
getExtensionType(CdmImportBase
<?
, ?
> currentImport
, UUID uuid
, String label
, String text
, String labelAbbrev
){
188 ITermService termService
= currentImport
.getTermService();
189 ExtensionType extensionType
= (ExtensionType
)termService
.find(uuid
);
190 if (extensionType
== null){
191 extensionType
= ExtensionType
.NewInstance(text
, label
, labelAbbrev
);
192 extensionType
.setUuid(uuid
);
193 //set vocabulary //TODO allow user defined vocabularies
194 UUID uuidExtensionTypeVocabulary
= UUID
.fromString("117cc307-5bd4-4b10-9b2f-2e14051b3b20");
195 IVocabularyService vocService
= currentImport
.getVocabularyService();
196 TransactionStatus tx
= currentImport
.startTransaction();
197 TermVocabulary
<ExtensionType
> voc
= vocService
.find(uuidExtensionTypeVocabulary
);
198 currentImport
.getVocabularyService().saveOrUpdate(voc
);
200 voc
.addTerm(extensionType
);
202 logger
.warn("Could not find default extensionType vocabulary. Vocabulary not set for new extension type.");
205 termService
.save(extensionType
);
206 currentImport
.commitTransaction(tx
);
208 return extensionType
;
214 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
216 public Class
<String
> getTypeClass(){