adding map to ReferenceSystem
[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, 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, 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, language, annotationType);
93 }
94
95
96 private AnnotationType annotationType;
97 private Language language;
98
99 /**
100 * @param dbAttributeString
101 * @param uuid
102 * @param label
103 * @param text
104 * @param labelAbbrev
105 */
106 private DbImportAnnotationMapper(String dbAttributeString, Language language, AnnotationType annotationType) {
107 super(dbAttributeString, dbAttributeString);
108 this.language = language;
109 this.annotationType = annotationType;
110 }
111
112
113 /* (non-Javadoc)
114 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
115 */
116 @Override
117 public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
118 importMapperHelper.initialize(state, destinationClass);
119 }
120
121 // /**
122 // * @param service
123 // * @param state
124 // * @param tableName
125 // */
126 // public void initialize(ITermService service, DbImportStateBase state, Class<? extends CdmBase> destinationClass) {
127 // importMapperHelper.initialize(state, destinationClass);
128 // try {
129 // if ( checkDbColumnExists()){
130 // if (this.annotationType == null){
131 // this.annotationType = getAnnotationType(service, uuid, label, text, labelAbbrev);
132 // }
133 // }else{
134 // ignore = true;
135 // }
136 // } catch (MethodNotSupportedException e) {
137 // //do nothing
138 // }
139 // }
140
141
142 /* (non-Javadoc)
143 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
144 */
145 @Override
146 public AnnotatableEntity invoke(ResultSet rs, AnnotatableEntity annotatableEntity) throws SQLException {
147 if (ignore){
148 return annotatableEntity;
149 }
150 String dbValue = rs.getString(getSourceAttribute());
151 return doInvoke(annotatableEntity, dbValue);
152 }
153
154 /* (non-Javadoc)
155 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#doInvoke(eu.etaxonomy.cdm.model.common.CdmBase, java.lang.Object)
156 */
157 @Override
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);
164 }
165 }
166 return annotatableEntity;
167 }
168
169
170
171
172 /**
173 * @param service
174 * @param uuid
175 * @param label
176 * @param text
177 * @param labelAbbrev
178 * @return
179 */
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);
190 if (voc != null){
191 voc.addTerm(annotationType);
192 }else{
193 logger.warn("Could not find default annotation type vocabulary. Vocabulary not set for new annotation type.");
194 }
195 //savetermService.save(annotationType);
196 }
197 return annotationType;
198 }
199
200
201 //not used
202 /* (non-Javadoc)
203 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
204 */
205 public Class<String> getTypeClass(){
206 return String.class;
207 }
208
209
210
211
212
213 }