|
17 | 17 | package org.springframework.data.couchbase.core.convert;
|
18 | 18 |
|
19 | 19 | import java.util.Collections;
|
20 |
| -import java.util.Map; |
21 | 20 |
|
22 | 21 | import org.springframework.beans.factory.InitializingBean;
|
23 | 22 | import org.springframework.core.convert.ConversionService;
|
@@ -107,64 +106,22 @@ public void afterPropertiesSet() {
|
107 | 106 | * @return
|
108 | 107 | */
|
109 | 108 | @Override
|
110 |
| - public Object convertForWriteIfNeeded(CouchbasePersistentProperty prop, ConvertingPropertyAccessor<Object> accessor) { |
| 109 | + public Object convertForWriteIfNeeded(CouchbasePersistentProperty prop, ConvertingPropertyAccessor<Object> accessor, |
| 110 | + boolean processValueConverter) { |
111 | 111 | Object value = accessor.getProperty(prop, prop.getType());
|
112 | 112 | if (value == null) {
|
113 | 113 | return null;
|
114 | 114 | }
|
115 |
| - if (conversions.hasValueConverter(prop)) { |
| 115 | + if (processValueConverter && conversions.hasValueConverter(prop)) { |
116 | 116 | CouchbaseDocument encrypted = (CouchbaseDocument) conversions.getPropertyValueConversions()
|
117 |
| - .getValueConverter(prop).write(value, new CouchbaseConversionContext(prop, (MappingCouchbaseConverter)this, accessor)); |
| 117 | + .getValueConverter(prop) |
| 118 | + .write(value, new CouchbaseConversionContext(prop, (MappingCouchbaseConverter) this, accessor)); |
118 | 119 | return encrypted;
|
119 | 120 | }
|
120 |
| - Class<?> targetClass = Object.class; |
| 121 | + Class<?> targetClass = this.conversions.getCustomWriteTarget(value.getClass()).orElse(null); |
121 | 122 |
|
122 |
| - if (prop.findAnnotation(com.couchbase.client.java.encryption.annotation.Encrypted.class) != null) { |
123 |
| - targetClass = Map.class; |
124 |
| - } |
125 |
| - boolean canConvert = this.conversionService.canConvert(new TypeDescriptor(prop.getField()), |
126 |
| - TypeDescriptor.valueOf(targetClass)); |
127 |
| - if (canConvert) { |
128 |
| - return this.conversionService.convert(value, new TypeDescriptor(prop.getField()), |
129 |
| - TypeDescriptor.valueOf(targetClass)); |
130 |
| - } |
131 |
| - |
132 |
| - Object result = this.conversions.getCustomWriteTarget(prop.getType()) // |
133 |
| - .map(it -> this.conversionService.convert(value, new TypeDescriptor(prop.getField()), |
134 |
| - TypeDescriptor.valueOf(it))) // |
135 |
| - .orElseGet(() -> Enum.class.isAssignableFrom(value.getClass()) ? ((Enum<?>) value).name() : value); |
136 |
| - |
137 |
| - return result; |
138 |
| - |
139 |
| - } |
140 |
| - |
141 |
| - /** |
142 |
| - * This convertForWriteIfNeeded takes a property and accessor so that the annotations can be accessed (ie. @Encrypted) |
143 |
| - * |
144 |
| - * @param prop the property to be converted to the class that would actually be stored. |
145 |
| - * @param accessor the property accessor |
146 |
| - * @return |
147 |
| - */ |
148 |
| -//@Override |
149 |
| - public Object convertForWriteIfNeeded2(CouchbasePersistentProperty prop, ConvertingPropertyAccessor<Object> accessor) { |
150 |
| - Object value = accessor.getProperty(prop, prop.getType()); |
151 |
| - if (value == null) { |
152 |
| - return null; |
153 |
| - } |
154 |
| - /* |
155 |
| - if (conversions.hasValueConverter(prop)) { |
156 |
| - CouchbaseDocument encrypted = (CouchbaseDocument) conversions.getPropertyValueConversions() |
157 |
| - .getValueConverter(prop).write(value, new CouchbaseConversionContext(prop, (MappingCouchbaseConverter)this, accessor)); |
158 |
| - return encrypted; |
159 |
| - } |
160 |
| - */ |
161 |
| - Class<?> targetClass = Object.class; |
162 |
| - |
163 |
| - if (prop.findAnnotation(com.couchbase.client.java.encryption.annotation.Encrypted.class) != null) { |
164 |
| - targetClass = Map.class; |
165 |
| - } |
166 |
| - boolean canConvert = this.conversionService.canConvert(new TypeDescriptor(prop.getField()), |
167 |
| - TypeDescriptor.valueOf(targetClass)); |
| 123 | + boolean canConvert = targetClass == null ? false |
| 124 | + : this.conversionService.canConvert(new TypeDescriptor(prop.getField()), TypeDescriptor.valueOf(targetClass)); |
168 | 125 | if (canConvert) {
|
169 | 126 | return this.conversionService.convert(value, new TypeDescriptor(prop.getField()),
|
170 | 127 | TypeDescriptor.valueOf(targetClass));
|
@@ -198,32 +155,13 @@ public Object convertForWriteIfNeeded(Object value) {
|
198 | 155 |
|
199 | 156 | }
|
200 | 157 |
|
201 |
| - /* TODO needed later |
202 |
| - @Override |
203 |
| - public Object convertToCouchbaseType(Object value, TypeInformation<?> typeInformation) { |
204 |
| - if (value == null) { |
205 |
| - return null; |
206 |
| - } |
207 |
| -
|
208 |
| - return this.conversions.getCustomWriteTarget(value.getClass()) // |
209 |
| - .map(it -> (Object) this.conversionService.convert(value, it)) // |
210 |
| - .orElseGet(() -> Enum.class.isAssignableFrom(value.getClass()) ? ((Enum<?>) value).name() : value); |
211 |
| -
|
212 |
| - } |
213 |
| -
|
214 |
| - @Override |
215 |
| - public Object convertToCouchbaseType(String source) { |
216 |
| - return source; |
217 |
| - } |
218 |
| - */ |
219 |
| - |
220 | 158 | @Override
|
221 | 159 | public Class<?> getWriteClassFor(Class<?> clazz) {
|
222 | 160 | return this.conversions.getCustomWriteTarget(clazz).orElse(clazz);
|
223 | 161 | }
|
224 | 162 |
|
225 | 163 | @Override
|
226 |
| - public CustomConversions getConversions(){ |
| 164 | + public CustomConversions getConversions() { |
227 | 165 | return conversions;
|
228 | 166 | }
|
229 | 167 | }
|
0 commit comments