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