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
;
14 import java
.util
.UUID
;
16 import org
.apache
.commons
.lang3
.StringUtils
;
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
.io
.common
.CdmImportBase
;
22 import eu
.etaxonomy
.cdm
.io
.common
.DbImportStateBase
;
23 import eu
.etaxonomy
.cdm
.model
.common
.AnnotatableEntity
;
24 import eu
.etaxonomy
.cdm
.model
.common
.Annotation
;
25 import eu
.etaxonomy
.cdm
.model
.common
.AnnotationType
;
26 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
27 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
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
38 public class DbImportAnnotationMapper
extends DbSingleAttributeImportMapperBase
<DbImportStateBase
<?
,?
>, AnnotatableEntity
> implements IDbImportMapper
<DbImportStateBase
<?
,?
>,AnnotatableEntity
>{
39 private static final Logger logger
= Logger
.getLogger(DbImportAnnotationMapper
.class);
42 * FIXME Warning: the annotation type creation is not yet implemented
43 * @param dbAttributeString
50 public static DbImportAnnotationMapper
NewInstance(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
){
51 Language language
= null;
52 AnnotationType annotationType
= null;
53 return new DbImportAnnotationMapper(dbAttributeString
, language
, annotationType
);
57 * * FIXME Warning: the annotation type creation is not yet implemented
58 * @param dbAttributeString
65 public static DbImportAnnotationMapper
NewInstance(String dbAttributeString
, UUID uuid
, String label
, String text
, String labelAbbrev
, Language language
){
66 AnnotationType annotationType
= null;
67 return new DbImportAnnotationMapper(dbAttributeString
, language
, annotationType
);
71 * @param dbAttributeString
72 * @param annotationType
75 public static DbImportAnnotationMapper
NewInstance(String dbAttributeString
, AnnotationType annotationType
){
76 Language language
= null;
77 return NewInstance(dbAttributeString
, annotationType
, language
);
81 * @param dbAttributeString
82 * @param annotationType
86 public static DbImportAnnotationMapper
NewInstance(String dbAttributeString
, AnnotationType annotationType
, Language language
){
87 return new DbImportAnnotationMapper(dbAttributeString
, language
, annotationType
);
91 private AnnotationType annotationType
;
92 private Language language
;
95 * @param dbAttributeString
101 private DbImportAnnotationMapper(String dbAttributeString
, Language language
, AnnotationType annotationType
) {
102 super(dbAttributeString
, dbAttributeString
);
103 this.language
= language
;
104 this.annotationType
= annotationType
;
108 public void initialize(DbImportStateBase
<?
,?
> state
, Class
<?
extends CdmBase
> destinationClass
) {
109 importMapperHelper
.initialize(state
, destinationClass
);
115 // * @param tableName
117 // public void initialize(ITermService service, DbImportStateBase state, Class<? extends CdmBase> destinationClass) {
118 // importMapperHelper.initialize(state, destinationClass);
120 // if ( checkDbColumnExists()){
121 // if (this.annotationType == null){
122 // this.annotationType = getAnnotationType(service, uuid, label, text, labelAbbrev);
127 // } catch (MethodNotSupportedException e) {
133 public AnnotatableEntity
invoke(ResultSet rs
, AnnotatableEntity annotatableEntity
) throws SQLException
{
135 return annotatableEntity
;
137 String dbValue
= rs
.getString(getSourceAttribute());
138 return doInvoke(annotatableEntity
, dbValue
);
142 protected AnnotatableEntity
doInvoke(AnnotatableEntity annotatableEntity
, Object dbValue
){
143 String strAnnotation
= (String
)dbValue
;
144 if (StringUtils
.isNotBlank(strAnnotation
)){
145 Annotation annotation
= Annotation
.NewInstance(strAnnotation
, annotationType
, language
);
146 if (annotatableEntity
!= null){
147 annotatableEntity
.addAnnotation(annotation
);
150 return annotatableEntity
;
161 protected AnnotationType
getAnnotationType(CdmImportBase
<?
, ?
> currentImport
, UUID uuid
, String label
, String text
, String labelAbbrev
){
162 ITermService termService
= currentImport
.getTermService();
163 AnnotationType annotationType
= (AnnotationType
)termService
.find(uuid
);
164 if (annotationType
== null){
165 annotationType
= AnnotationType
.NewInstance(text
, label
, labelAbbrev
);
166 annotationType
.setUuid(uuid
);
167 //set vocabulary //TODO allow user defined vocabularies
168 UUID uuidAnnotationTypeVocabulary
= UUID
.fromString("ca04609b-1ba0-4d31-9c2e-aa8eb2f4e62d");
169 IVocabularyService vocService
= currentImport
.getVocabularyService();
170 @SuppressWarnings("unchecked")
171 TermVocabulary
<DefinedTermBase
<?
>> voc
= vocService
.find(uuidAnnotationTypeVocabulary
);
173 voc
.addTerm(annotationType
);
175 logger
.warn("Could not find default annotation type vocabulary. Vocabulary not set for new annotation type.");
177 //savetermService.save(annotationType);
179 return annotationType
;
185 public Class
<String
> getTypeClass(){