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 javax.mail.MethodNotSupportedException;
18
19 import org.apache.log4j.Logger;
20
21 import eu.etaxonomy.cdm.api.service.ITermService;
22 import eu.etaxonomy.cdm.api.service.IVocabularyService;
23 import eu.etaxonomy.cdm.io.common.CdmImportBase;
24 import eu.etaxonomy.cdm.io.common.DbImportStateBase;
25 import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
26 import eu.etaxonomy.cdm.model.common.CdmBase;
27 import eu.etaxonomy.cdm.model.common.Marker;
28 import eu.etaxonomy.cdm.model.common.MarkerType;
29 import eu.etaxonomy.cdm.model.common.TermVocabulary;
30
31
32
33
34
35
36
37
38
39 public class DbImportMarkerMapper extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, AnnotatableEntity> implements IDbImportMapper<DbImportStateBase<?,?>,AnnotatableEntity>{
40 @SuppressWarnings("unused")
41 private static final Logger logger = Logger.getLogger(DbImportMarkerMapper.class);
42
43 /
44
45
46
47
48
49
50
51
52
53 public static DbImportMarkerMapper NewInstance(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev){
54 return new DbImportMarkerMapper(dbAttributeString, uuid, label, text, labelAbbrev);
55 }
56
57 public static DbImportMarkerMapper NewInstance(String dbAttributeString, MarkerType markerType){
58 return new DbImportMarkerMapper(dbAttributeString, markerType);
59 }
60
61 /
62
63 private MarkerType markerType;
64 private String label;
65 private String text;
66 private String labelAbbrev;
67 private UUID uuid;
68
69 /
70
71
72
73
74
75
76
77 private DbImportMarkerMapper(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev) {
78 super(dbAttributeString, dbAttributeString);
79 this.uuid = uuid;
80 this.label = label;
81 this.text = text;
82 this.labelAbbrev = labelAbbrev;
83 }
84
85
86
87
88
89 private DbImportMarkerMapper(String dbAttributeString, MarkerType markerType) {
90 super(dbAttributeString, dbAttributeString);
91 this.markerType = markerType;
92 }
93
94 /
95
96
97
98
99 @Override
100 public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
101 importMapperHelper.initialize(state, destinationClass);
102 CdmImportBase<?, ?> currentImport = state.getCurrentIO();
103 if (currentImport == null){
104 throw new IllegalStateException("Current import is not available. Please make sure the the state knows about the current import (state.setCurrentImport())) !");
105 }
106
107 try {
108 if ( checkDbColumnExists()){
109 if (this.markerType == null){
110 this.markerType = getMarkerType(currentImport, uuid, label, text, labelAbbrev);
111 }
112 }else{
113 ignore = true;
114 }
115 } catch (MethodNotSupportedException e) {
116
117 }
118 }
119
120
121
122
123
124
125
126 private boolean invoke(Boolean dbValue, CdmBase cdmBase){
127 if (ignore){
128 return true;
129 }
130 if (cdmBase instanceof AnnotatableEntity){
131 AnnotatableEntity annotatableEntity = (AnnotatableEntity) cdmBase;
132 invoke(dbValue, annotatableEntity);
133 return true;
134 }else{
135 throw new IllegalArgumentException("marked object must be of type annotatable entity.");
136 }
137
138 }
139
140
141
142
143 public AnnotatableEntity invoke(ResultSet rs, AnnotatableEntity annotatableEntity) throws SQLException {
144 Boolean dbValue = rs.getBoolean(getSourceAttribute());
145 return invoke(dbValue, annotatableEntity);
146 }
147
148
149
150
151
152
153 private AnnotatableEntity invoke(Boolean dbValue, AnnotatableEntity annotatableEntity){
154 if (ignore){
155 return annotatableEntity;
156 }
157 if (dbValue != null){
158 Marker.NewInstance(annotatableEntity, dbValue, this.markerType);
159 }
160 return annotatableEntity;
161 }
162
163
164
165
166
167
168
169
170
171
172 protected MarkerType getMarkerType(CdmImportBase<?, ?> currentImport, UUID uuid, String label, String text, String labelAbbrev){
173 ITermService termService = currentImport.getTermService();
174 MarkerType markerType = (MarkerType)termService.find(uuid);
175 if (markerType == null){
176
177 markerType = MarkerType.NewInstance(text, label, labelAbbrev);
178 markerType.setUuid(uuid);
179
180 UUID uuidMarkerTypeVocabulary = UUID.fromString("19dffff7-e142-429c-a420-5d28e4ebe305");
181 IVocabularyService vocService = currentImport.getVocabularyService();
182 TermVocabulary voc = vocService.find(uuidMarkerTypeVocabulary);
183 if (voc != null){
184 voc.addTerm(markerType);
185 }else{
186 logger.warn("Could not find default markerType vocabulary. Vocabulary not set for new marker type.");
187 }
188
189 termService.save(markerType);
190 }
191 return markerType;
192 }
193
194
195
196
197
198
199 public Class<Boolean> getTypeClass(){
200 return Boolean.class;
201 }
202
203
204
205 }