17
17
package org .springframework .data .couchbase .core .convert ;
18
18
19
19
import java .util .Collections ;
20
+ import java .util .Map ;
20
21
21
22
import org .springframework .beans .factory .InitializingBean ;
22
23
import org .springframework .core .convert .ConversionService ;
23
24
import org .springframework .core .convert .TypeDescriptor ;
24
25
import org .springframework .core .convert .support .GenericConversionService ;
25
26
import org .springframework .data .convert .CustomConversions ;
27
+ import org .springframework .data .couchbase .core .mapping .CouchbaseDocument ;
26
28
import org .springframework .data .couchbase .core .mapping .CouchbasePersistentProperty ;
27
29
import org .springframework .data .mapping .model .ConvertingPropertyAccessor ;
28
30
import org .springframework .data .mapping .model .EntityInstantiators ;
@@ -99,7 +101,7 @@ public void afterPropertiesSet() {
99
101
100
102
/**
101
103
* This convertForWriteIfNeeded takes a property and accessor so that the annotations can be accessed (ie. @Encrypted)
102
- *
104
+ *
103
105
* @param prop the property to be converted to the class that would actually be stored.
104
106
* @param accessor the property accessor
105
107
* @return
@@ -110,6 +112,63 @@ public Object convertForWriteIfNeeded(CouchbasePersistentProperty prop, Converti
110
112
if (value == null ) {
111
113
return null ;
112
114
}
115
+ if (conversions .hasValueConverter (prop )) {
116
+ CouchbaseDocument encrypted = (CouchbaseDocument ) conversions .getPropertyValueConversions ()
117
+ .getValueConverter (prop ).write (value , new CouchbaseConversionContext (prop , (MappingCouchbaseConverter )this , accessor ));
118
+ return encrypted ;
119
+ }
120
+ Class <?> targetClass = Object .class ;
121
+
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 ));
168
+ if (canConvert ) {
169
+ return this .conversionService .convert (value , new TypeDescriptor (prop .getField ()),
170
+ TypeDescriptor .valueOf (targetClass ));
171
+ }
113
172
114
173
Object result = this .conversions .getCustomWriteTarget (prop .getType ()) //
115
174
.map (it -> this .conversionService .convert (value , new TypeDescriptor (prop .getField ()),
@@ -123,7 +182,7 @@ public Object convertForWriteIfNeeded(CouchbasePersistentProperty prop, Converti
123
182
/**
124
183
* This convertForWriteIfNeed takes only the value to convert. It cannot access the annotations of the Field being
125
184
* converted.
126
- *
185
+ *
127
186
* @param value the value to be converted to the class that would actually be stored.
128
187
* @return
129
188
*/
@@ -145,13 +204,13 @@ public Object convertToCouchbaseType(Object value, TypeInformation<?> typeInfor
145
204
if (value == null) {
146
205
return null;
147
206
}
148
-
207
+
149
208
return this.conversions.getCustomWriteTarget(value.getClass()) //
150
209
.map(it -> (Object) this.conversionService.convert(value, it)) //
151
210
.orElseGet(() -> Enum.class.isAssignableFrom(value.getClass()) ? ((Enum<?>) value).name() : value);
152
-
211
+
153
212
}
154
-
213
+
155
214
@Override
156
215
public Object convertToCouchbaseType(String source) {
157
216
return source;
@@ -162,4 +221,9 @@ public Object convertToCouchbaseType(String source) {
162
221
public Class <?> getWriteClassFor (Class <?> clazz ) {
163
222
return this .conversions .getCustomWriteTarget (clazz ).orElse (clazz );
164
223
}
224
+
225
+ @ Override
226
+ public CustomConversions getConversions (){
227
+ return conversions ;
228
+ }
165
229
}
0 commit comments