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.logging.log4j.LogManager;import org.apache.logging.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.Language;
|
28
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
29
|
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
|
30
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
31
|
|
32
|
/**
|
33
|
* This class maps a database attribute to CDM annotation added to the target class
|
34
|
* TODO maybe this class should not inherit from DbSingleAttributeImportMapperBase
|
35
|
* as it does not map to a single attribute
|
36
|
* @author a.mueller
|
37
|
* @since 01.03.2010
|
38
|
*/
|
39
|
public class DbImportAnnotationMapper
|
40
|
extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, AnnotatableEntity>{
|
41
|
|
42
|
private static final Logger logger = LogManager.getLogger(DbImportAnnotationMapper.class);
|
43
|
|
44
|
private MarkerType ifNullMarkerType;
|
45
|
|
46
|
/**
|
47
|
* FIXME Warning: the annotation type creation is not yet implemented
|
48
|
* @param dbAttributeString
|
49
|
* @param uuid
|
50
|
* @param label
|
51
|
* @param text
|
52
|
* @param labelAbbrev
|
53
|
* @return
|
54
|
*/
|
55
|
@Deprecated
|
56
|
public static DbImportAnnotationMapper NewInstance(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev){
|
57
|
Language language = null;
|
58
|
AnnotationType annotationType = null;
|
59
|
return new DbImportAnnotationMapper(dbAttributeString, language, annotationType, null);
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* @param dbAttributeString
|
64
|
* @param annotationType
|
65
|
* @param ifNullMarkerType the type of marker to set if the annotation value is null
|
66
|
* @return
|
67
|
*/
|
68
|
public static DbImportAnnotationMapper NewInstance(String dbAttributeString, AnnotationType annotationType,
|
69
|
MarkerType ifNullMarkerType) {
|
70
|
return new DbImportAnnotationMapper(dbAttributeString, null, annotationType, ifNullMarkerType);
|
71
|
}
|
72
|
|
73
|
/**
|
74
|
* * FIXME Warning: the annotation type creation is not yet implemented
|
75
|
* @param dbAttributeString
|
76
|
* @param uuid
|
77
|
* @param label
|
78
|
* @param text
|
79
|
* @param labelAbbrev
|
80
|
* @return
|
81
|
*/
|
82
|
public static DbImportAnnotationMapper NewInstance(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev, Language language){
|
83
|
AnnotationType annotationType = null;
|
84
|
return new DbImportAnnotationMapper(dbAttributeString, language, annotationType, null);
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* @param dbAttributeString
|
89
|
* @param annotationType
|
90
|
* @return
|
91
|
*/
|
92
|
public static DbImportAnnotationMapper NewInstance(String dbAttributeString, AnnotationType annotationType){
|
93
|
Language language = null;
|
94
|
return NewInstance(dbAttributeString, annotationType, language);
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* @param dbAttributeString
|
99
|
* @param annotationType
|
100
|
* @param language
|
101
|
* @return
|
102
|
*/
|
103
|
public static DbImportAnnotationMapper NewInstance(String dbAttributeString, AnnotationType annotationType, Language language){
|
104
|
return new DbImportAnnotationMapper(dbAttributeString, language, annotationType, null);
|
105
|
}
|
106
|
|
107
|
|
108
|
private AnnotationType annotationType;
|
109
|
private Language language;
|
110
|
|
111
|
|
112
|
private DbImportAnnotationMapper(String dbAttributeString, Language language, AnnotationType annotationType,
|
113
|
MarkerType ifNullMarkerType) {
|
114
|
super(dbAttributeString, dbAttributeString);
|
115
|
this.language = language;
|
116
|
this.annotationType = annotationType;
|
117
|
this.ifNullMarkerType = ifNullMarkerType;
|
118
|
}
|
119
|
|
120
|
@Override
|
121
|
public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
|
122
|
importMapperHelper.initialize(state, destinationClass);
|
123
|
}
|
124
|
|
125
|
// /**
|
126
|
// * @param service
|
127
|
// * @param state
|
128
|
// * @param tableName
|
129
|
// */
|
130
|
// public void initialize(ITermService service, DbImportStateBase state, Class<? extends CdmBase> destinationClass) {
|
131
|
// importMapperHelper.initialize(state, destinationClass);
|
132
|
// try {
|
133
|
// if ( checkDbColumnExists()){
|
134
|
// if (this.annotationType == null){
|
135
|
// this.annotationType = getAnnotationType(service, uuid, label, text, labelAbbrev);
|
136
|
// }
|
137
|
// }else{
|
138
|
// ignore = true;
|
139
|
// }
|
140
|
// } catch (MethodNotSupportedException e) {
|
141
|
// //do nothing
|
142
|
// }
|
143
|
// }
|
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
|
@Override
|
155
|
protected AnnotatableEntity doInvoke(AnnotatableEntity annotatableEntity, Object dbValue){
|
156
|
String strAnnotation = (String)dbValue;
|
157
|
if (StringUtils.isNotBlank(strAnnotation)){
|
158
|
Annotation annotation = Annotation.NewInstance(strAnnotation.trim(), annotationType, language);
|
159
|
if (annotatableEntity != null){
|
160
|
annotatableEntity.addAnnotation(annotation);
|
161
|
}
|
162
|
}else if(this.ifNullMarkerType != null){
|
163
|
annotatableEntity.addMarker(ifNullMarkerType, true);
|
164
|
}
|
165
|
return annotatableEntity;
|
166
|
}
|
167
|
|
168
|
protected AnnotationType getAnnotationType(CdmImportBase<?, ?> currentImport, UUID uuid, String label, String text, String labelAbbrev){
|
169
|
ITermService termService = currentImport.getTermService();
|
170
|
AnnotationType annotationType = (AnnotationType)termService.find(uuid);
|
171
|
if (annotationType == null){
|
172
|
annotationType = AnnotationType.NewInstance(text, label, labelAbbrev);
|
173
|
annotationType.setUuid(uuid);
|
174
|
//set vocabulary //TODO allow user defined vocabularies
|
175
|
UUID uuidAnnotationTypeVocabulary = UUID.fromString("ca04609b-1ba0-4d31-9c2e-aa8eb2f4e62d");
|
176
|
IVocabularyService vocService = currentImport.getVocabularyService();
|
177
|
@SuppressWarnings("unchecked")
|
178
|
TermVocabulary<DefinedTermBase<?>> voc = vocService.find(uuidAnnotationTypeVocabulary);
|
179
|
if (voc != null){
|
180
|
voc.addTerm(annotationType);
|
181
|
}else{
|
182
|
logger.warn("Could not find default annotation type vocabulary. Vocabulary not set for new annotation type.");
|
183
|
}
|
184
|
//savetermService.save(annotationType);
|
185
|
}
|
186
|
return annotationType;
|
187
|
}
|
188
|
|
189
|
|
190
|
//not used
|
191
|
@Override
|
192
|
public Class<String> getTypeClass(){
|
193
|
return String.class;
|
194
|
}
|
195
|
}
|