26
26
27
27
import org .springframework .data .couchbase .core .convert .CouchbaseConverter ;
28
28
import org .springframework .data .couchbase .core .convert .MappingCouchbaseConverter ;
29
- import org .springframework .data .couchbase .core .mapping .ConvertedCouchbaseDocument ;
29
+ import org .springframework .data .couchbase .core .convert .translation .JacksonTranslationService ;
30
+ import org .springframework .data .couchbase .core .convert .translation .TranslationService ;
31
+ import org .springframework .data .couchbase .core .mapping .CouchbaseDocument ;
30
32
import org .springframework .data .couchbase .core .mapping .CouchbasePersistentEntity ;
31
33
import org .springframework .data .couchbase .core .mapping .CouchbasePersistentProperty ;
34
+ import org .springframework .data .couchbase .core .mapping .CouchbaseStorable ;
32
35
import org .springframework .data .mapping .context .MappingContext ;
33
36
34
37
import com .couchbase .client .CouchbaseClient ;
@@ -45,6 +48,7 @@ public class CouchbaseTemplate implements CouchbaseOperations {
45
48
private static final Collection <String > ITERABLE_CLASSES ;
46
49
private final CouchbaseExceptionTranslator exceptionTranslator =
47
50
new CouchbaseExceptionTranslator ();
51
+ private final TranslationService <Object > translationService ;
48
52
49
53
static {
50
54
Set <String > iterableClasses = new HashSet <String >();
@@ -61,8 +65,9 @@ public CouchbaseTemplate(final CouchbaseClient client) {
61
65
public CouchbaseTemplate (final CouchbaseClient client ,
62
66
final CouchbaseConverter converter ) {
63
67
this .client = client ;
64
- this .couchbaseConverter = converter == null ? getDefaultConverter (client ) : converter ;
65
- this .mappingContext = this .couchbaseConverter .getMappingContext ();
68
+ couchbaseConverter = converter == null ? getDefaultConverter (client ) : converter ;
69
+ mappingContext = couchbaseConverter .getMappingContext ();
70
+ translationService = new JacksonTranslationService ();
66
71
}
67
72
68
73
private CouchbaseConverter getDefaultConverter (final CouchbaseClient client ) {
@@ -72,40 +77,47 @@ private CouchbaseConverter getDefaultConverter(final CouchbaseClient client) {
72
77
return converter ;
73
78
}
74
79
80
+ private Object translateEncode (final CouchbaseStorable source ) {
81
+ return translationService .encode (source );
82
+ }
83
+
84
+ private CouchbaseStorable translateDecode (final String source , final CouchbaseStorable target ) {
85
+ return translationService .decode (source , target );
86
+ }
87
+
75
88
public final void insert (final Object objectToSave ) {
76
89
ensureNotIterable (objectToSave );
77
90
78
- final ConvertedCouchbaseDocument converted =
79
- new ConvertedCouchbaseDocument ();
91
+ final CouchbaseDocument converted = new CouchbaseDocument ();
80
92
couchbaseConverter .write (objectToSave , converted );
93
+
81
94
execute (new BucketCallback <OperationFuture <Boolean >>() {
82
95
@ Override
83
96
public OperationFuture <Boolean > doInBucket () {
84
97
return client .add (
85
- converted .getId (), converted .getExpiry (), converted . getRawValue ( ));
98
+ converted .getId (), converted .getExpiration (), translateEncode ( converted ));
86
99
}
87
100
});
88
101
}
89
102
90
103
public final void insert (final Collection <? extends Object > batchToSave ) {
91
104
Iterator <? extends Object > iter = batchToSave .iterator ();
92
- while (iter .hasNext ()) {
105
+ while (iter .hasNext ()) {
93
106
insert (iter .next ());
94
107
}
95
108
}
96
109
97
110
public void save (final Object objectToSave ) {
98
111
ensureNotIterable (objectToSave );
99
112
100
- final ConvertedCouchbaseDocument converted =
101
- new ConvertedCouchbaseDocument ();
113
+ final CouchbaseDocument converted = new CouchbaseDocument ();
102
114
couchbaseConverter .write (objectToSave , converted );
103
115
104
116
execute (new BucketCallback <OperationFuture <Boolean >>() {
105
117
@ Override
106
118
public OperationFuture <Boolean > doInBucket () {
107
119
return client .set (
108
- converted .getId (), converted .getExpiry (), converted . getRawValue ( ));
120
+ converted .getId (), converted .getExpiration (), translateEncode ( converted ));
109
121
}
110
122
});
111
123
}
@@ -120,15 +132,14 @@ public void save(final Collection<? extends Object> batchToSave) {
120
132
public void update (final Object objectToSave ) {
121
133
ensureNotIterable (objectToSave );
122
134
123
- final ConvertedCouchbaseDocument converted =
124
- new ConvertedCouchbaseDocument ();
135
+ final CouchbaseDocument converted = new CouchbaseDocument ();
125
136
couchbaseConverter .write (objectToSave , converted );
126
137
127
138
execute (new BucketCallback <OperationFuture <Boolean >>() {
128
139
@ Override
129
140
public OperationFuture <Boolean > doInBucket () {
130
141
return client .replace (
131
- converted .getId (), converted .getExpiry (), converted . getRawValue ( ));
142
+ converted .getId (), converted .getExpiration (), translateEncode ( converted ));
132
143
}
133
144
});
134
145
@@ -152,9 +163,9 @@ public String doInBucket() {
152
163
if (result == null ) {
153
164
return null ;
154
165
}
155
-
156
- ConvertedCouchbaseDocument converted = new ConvertedCouchbaseDocument (id , result );
157
- return couchbaseConverter .read (entityClass , converted );
166
+
167
+ CouchbaseDocument converted = new CouchbaseDocument (id );
168
+ return couchbaseConverter .read (entityClass , ( CouchbaseDocument ) translateDecode ( result , converted ) );
158
169
}
159
170
160
171
@@ -173,9 +184,9 @@ public <T> List<T> findByView(final String designName, final String viewName,
173
184
174
185
List <T > result = new ArrayList <T >(response .size ());
175
186
for (ViewRow row : response ) {
176
- ConvertedCouchbaseDocument converted =
177
- new ConvertedCouchbaseDocument ( row . getId (), ( String ) row . getDocument ());
178
- result . add ( couchbaseConverter . read ( entityClass , converted ));
187
+ CouchbaseDocument converted = new CouchbaseDocument ( row . getId ());
188
+ result . add ( couchbaseConverter . read ( entityClass ,
189
+ ( CouchbaseDocument ) translateDecode (( String ) row . getDocument () , converted ) ));
179
190
}
180
191
181
192
return result ;
@@ -206,7 +217,7 @@ public OperationFuture<Boolean> doInBucket() {
206
217
return ;
207
218
}
208
219
209
- final ConvertedCouchbaseDocument converted = new ConvertedCouchbaseDocument ();
220
+ final CouchbaseDocument converted = new CouchbaseDocument ();
210
221
couchbaseConverter .write (objectToRemove , converted );
211
222
212
223
execute (new BucketCallback <OperationFuture <Boolean >>() {
@@ -257,7 +268,7 @@ protected final void ensureNotIterable(Object o) {
257
268
}
258
269
259
270
private RuntimeException potentiallyConvertRuntimeException (final RuntimeException ex ) {
260
- RuntimeException resolved = this . exceptionTranslator .translateExceptionIfPossible (ex );
271
+ RuntimeException resolved = exceptionTranslator .translateExceptionIfPossible (ex );
261
272
return resolved == null ? ex : resolved ;
262
273
}
263
274
0 commit comments