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
;
15 import java
.util
.UUID
;
17 import javax
.mail
.MethodNotSupportedException
;
19 import org
.apache
.log4j
.Logger
;
21 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
22 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
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
.common
.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 @SuppressWarnings("unused")
41 private static final Logger logger
= Logger
.getLogger(DbImportMarkerMapper
.class);
43 //************************** FACTORY METHODS ***************************************************************/
46 * @param dbAttributeString
53 public static DbImportMarkerMapper
NewInstance(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
){
54 return new DbImportMarkerMapper(dbAttributeString
, uuid
, label
, text
, labelAbbrev
);
57 public static DbImportMarkerMapper
NewInstance(String dbAttributeString
, MarkerType markerType
){
58 return new DbImportMarkerMapper(dbAttributeString
, markerType
);
61 //***************** VARIABLES **********************************************************/
63 private MarkerType markerType
;
66 private String labelAbbrev
;
69 //******************************** CONSTRUCTOR *****************************************************************/
71 * @param dbAttributeString
77 private DbImportMarkerMapper(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
) {
78 super(dbAttributeString
, dbAttributeString
);
82 this.labelAbbrev
= labelAbbrev
;
86 * @param dbAttributeString
87 * @param extensionType
89 private DbImportMarkerMapper(String dbAttributeString
, MarkerType markerType
) {
90 super(dbAttributeString
, dbAttributeString
);
91 this.markerType
= markerType
;
94 //****************************** METHODS ***************************************************/
97 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
100 public void initialize(DbImportStateBase
<?
,?
> state
, Class
<?
extends CdmBase
> destinationClass
) {
101 importMapperHelper
.initialize(state
, destinationClass
);
102 CdmImportBase
<?
, ?
> currentImport
= state
.getCurrentIO();
103 if (currentImport
== null){
104 throw new IllegalStateException("Current import is not available. Please make sure the the state knows about the current import (state.setCurrentImport())) !");
108 if ( checkDbColumnExists()){
109 if (this.markerType
== null){
110 this.markerType
= getMarkerType(currentImport
, uuid
, label
, text
, labelAbbrev
);
115 } catch (MethodNotSupportedException e
) {
116 //do nothing - checkDbColumnExists is not possible
126 private boolean invoke(Boolean dbValue
, CdmBase cdmBase
){
130 if (cdmBase
instanceof AnnotatableEntity
){
131 AnnotatableEntity annotatableEntity
= (AnnotatableEntity
) cdmBase
;
132 invoke(dbValue
, annotatableEntity
);
135 throw new IllegalArgumentException("marked object must be of type annotatable entity.");
141 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
143 public AnnotatableEntity
invoke(ResultSet rs
, AnnotatableEntity annotatableEntity
) throws SQLException
{
144 Boolean dbValue
= rs
.getBoolean(getSourceAttribute());
145 return invoke(dbValue
, annotatableEntity
);
150 * @param identifiableEntity
153 private AnnotatableEntity
invoke(Boolean dbValue
, AnnotatableEntity annotatableEntity
){
155 return annotatableEntity
;
157 if (dbValue
!= null){
158 Marker
.NewInstance(annotatableEntity
, dbValue
, this.markerType
);
160 return annotatableEntity
;
165 * @param currentImport
172 protected MarkerType
getMarkerType(CdmImportBase
<?
, ?
> currentImport
, UUID uuid
, String label
, String text
, String labelAbbrev
){
173 ITermService termService
= currentImport
.getTermService();
174 MarkerType markerType
= (MarkerType
)termService
.find(uuid
);
175 if (markerType
== null){
177 markerType
= MarkerType
.NewInstance(text
, label
, labelAbbrev
);
178 markerType
.setUuid(uuid
);
179 //set vocabulary //TODO allow user defined vocabularies
180 UUID uuidMarkerTypeVocabulary
= UUID
.fromString("19dffff7-e142-429c-a420-5d28e4ebe305");
181 IVocabularyService vocService
= currentImport
.getVocabularyService();
182 TermVocabulary voc
= vocService
.find(uuidMarkerTypeVocabulary
);
184 voc
.addTerm(markerType
);
186 logger
.warn("Could not find default markerType vocabulary. Vocabulary not set for new marker type.");
189 termService
.save(markerType
);
197 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
199 public Class
<Boolean
> getTypeClass(){
200 return Boolean
.class;