minor
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportAnnotationMapper.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.cdm.io.common.mapping;
12
13 import java.sql.ResultSet;
14 import java.sql.SQLException;
15 import java.util.UUID;
16
17 import org.apache.log4j.Logger;
18
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;
30
31 /**
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
35 * @author a.mueller
36 * @created 01.03.2010
37 * @version 1.0
38 */
39 public class DbImportAnnotationMapper extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, AnnotatableEntity> implements IDbImportMapper<DbImportStateBase<?,?>,AnnotatableEntity>{
40 private static final Logger logger = Logger.getLogger(DbImportAnnotationMapper.class);
41
42 /**
43 * FIXME Warning: the annotation type creation is not yet implemented
44 * @param dbAttributeString
45 * @param uuid
46 * @param label
47 * @param text
48 * @param labelAbbrev
49 * @return
50 */
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);
55 }
56
57 /**
58 * * FIXME Warning: the annotation type creation is not yet implemented
59 * @param dbAttributeString
60 * @param uuid
61 * @param label
62 * @param text
63 * @param labelAbbrev
64 * @return
65 */
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);
69 }
70
71 /**
72 * @param dbAttributeString
73 * @param annotationType
74 * @return
75 */
76 public static DbImportAnnotationMapper NewInstance(String dbAttributeString, AnnotationType annotationType){
77 Language language = null;
78 return NewInstance(dbAttributeString, annotationType, language);
79 }
80
81 /**
82 * @param dbAttributeString
83 * @param annotationType
84 * @param language
85 * @return
86 */
87 public static DbImportAnnotationMapper NewInstance(String dbAttributeString, AnnotationType annotationType, Language language){
88 String label = null;
89 String text = null;
90 String labelAbbrev = null;
91 UUID uuid = null;
92 return new DbImportAnnotationMapper(dbAttributeString, uuid, label, text, labelAbbrev, language, annotationType);
93 }
94
95
96 private AnnotationType annotationType;
97 private String label;
98 private String text;
99 private String labelAbbrev;
100 private UUID uuid;
101 private Language language;
102
103 /**
104 * @param dbAttributeString
105 * @param uuid
106 * @param label
107 * @param text
108 * @param labelAbbrev
109 */
110 private DbImportAnnotationMapper(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev, Language language, AnnotationType annotationType) {
111 super(dbAttributeString, dbAttributeString);
112 this.uuid = uuid;
113 this.label = label;
114 this.text = text;
115 this.labelAbbrev = labelAbbrev;
116 this.language = language;
117 this.annotationType = annotationType;
118 }
119
120
121 /* (non-Javadoc)
122 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
123 */
124 @Override
125 public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
126 importMapperHelper.initialize(state, destinationClass);
127 }
128
129 // /**
130 // * @param service
131 // * @param state
132 // * @param tableName
133 // */
134 // public void initialize(ITermService service, DbImportStateBase state, Class<? extends CdmBase> destinationClass) {
135 // importMapperHelper.initialize(state, destinationClass);
136 // try {
137 // if ( checkDbColumnExists()){
138 // if (this.annotationType == null){
139 // this.annotationType = getAnnotationType(service, uuid, label, text, labelAbbrev);
140 // }
141 // }else{
142 // ignore = true;
143 // }
144 // } catch (MethodNotSupportedException e) {
145 // //do nothing
146 // }
147 // }
148
149
150 /* (non-Javadoc)
151 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
152 */
153 @Override
154 public AnnotatableEntity invoke(ResultSet rs, AnnotatableEntity annotatableEntity) throws SQLException {
155 if (ignore){
156 return annotatableEntity;
157 }
158 String dbValue = rs.getString(getSourceAttribute());
159 return doInvoke(annotatableEntity, dbValue);
160 }
161
162 /* (non-Javadoc)
163 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#doInvoke(eu.etaxonomy.cdm.model.common.CdmBase, java.lang.Object)
164 */
165 @Override
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);
172 }
173 }
174 return annotatableEntity;
175 }
176
177
178
179
180 /**
181 * @param service
182 * @param uuid
183 * @param label
184 * @param text
185 * @param labelAbbrev
186 * @return
187 */
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);
198 if (voc != null){
199 voc.addTerm(annotationType);
200 }else{
201 logger.warn("Could not find default annotation type vocabulary. Vocabulary not set for new annotation type.");
202 }
203 //savetermService.save(annotationType);
204 }
205 return annotationType;
206 }
207
208
209 //not used
210 /* (non-Javadoc)
211 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
212 */
213 public Class<String> getTypeClass(){
214 return String.class;
215 }
216
217
218
219
220
221 }