1
2
3
4
5
6
7
8
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
33
34
35
36
37
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
44
45
46
47
48
49
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
59
60
61
62
63
64
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
73
74
75
76 public static DbImportAnnotationMapper NewInstance(String dbAttributeString, AnnotationType annotationType){
77 Language language = null;
78 return NewInstance(dbAttributeString, annotationType, language);
79 }
80
81
82
83
84
85
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
105
106
107
108
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
122
123
124 @Override
125 public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
126 importMapperHelper.initialize(state, destinationClass);
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
163
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
182
183
184
185
186
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
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
204 }
205 return annotationType;
206 }
207
208
209
210
211
212
213 public Class<String> getTypeClass(){
214 return String.class;
215 }
216
217
218
219
220
221 }