1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.io.common.mapping;
|
11
|
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.commons.lang3.StringUtils;
|
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.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;
|
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
|
* @since 01.03.2010
|
37
|
*/
|
38
|
public class DbImportAnnotationMapper extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, AnnotatableEntity> implements IDbImportMapper<DbImportStateBase<?,?>,AnnotatableEntity>{
|
39
|
private static final Logger logger = Logger.getLogger(DbImportAnnotationMapper.class);
|
40
|
|
41
|
/**
|
42
|
* FIXME Warning: the annotation type creation is not yet implemented
|
43
|
* @param dbAttributeString
|
44
|
* @param uuid
|
45
|
* @param label
|
46
|
* @param text
|
47
|
* @param labelAbbrev
|
48
|
* @return
|
49
|
*/
|
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);
|
54
|
}
|
55
|
|
56
|
/**
|
57
|
* * FIXME Warning: the annotation type creation is not yet implemented
|
58
|
* @param dbAttributeString
|
59
|
* @param uuid
|
60
|
* @param label
|
61
|
* @param text
|
62
|
* @param labelAbbrev
|
63
|
* @return
|
64
|
*/
|
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);
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* @param dbAttributeString
|
72
|
* @param annotationType
|
73
|
* @return
|
74
|
*/
|
75
|
public static DbImportAnnotationMapper NewInstance(String dbAttributeString, AnnotationType annotationType){
|
76
|
Language language = null;
|
77
|
return NewInstance(dbAttributeString, annotationType, language);
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* @param dbAttributeString
|
82
|
* @param annotationType
|
83
|
* @param language
|
84
|
* @return
|
85
|
*/
|
86
|
public static DbImportAnnotationMapper NewInstance(String dbAttributeString, AnnotationType annotationType, Language language){
|
87
|
return new DbImportAnnotationMapper(dbAttributeString, language, annotationType);
|
88
|
}
|
89
|
|
90
|
|
91
|
private AnnotationType annotationType;
|
92
|
private Language language;
|
93
|
|
94
|
/**
|
95
|
* @param dbAttributeString
|
96
|
* @param uuid
|
97
|
* @param label
|
98
|
* @param text
|
99
|
* @param labelAbbrev
|
100
|
*/
|
101
|
private DbImportAnnotationMapper(String dbAttributeString, Language language, AnnotationType annotationType) {
|
102
|
super(dbAttributeString, dbAttributeString);
|
103
|
this.language = language;
|
104
|
this.annotationType = annotationType;
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
|
109
|
importMapperHelper.initialize(state, destinationClass);
|
110
|
}
|
111
|
|
112
|
// /**
|
113
|
// * @param service
|
114
|
// * @param state
|
115
|
// * @param tableName
|
116
|
// */
|
117
|
// public void initialize(ITermService service, DbImportStateBase state, Class<? extends CdmBase> destinationClass) {
|
118
|
// importMapperHelper.initialize(state, destinationClass);
|
119
|
// try {
|
120
|
// if ( checkDbColumnExists()){
|
121
|
// if (this.annotationType == null){
|
122
|
// this.annotationType = getAnnotationType(service, uuid, label, text, labelAbbrev);
|
123
|
// }
|
124
|
// }else{
|
125
|
// ignore = true;
|
126
|
// }
|
127
|
// } catch (MethodNotSupportedException e) {
|
128
|
// //do nothing
|
129
|
// }
|
130
|
// }
|
131
|
|
132
|
@Override
|
133
|
public AnnotatableEntity invoke(ResultSet rs, AnnotatableEntity annotatableEntity) throws SQLException {
|
134
|
if (ignore){
|
135
|
return annotatableEntity;
|
136
|
}
|
137
|
String dbValue = rs.getString(getSourceAttribute());
|
138
|
return doInvoke(annotatableEntity, dbValue);
|
139
|
}
|
140
|
|
141
|
@Override
|
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);
|
148
|
}
|
149
|
}
|
150
|
return annotatableEntity;
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* @param service
|
155
|
* @param uuid
|
156
|
* @param label
|
157
|
* @param text
|
158
|
* @param labelAbbrev
|
159
|
* @return
|
160
|
*/
|
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);
|
172
|
if (voc != null){
|
173
|
voc.addTerm(annotationType);
|
174
|
}else{
|
175
|
logger.warn("Could not find default annotation type vocabulary. Vocabulary not set for new annotation type.");
|
176
|
}
|
177
|
//savetermService.save(annotationType);
|
178
|
}
|
179
|
return annotationType;
|
180
|
}
|
181
|
|
182
|
|
183
|
//not used
|
184
|
/* (non-Javadoc)
|
185
|
* @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
|
186
|
*/
|
187
|
@Override
|
188
|
public Class<String> getTypeClass(){
|
189
|
return String.class;
|
190
|
}
|
191
|
|
192
|
|
193
|
|
194
|
|
195
|
|
196
|
}
|