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 javax
.mail
.MethodNotSupportedException
;
20 import org
.apache
.log4j
.Logger
;
21 import org
.springframework
.transaction
.TransactionStatus
;
23 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
24 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
25 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
26 import eu
.etaxonomy
.cdm
.io
.common
.CdmImportBase
;
27 import eu
.etaxonomy
.cdm
.io
.common
.DbImportStateBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
29 import eu
.etaxonomy
.cdm
.model
.common
.Extension
;
30 import eu
.etaxonomy
.cdm
.model
.common
.ExtensionType
;
31 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
32 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
35 * This class maps a database attribute to CDM extension added to the target class
36 * TODO maybe this class should not inherit from DbSingleAttributeImportMapperBase
37 * as it does not map to a single attribute
42 public class DbImportExtensionMapper
extends DbSingleAttributeImportMapperBase
<DbImportStateBase
<?
,?
>, IdentifiableEntity
> implements IDbImportMapper
<DbImportStateBase
<?
,?
>,IdentifiableEntity
>{
43 private static final Logger logger
= Logger
.getLogger(DbImportExtensionMapper
.class);
45 //************************** FACTORY METHODS ***************************************************************/
48 * @param dbAttributeString
56 public static DbImportExtensionMapper
NewInstance(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
){
57 return new DbImportExtensionMapper(dbAttributeString
, uuid
, label
, text
, labelAbbrev
);
60 public static DbImportExtensionMapper
NewInstance(String dbAttributeString
, ExtensionType extensionType
){
61 return new DbImportExtensionMapper(dbAttributeString
, extensionType
);
64 //***************** VARIABLES **********************************************************/
66 private ExtensionType extensionType
;
69 private String labelAbbrev
;
72 //******************************** CONSTRUCTOR *****************************************************************/
74 * @param dbAttributeString
80 private DbImportExtensionMapper(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
) {
81 super(dbAttributeString
, dbAttributeString
);
85 this.labelAbbrev
= labelAbbrev
;
89 * @param dbAttributeString
90 * @param extensionType
92 private DbImportExtensionMapper(String dbAttributeString
, ExtensionType extensionType
) {
93 super(dbAttributeString
, dbAttributeString
);
94 this.extensionType
= extensionType
;
97 //****************************** METHODS ***************************************************/
100 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
103 public void initialize(DbImportStateBase
<?
,?
> state
, Class
<?
extends CdmBase
> destinationClass
) {
104 importMapperHelper
.initialize(state
, destinationClass
);
105 CdmImportBase
<?
, ?
> currentImport
= state
.getCurrentIO();
106 if (currentImport
== null){
107 throw new IllegalStateException("Current import is not available. Please make sure the the state knows about the current import (state.setCurrentImport())) !");
111 if ( checkDbColumnExists()){
112 if (this.extensionType
== null){
113 this.extensionType
= getExtensionType(currentImport
, uuid
, label
, text
, labelAbbrev
);
118 } catch (MethodNotSupportedException e
) {
119 //do nothing - checkDbColumnExists is not possible
129 public boolean invoke(Map
<String
, Object
> valueMap
, CdmBase cdmBase
){
130 Object dbValueObject
= valueMap
.get(this.getSourceAttribute().toLowerCase());
131 String dbValue
= dbValueObject
== null?
null: dbValueObject
.toString();
132 return invoke(dbValue
, cdmBase
);
140 private boolean invoke(String dbValue
, CdmBase cdmBase
){
144 if (cdmBase
instanceof IdentifiableEntity
){
145 IdentifiableEntity identifiableEntity
= (IdentifiableEntity
) cdmBase
;
146 invoke(dbValue
, identifiableEntity
);
149 throw new IllegalArgumentException("extended object must be of type identifiable entity.");
155 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
157 public IdentifiableEntity
invoke(ResultSet rs
, IdentifiableEntity identifiableEntity
) throws SQLException
{
158 String dbValue
= rs
.getString(getSourceAttribute());
159 return invoke(dbValue
, identifiableEntity
);
164 * @param identifiableEntity
167 private IdentifiableEntity
invoke(String dbValue
, IdentifiableEntity identifiableEntity
){
169 return identifiableEntity
;
171 if (CdmUtils
.isNotEmpty(dbValue
)){
172 Extension
.NewInstance(identifiableEntity
, dbValue
, extensionType
);
174 return identifiableEntity
;
186 protected ExtensionType
getExtensionType(CdmImportBase
<?
, ?
> currentImport
, UUID uuid
, String label
, String text
, String labelAbbrev
){
187 ITermService termService
= currentImport
.getTermService();
188 ExtensionType extensionType
= (ExtensionType
)termService
.find(uuid
);
189 if (extensionType
== null){
190 extensionType
= ExtensionType
.NewInstance(text
, label
, labelAbbrev
);
191 extensionType
.setUuid(uuid
);
192 //set vocabulary //TODO allow user defined vocabularies
193 UUID uuidExtensionTypeVocabulary
= UUID
.fromString("117cc307-5bd4-4b10-9b2f-2e14051b3b20");
194 IVocabularyService vocService
= currentImport
.getVocabularyService();
195 TermVocabulary voc
= vocService
.find(uuidExtensionTypeVocabulary
);
197 TransactionStatus tx
= currentImport
.startTransaction();
198 currentImport
.getVocabularyService().saveOrUpdate(voc
);
201 voc
.addTerm(extensionType
);
203 logger
.warn("Could not find default extensionType vocabulary. Vocabulary not set for new extension type.");
206 termService
.save(extensionType
);
208 currentImport
.commitTransaction(tx
);
211 return extensionType
;
217 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
219 public Class
<String
> getTypeClass(){