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 org
.apache
.log4j
.Logger
;
19 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
20 import eu
.etaxonomy
.cdm
.api
.service
.IVocabularyService
;
21 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
22 import eu
.etaxonomy
.cdm
.io
.common
.CdmImportBase
;
23 import eu
.etaxonomy
.cdm
.io
.common
.DbImportStateBase
;
24 import eu
.etaxonomy
.cdm
.model
.common
.AnnotatableEntity
;
25 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
26 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
29 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
32 * This class maps a database attribute to CDM annotation 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 DbImportAnnotationMapper
extends DbSingleAttributeImportMapperBase
<DbImportStateBase
<?
,?
>, AnnotatableEntity
> implements IDbImportMapper
<DbImportStateBase
<?
,?
>,AnnotatableEntity
>{
40 private static final Logger logger
= Logger
.getLogger(DbImportAnnotationMapper
.class);
43 * FIXME Warning: the annotation type creation is not yet implemented
44 * @param dbAttributeString
51 public static DbImportAnnotationMapper
NewInstance(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
){
52 Language language
= null;
53 AnnotationType annotationType
= null;
54 return new DbImportAnnotationMapper(dbAttributeString
, uuid
, label
, text
, labelAbbrev
, language
, annotationType
);
58 * * FIXME Warning: the annotation type creation is not yet implemented
59 * @param dbAttributeString
66 public static DbImportAnnotationMapper
NewInstance(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
, Language language
){
67 AnnotationType annotationType
= null;
68 return new DbImportAnnotationMapper(dbAttributeString
, uuid
, label
, text
, labelAbbrev
, language
, annotationType
);
72 * @param dbAttributeString
73 * @param annotationType
76 public static DbImportAnnotationMapper
NewInstance(String dbAttributeString
, AnnotationType annotationType
){
77 Language language
= null;
78 return NewInstance(dbAttributeString
, annotationType
, language
);
82 * @param dbAttributeString
83 * @param annotationType
87 public static DbImportAnnotationMapper
NewInstance(String dbAttributeString
, AnnotationType annotationType
, Language language
){
90 String labelAbbrev
= null;
92 return new DbImportAnnotationMapper(dbAttributeString
, uuid
, label
, text
, labelAbbrev
, language
, annotationType
);
96 private AnnotationType annotationType
;
99 private String labelAbbrev
;
101 private Language language
;
104 * @param dbAttributeString
110 private DbImportAnnotationMapper(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
, Language language
, AnnotationType annotationType
) {
111 super(dbAttributeString
, dbAttributeString
);
115 this.labelAbbrev
= labelAbbrev
;
116 this.language
= language
;
117 this.annotationType
= annotationType
;
122 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
125 public void initialize(DbImportStateBase
<?
,?
> state
, Class
<?
extends CdmBase
> destinationClass
) {
126 importMapperHelper
.initialize(state
, destinationClass
);
132 // * @param tableName
134 // public void initialize(ITermService service, DbImportStateBase state, Class<? extends CdmBase> destinationClass) {
135 // importMapperHelper.initialize(state, destinationClass);
137 // if ( checkDbColumnExists()){
138 // if (this.annotationType == null){
139 // this.annotationType = getAnnotationType(service, uuid, label, text, labelAbbrev);
144 // } catch (MethodNotSupportedException e) {
151 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
154 public AnnotatableEntity
invoke(ResultSet rs
, AnnotatableEntity annotatableEntity
) throws SQLException
{
156 return annotatableEntity
;
158 String dbValue
= rs
.getString(getSourceAttribute());
159 return doInvoke(annotatableEntity
, dbValue
);
163 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#doInvoke(eu.etaxonomy.cdm.model.common.CdmBase, java.lang.Object)
166 protected AnnotatableEntity
doInvoke(AnnotatableEntity annotatableEntity
, Object dbValue
){
167 String strAnnotation
= (String
)dbValue
;
168 if (CdmUtils
.isNotEmpty(strAnnotation
));{
169 Annotation annotation
= Annotation
.NewInstance(strAnnotation
, annotationType
, language
);
170 if (annotatableEntity
!= null){
171 annotatableEntity
.addAnnotation(annotation
);
174 return annotatableEntity
;
188 protected AnnotationType
getAnnotationType(CdmImportBase
<?
, ?
> currentImport
, UUID uuid
, String label
, String text
, String labelAbbrev
){
189 ITermService termService
= currentImport
.getTermService();
190 AnnotationType annotationType
= (AnnotationType
)termService
.find(uuid
);
191 if (annotationType
== null){
192 annotationType
= AnnotationType
.NewInstance(text
, label
, labelAbbrev
);
193 annotationType
.setUuid(uuid
);
194 //set vocabulary //TODO allow user defined vocabularies
195 UUID uuidAnnotationTypeVocabulary
= UUID
.fromString("ca04609b-1ba0-4d31-9c2e-aa8eb2f4e62d");
196 IVocabularyService vocService
= currentImport
.getVocabularyService();
197 TermVocabulary voc
= vocService
.find(uuidAnnotationTypeVocabulary
);
199 voc
.addTerm(annotationType
);
201 logger
.warn("Could not find default annotation type vocabulary. Vocabulary not set for new annotation type.");
203 //savetermService.save(annotationType);
205 return annotationType
;
211 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
213 public Class
<String
> getTypeClass(){