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
, 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
, 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
){
88 // String label = null;
89 // String text = null;
90 // String labelAbbrev = null;
92 return new DbImportAnnotationMapper(dbAttributeString
, language
, annotationType
);
96 private AnnotationType annotationType
;
97 private Language language
;
100 * @param dbAttributeString
106 private DbImportAnnotationMapper(String dbAttributeString
, Language language
, AnnotationType annotationType
) {
107 super(dbAttributeString
, dbAttributeString
);
108 this.language
= language
;
109 this.annotationType
= annotationType
;
114 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
117 public void initialize(DbImportStateBase
<?
,?
> state
, Class
<?
extends CdmBase
> destinationClass
) {
118 importMapperHelper
.initialize(state
, destinationClass
);
124 // * @param tableName
126 // public void initialize(ITermService service, DbImportStateBase state, Class<? extends CdmBase> destinationClass) {
127 // importMapperHelper.initialize(state, destinationClass);
129 // if ( checkDbColumnExists()){
130 // if (this.annotationType == null){
131 // this.annotationType = getAnnotationType(service, uuid, label, text, labelAbbrev);
136 // } catch (MethodNotSupportedException e) {
143 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
146 public AnnotatableEntity
invoke(ResultSet rs
, AnnotatableEntity annotatableEntity
) throws SQLException
{
148 return annotatableEntity
;
150 String dbValue
= rs
.getString(getSourceAttribute());
151 return doInvoke(annotatableEntity
, dbValue
);
155 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#doInvoke(eu.etaxonomy.cdm.model.common.CdmBase, java.lang.Object)
158 protected AnnotatableEntity
doInvoke(AnnotatableEntity annotatableEntity
, Object dbValue
){
159 String strAnnotation
= (String
)dbValue
;
160 if (CdmUtils
.isNotEmpty(strAnnotation
));{
161 Annotation annotation
= Annotation
.NewInstance(strAnnotation
, annotationType
, language
);
162 if (annotatableEntity
!= null){
163 annotatableEntity
.addAnnotation(annotation
);
166 return annotatableEntity
;
180 protected AnnotationType
getAnnotationType(CdmImportBase
<?
, ?
> currentImport
, UUID uuid
, String label
, String text
, String labelAbbrev
){
181 ITermService termService
= currentImport
.getTermService();
182 AnnotationType annotationType
= (AnnotationType
)termService
.find(uuid
);
183 if (annotationType
== null){
184 annotationType
= AnnotationType
.NewInstance(text
, label
, labelAbbrev
);
185 annotationType
.setUuid(uuid
);
186 //set vocabulary //TODO allow user defined vocabularies
187 UUID uuidAnnotationTypeVocabulary
= UUID
.fromString("ca04609b-1ba0-4d31-9c2e-aa8eb2f4e62d");
188 IVocabularyService vocService
= currentImport
.getVocabularyService();
189 TermVocabulary voc
= vocService
.find(uuidAnnotationTypeVocabulary
);
191 voc
.addTerm(annotationType
);
193 logger
.warn("Could not find default annotation type vocabulary. Vocabulary not set for new annotation type.");
195 //savetermService.save(annotationType);
197 return annotationType
;
203 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
205 public Class
<String
> getTypeClass(){