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