some generics
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / common / mapping / DbImportExtensionMapper.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
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 import org.springframework.transaction.TransactionStatus;
22
23 import eu.etaxonomy.cdm.api.service.ITermService;
24 import eu.etaxonomy.cdm.api.service.IVocabularyService;
25 import eu.etaxonomy.cdm.common.CdmUtils;
26 import eu.etaxonomy.cdm.io.common.CdmImportBase;
27 import eu.etaxonomy.cdm.io.common.DbImportStateBase;
28 import eu.etaxonomy.cdm.model.common.CdmBase;
29 import eu.etaxonomy.cdm.model.common.Extension;
30 import eu.etaxonomy.cdm.model.common.ExtensionType;
31 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
32 import eu.etaxonomy.cdm.model.common.TermVocabulary;
33
34 /**
35 * This class maps a database attribute to CDM extension added to the target class
36 * TODO maybe this class should not inherit from DbSingleAttributeImportMapperBase
37 * as it does not map to a single attribute
38 * @author a.mueller
39 * @created 12.05.2009
40 * @version 1.0
41 */
42 public class DbImportExtensionMapper extends DbSingleAttributeImportMapperBase<DbImportStateBase<?,?>, IdentifiableEntity> implements IDbImportMapper<DbImportStateBase<?,?>,IdentifiableEntity>{
43 private static final Logger logger = Logger.getLogger(DbImportExtensionMapper.class);
44
45 //************************** FACTORY METHODS ***************************************************************/
46
47 /**
48 * @param dbAttributeString
49 * @param uuid
50 * @param label
51 * @param text
52 * @param labelAbbrev
53 * @return
54 */
55 @Deprecated
56 public static DbImportExtensionMapper NewInstance(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev){
57 return new DbImportExtensionMapper(dbAttributeString, uuid, label, text, labelAbbrev);
58 }
59
60 public static DbImportExtensionMapper NewInstance(String dbAttributeString, ExtensionType extensionType){
61 return new DbImportExtensionMapper(dbAttributeString, extensionType);
62 }
63
64 //***************** VARIABLES **********************************************************/
65
66 private ExtensionType extensionType;
67 private String label;
68 private String text;
69 private String labelAbbrev;
70 private UUID uuid;
71
72 //******************************** CONSTRUCTOR *****************************************************************/
73 /**
74 * @param dbAttributeString
75 * @param uuid
76 * @param label
77 * @param text
78 * @param labelAbbrev
79 */
80 private DbImportExtensionMapper(String dbAttributeString, UUID uuid, String label, String text, String labelAbbrev) {
81 super(dbAttributeString, dbAttributeString);
82 this.uuid = uuid;
83 this.label = label;
84 this.text = text;
85 this.labelAbbrev = labelAbbrev;
86 }
87
88 /**
89 * @param dbAttributeString
90 * @param extensionType
91 */
92 private DbImportExtensionMapper(String dbAttributeString, ExtensionType extensionType) {
93 super(dbAttributeString, dbAttributeString);
94 this.extensionType = extensionType;
95 }
96
97 //****************************** METHODS ***************************************************/
98
99 /* (non-Javadoc)
100 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#initialize(eu.etaxonomy.cdm.io.common.DbImportStateBase, java.lang.Class)
101 */
102 @Override
103 public void initialize(DbImportStateBase<?,?> state, Class<? extends CdmBase> destinationClass) {
104 importMapperHelper.initialize(state, destinationClass);
105 CdmImportBase<?, ?> currentImport = state.getCurrentIO();
106 if (currentImport == null){
107 throw new IllegalStateException("Current import is not available. Please make sure the the state knows about the current import (state.setCurrentImport())) !");
108 }
109
110 try {
111 if ( checkDbColumnExists()){
112 if (this.extensionType == null){
113 this.extensionType = getExtensionType(currentImport, uuid, label, text, labelAbbrev);
114 }
115 }else{
116 ignore = true;
117 }
118 } catch (MethodNotSupportedException e) {
119 //do nothing - checkDbColumnExists is not possible
120 }
121 }
122
123
124 /**
125 * @param valueMap
126 * @param cdmBase
127 * @return
128 */
129 public boolean invoke(Map<String, Object> valueMap, CdmBase cdmBase){
130 Object dbValueObject = valueMap.get(this.getSourceAttribute().toLowerCase());
131 String dbValue = dbValueObject == null? null: dbValueObject.toString();
132 return invoke(dbValue, cdmBase);
133 }
134
135 /**
136 * @param dbValue
137 * @param cdmBase
138 * @return
139 */
140 private boolean invoke(String dbValue, CdmBase cdmBase){
141 if (ignore){
142 return true;
143 }
144 if (cdmBase instanceof IdentifiableEntity){
145 IdentifiableEntity identifiableEntity = (IdentifiableEntity) cdmBase;
146 invoke(dbValue, identifiableEntity);
147 return true;
148 }else{
149 throw new IllegalArgumentException("extended object must be of type identifiable entity.");
150 }
151
152 }
153
154 /* (non-Javadoc)
155 * @see eu.etaxonomy.cdm.io.common.mapping.DbSingleAttributeImportMapperBase#invoke(java.sql.ResultSet, eu.etaxonomy.cdm.model.common.CdmBase)
156 */
157 public IdentifiableEntity invoke(ResultSet rs, IdentifiableEntity identifiableEntity) throws SQLException {
158 String dbValue = rs.getString(getSourceAttribute());
159 return invoke(dbValue, identifiableEntity);
160 }
161
162 /**
163 * @param dbValue
164 * @param identifiableEntity
165 * @return
166 */
167 private IdentifiableEntity invoke(String dbValue, IdentifiableEntity identifiableEntity){
168 if (ignore){
169 return identifiableEntity;
170 }
171 if (CdmUtils.isNotEmpty(dbValue)){
172 Extension.NewInstance(identifiableEntity, dbValue, extensionType);
173 }
174 return identifiableEntity;
175 }
176
177
178 /**
179 * @param service
180 * @param uuid
181 * @param label
182 * @param text
183 * @param labelAbbrev
184 * @return
185 */
186 protected ExtensionType getExtensionType(CdmImportBase<?, ?> currentImport, UUID uuid, String label, String text, String labelAbbrev){
187 ITermService termService = currentImport.getTermService();
188 ExtensionType extensionType = (ExtensionType)termService.find(uuid);
189 if (extensionType == null){
190 extensionType = ExtensionType.NewInstance(text, label, labelAbbrev);
191 extensionType.setUuid(uuid);
192 //set vocabulary //TODO allow user defined vocabularies
193 UUID uuidExtensionTypeVocabulary = UUID.fromString("117cc307-5bd4-4b10-9b2f-2e14051b3b20");
194 IVocabularyService vocService = currentImport.getVocabularyService();
195 TermVocabulary voc = vocService.find(uuidExtensionTypeVocabulary);
196 //NEW
197 TransactionStatus tx = currentImport.startTransaction();
198 currentImport.getVocabularyService().saveOrUpdate(voc);
199 //END NEW
200 if (voc != null){
201 voc.addTerm(extensionType);
202 }else{
203 logger.warn("Could not find default extensionType vocabulary. Vocabulary not set for new extension type.");
204 }
205 //save
206 termService.save(extensionType);
207 //NEW
208 currentImport.commitTransaction(tx);
209 //END NEW
210 }
211 return extensionType;
212 }
213
214
215 //not used
216 /* (non-Javadoc)
217 * @see eu.etaxonomy.cdm.io.common.CdmSingleAttributeMapperBase#getTypeClass()
218 */
219 public Class<String> getTypeClass(){
220 return String.class;
221 }
222
223
224
225 }