2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.common
.mapping
;
12 import java
.sql
.ResultSet
;
13 import java
.sql
.SQLException
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.springframework
.transaction
.TransactionStatus
;
20 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
21 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
22 import eu
.etaxonomy
.cdm
.database
.update
.DatabaseTypeNotSupportedException
;
23 import eu
.etaxonomy
.cdm
.io
.common
.CdmImportBase
;
24 import eu
.etaxonomy
.cdm
.io
.common
.DbImportStateBase
;
25 import eu
.etaxonomy
.cdm
.model
.common
.AnnotatableEntity
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.Marker
;
28 import eu
.etaxonomy
.cdm
.model
.common
.MarkerType
;
29 import eu
.etaxonomy
.cdm
.model
.term
.TermVocabulary
;
32 * This class maps a database attribute to CDM extension added to the target class
33 * TODO maybe this class should not inherit from DbSingleAttributeImportMapperBase
34 * as it does not map to a single attribute
39 public class DbImportMarkerMapper
extends DbSingleAttributeImportMapperBase
<DbImportStateBase
<?
,?
>, AnnotatableEntity
> implements IDbImportMapper
<DbImportStateBase
<?
,?
>,AnnotatableEntity
>{
40 private static final Logger logger
= Logger
.getLogger(DbImportMarkerMapper
.class);
42 //************************** FACTORY METHODS ***************************************************************/
45 * @param dbAttributeString
52 public static DbImportMarkerMapper
NewInstance(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
, Boolean ignoreValue
){
53 return new DbImportMarkerMapper(dbAttributeString
, uuid
, label
, text
, labelAbbrev
, ignoreValue
);
56 public static DbImportMarkerMapper
NewInstance(String dbAttributeString
, MarkerType markerType
, Boolean ignoreValue
){
57 return new DbImportMarkerMapper(dbAttributeString
, markerType
, ignoreValue
);
60 //***************** VARIABLES **********************************************************/
62 private MarkerType markerType
;
65 private String labelAbbrev
;
67 private Boolean ignoreValue
;
69 //******************************** CONSTRUCTOR *****************************************************************/
71 * @param dbAttributeString
77 private DbImportMarkerMapper(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
, Boolean ignoreValue
) {
78 super(dbAttributeString
, dbAttributeString
);
82 this.labelAbbrev
= labelAbbrev
;
83 this.ignoreValue
= ignoreValue
;
87 * @param dbAttributeString
88 * @param extensionType
90 private DbImportMarkerMapper(String dbAttributeString
, MarkerType markerType
, Boolean ignoreValue
) {
91 super(dbAttributeString
, dbAttributeString
);
92 this.markerType
= markerType
;
93 this.ignoreValue
= ignoreValue
;
96 //****************************** METHODS ***************************************************/
99 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
102 public void initialize(DbImportStateBase
<?
,?
> state
, Class
<?
extends CdmBase
> destinationClass
) {
103 importMapperHelper
.initialize(state
, destinationClass
);
104 CdmImportBase
<?
, ?
> currentImport
= state
.getCurrentIO();
105 if (currentImport
== null){
106 throw new IllegalStateException("Current import is not available. Please make sure the the state knows about the current import (state.setCurrentImport())) !");
110 if ( checkDbColumnExists()){
111 if (this.markerType
== null){
112 this.markerType
= getMarkerType(currentImport
, uuid
, label
, text
, labelAbbrev
);
117 } catch (DatabaseTypeNotSupportedException e
) {
118 //do nothing - checkDbColumnExists is not possible
128 public boolean invoke(Map
<String
, Object
> valueMap
, CdmBase cdmBase
){
129 Object dbValueObject
= valueMap
.get(this.getSourceAttribute().toLowerCase());
130 Boolean dbValue
= (Boolean
) (dbValueObject
== null?
null: dbValueObject
);
131 return invoke(dbValue
, cdmBase
);
139 private boolean invoke(Boolean dbValue
, CdmBase cdmBase
){
143 if (cdmBase
instanceof AnnotatableEntity
){
144 AnnotatableEntity annotatableEntity
= (AnnotatableEntity
) cdmBase
;
145 invoke(dbValue
, annotatableEntity
);
148 throw new IllegalArgumentException("marked object must be of type annotatable entity.");
154 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
156 public AnnotatableEntity
invoke(ResultSet rs
, AnnotatableEntity annotatableEntity
) throws SQLException
{
157 Boolean dbValue
= rs
.getBoolean(getSourceAttribute());
158 return invoke(dbValue
, annotatableEntity
);
163 * @param identifiableEntity
166 private AnnotatableEntity
invoke(Boolean dbValue
, AnnotatableEntity annotatableEntity
){
168 return annotatableEntity
;
170 if (dbValue
!= null && ! dbValue
.equals(this.ignoreValue
)){
171 Marker
.NewInstance(annotatableEntity
, dbValue
, this.markerType
);
172 if (this.markerType
== null){
173 logger
.warn("No marker type available for marker");
176 return annotatableEntity
;
181 * @param currentImport
188 protected MarkerType
getMarkerType(CdmImportBase
<?
, ?
> currentImport
, UUID uuid
, String label
, String text
, String labelAbbrev
){
189 ITermService termService
= currentImport
.getTermService();
190 MarkerType markerType
= (MarkerType
)termService
.find(uuid
);
191 if (markerType
== null){
193 markerType
= MarkerType
.NewInstance(text
, label
, labelAbbrev
);
194 markerType
.setUuid(uuid
);
195 //set vocabulary //TODO allow user defined vocabularies
196 UUID uuidMarkerTypeVocabulary
= UUID
.fromString("19dffff7-e142-429c-a420-5d28e4ebe305");
197 IVocabularyService vocService
= currentImport
.getVocabularyService();
198 TransactionStatus tx
= currentImport
.startTransaction();
199 TermVocabulary voc
= vocService
.find(uuidMarkerTypeVocabulary
);
201 voc
.addTerm(markerType
);
203 logger
.warn("Could not find default markerType vocabulary. Vocabulary not set for new marker type.");
206 termService
.save(markerType
);
207 currentImport
.commitTransaction(tx
);
215 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
217 public Class
<Boolean
> getTypeClass(){
218 return Boolean
.class;