Skip to content

Commit fbaba05

Browse files
author
a-brandt
committed
added missing revision, id, and key update after create, replace and update
1 parent a2c7208 commit fbaba05

File tree

9 files changed

+797
-270
lines changed

9 files changed

+797
-270
lines changed

src/main/java/com/arangodb/entity/BaseDocument.java

Lines changed: 225 additions & 206 deletions
Large diffs are not rendered by default.

src/main/java/com/arangodb/entity/EdgeEntity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
*/
2626
public class EdgeEntity<T> extends DocumentEntity<T> {
2727

28-
@SerializedName("_from")
28+
@SerializedName(BaseDocument.FROM)
2929
String fromVertexHandle;
30-
@SerializedName("_to")
30+
@SerializedName(BaseDocument.TO)
3131
String toVertexHandle;
3232

3333
public String getFromVertexHandle() {
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.arangodb.impl;
2+
3+
import java.lang.annotation.Annotation;
4+
import java.lang.reflect.Field;
5+
import java.util.ArrayList;
6+
import java.util.HashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
import com.arangodb.entity.BaseDocument;
14+
import com.google.gson.annotations.SerializedName;
15+
16+
public class AnnotationHandler {
17+
18+
private static Logger logger = LoggerFactory.getLogger(AnnotationHandler.class);
19+
20+
static class DocumentAttributes {
21+
public Field rev;
22+
public Field id;
23+
public Field key;
24+
public Field from;
25+
public Field to;
26+
}
27+
28+
static Map<Class<?>, DocumentAttributes> class2DocumentAttributes;
29+
30+
static {
31+
class2DocumentAttributes = new HashMap<Class<?>, DocumentAttributes>();
32+
}
33+
34+
public AnnotationHandler() {
35+
}
36+
37+
@SuppressWarnings({ "unchecked", "rawtypes" })
38+
public synchronized void updateDocumentAttributes(Object o, long rev, String id, String key) {
39+
if (o == null) {
40+
41+
} else if (o instanceof java.util.Map) {
42+
java.util.Map m = (java.util.Map) o;
43+
m.put(BaseDocument.ID, id);
44+
m.put(BaseDocument.KEY, key);
45+
m.put(BaseDocument.REV, rev);
46+
} else {
47+
DocumentAttributes documentAttributes = getDocumentAttributes(o);
48+
setAttribute(documentAttributes.id, o, id);
49+
setAttribute(documentAttributes.key, o, key);
50+
setAttribute(documentAttributes.rev, o, rev);
51+
}
52+
}
53+
54+
@SuppressWarnings({ "unchecked", "rawtypes" })
55+
public synchronized void updateDocumentRev(Object o, long rev) {
56+
if (o == null) {
57+
58+
} else if (o instanceof java.util.Map) {
59+
java.util.Map m = (java.util.Map) o;
60+
m.put(BaseDocument.REV, rev);
61+
} else {
62+
DocumentAttributes documentAttributes = getDocumentAttributes(o);
63+
setAttribute(documentAttributes.rev, o, rev);
64+
}
65+
}
66+
67+
@SuppressWarnings({ "unchecked", "rawtypes" })
68+
public synchronized void updateEdgeAttributes(Object o, long rev, String id, String key, String from, String to) {
69+
if (o == null) {
70+
71+
} else if (o instanceof java.util.Map) {
72+
java.util.Map m = (java.util.Map) o;
73+
m.put(BaseDocument.ID, id);
74+
m.put(BaseDocument.KEY, key);
75+
m.put(BaseDocument.REV, rev);
76+
m.put(BaseDocument.FROM, from);
77+
m.put(BaseDocument.TO, to);
78+
} else {
79+
DocumentAttributes documentAttributes = getDocumentAttributes(o);
80+
setAttribute(documentAttributes.id, o, id);
81+
setAttribute(documentAttributes.key, o, key);
82+
setAttribute(documentAttributes.rev, o, rev);
83+
setAttribute(documentAttributes.from, o, from);
84+
setAttribute(documentAttributes.to, o, to);
85+
}
86+
}
87+
88+
private void setAttribute(Field field, Object o, Object value) {
89+
if (field != null) {
90+
try {
91+
field.setAccessible(true);
92+
field.set(o, value);
93+
} catch (Throwable e) {
94+
logger.error("could not update document attribute of class " + value.getClass().getCanonicalName(), e);
95+
}
96+
}
97+
}
98+
99+
private DocumentAttributes getDocumentAttributes(Object o) {
100+
Class<? extends Object> clazz = o.getClass();
101+
DocumentAttributes documentAttributes = class2DocumentAttributes.get(clazz);
102+
103+
if (documentAttributes == null) {
104+
documentAttributes = new DocumentAttributes();
105+
documentAttributes.id = getFieldByAnnotationValue(clazz, BaseDocument.ID);
106+
documentAttributes.key = getFieldByAnnotationValue(clazz, BaseDocument.KEY);
107+
documentAttributes.rev = getFieldByAnnotationValue(clazz, BaseDocument.REV);
108+
documentAttributes.from = getFieldByAnnotationValue(clazz, BaseDocument.FROM);
109+
documentAttributes.to = getFieldByAnnotationValue(clazz, BaseDocument.TO);
110+
class2DocumentAttributes.put(clazz, documentAttributes);
111+
}
112+
113+
return documentAttributes;
114+
}
115+
116+
private Field getFieldByAnnotationValue(Class<?> clazz, String value) {
117+
118+
List<Field> fields = getAllDeclaredFields(clazz);
119+
for (Field field : fields) {
120+
121+
Annotation[] annotations = field.getAnnotations();
122+
for (Annotation annotation : annotations) {
123+
124+
if (annotation instanceof SerializedName) {
125+
SerializedName sn = (SerializedName) annotation;
126+
127+
if (value.equals(sn.value())) {
128+
return field;
129+
}
130+
}
131+
}
132+
}
133+
134+
return null;
135+
}
136+
137+
private List<Field> getAllDeclaredFields(Class<?> clazz) {
138+
List<Field> result = new ArrayList<Field>();
139+
140+
Class<?> current = clazz;
141+
142+
while (current != null) {
143+
Field[] fields = current.getDeclaredFields();
144+
for (Field field : fields) {
145+
result.add(field);
146+
}
147+
current = current.getSuperclass();
148+
}
149+
return result;
150+
}
151+
152+
}

src/main/java/com/arangodb/impl/BaseArangoDriverImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ class BaseArangoDriverImpl extends BaseArangoDriver implements BaseDriverInterfa
2828

2929
protected ArangoConfigure configure;
3030
protected HttpManager httpManager;
31+
protected AnnotationHandler annotationHandler;
3132

3233
BaseArangoDriverImpl(ArangoConfigure configure, HttpManager httpManager) {
3334
this.configure = configure;
3435
this.httpManager = httpManager;
36+
this.annotationHandler = new AnnotationHandler();
3537
}
3638

3739
@Override

src/main/java/com/arangodb/impl/InternalDocumentDriverImpl.java

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import com.arangodb.ArangoConfigure;
2424
import com.arangodb.ArangoException;
25+
import com.arangodb.entity.BaseDocument;
2526
import com.arangodb.entity.DefaultEntity;
2627
import com.arangodb.entity.DocumentEntity;
2728
import com.arangodb.entity.DocumentsEntity;
@@ -60,20 +61,24 @@ private <T> DocumentEntity<T> _createDocument(
6061
} else if (documentKey != null) {
6162
JsonElement elem = EntityFactory.toJsonElement(value, false);
6263
if (elem.isJsonObject()) {
63-
elem.getAsJsonObject().addProperty("_key", documentKey);
64+
elem.getAsJsonObject().addProperty(BaseDocument.KEY, documentKey);
6465
}
6566
body = EntityFactory.toJsonString(elem);
6667
} else {
6768
body = EntityFactory.toJsonString(value);
6869
}
6970

70-
HttpResponseEntity res = httpManager.doPost(
71-
createEndpointUrl(database, "/_api/document"),
71+
HttpResponseEntity res = httpManager.doPost(createEndpointUrl(database, "/_api/document"),
7272
new MapBuilder().put("collection", collectionName).put("createCollection", createCollection)
73-
.put("waitForSync", waitForSync).get(), body);
73+
.put("waitForSync", waitForSync).get(),
74+
body);
7475

7576
@SuppressWarnings("unchecked")
7677
DocumentEntity<T> result = createEntity(res, DocumentEntity.class);
78+
79+
annotationHandler.updateDocumentAttributes(value, result.getDocumentRevision(), result.getDocumentHandle(),
80+
result.getDocumentKey());
81+
7782
result.setEntity(value);
7883
return result;
7984
}
@@ -113,12 +118,14 @@ public <T> DocumentEntity<T> replaceDocument(
113118

114119
validateDocumentHandle(documentHandle);
115120
HttpResponseEntity res = httpManager.doPut(
116-
createEndpointUrl(database, "/_api/document", documentHandle),
117-
new MapBuilder().put("rev", rev).put("policy", policy == null ? null : policy.name())
118-
.put("waitForSync", waitForSync).get(), EntityFactory.toJsonString(value));
119-
120-
return createEntity(res, DocumentEntity.class);
121+
createEndpointUrl(database, "/_api/document", documentHandle), new MapBuilder().put("rev", rev)
122+
.put("policy", policy == null ? null : policy.name()).put("waitForSync", waitForSync).get(),
123+
EntityFactory.toJsonString(value));
121124

125+
DocumentEntity<T> result = createEntity(res, DocumentEntity.class);
126+
annotationHandler.updateDocumentRev(value, result.getDocumentRevision());
127+
result.setEntity(value);
128+
return result;
122129
}
123130

124131
@Override
@@ -132,16 +139,17 @@ public <T> DocumentEntity<T> updateDocument(
132139
Boolean keepNull) throws ArangoException {
133140

134141
validateDocumentHandle(documentHandle);
135-
HttpResponseEntity res = httpManager.doPatch(
136-
createEndpointUrl(database, "/_api/document", documentHandle),
142+
HttpResponseEntity res = httpManager.doPatch(createEndpointUrl(database, "/_api/document", documentHandle),
137143
new MapBuilder().put("rev", rev).put("policy", policy == null ? null : policy.name())
138144
.put("waitForSync", waitForSync).put("keepNull", keepNull).get(),
139145
EntityFactory.toJsonString(value, keepNull != null && !keepNull));
140146

141147
@SuppressWarnings("unchecked")
142-
DocumentEntity<T> entity = createEntity(res, DocumentEntity.class);
143-
return entity;
144-
148+
DocumentEntity<T> result = createEntity(res, DocumentEntity.class);
149+
annotationHandler.updateDocumentAttributes(value, result.getDocumentRevision(), result.getDocumentHandle(),
150+
result.getDocumentKey());
151+
result.setEntity(value);
152+
return result;
145153
}
146154

147155
private static final String API_DOCUMENT_PREFIX = "/_api/document/";
@@ -150,8 +158,8 @@ public <T> DocumentEntity<T> updateDocument(
150158
public List<String> getDocuments(String database, String collectionName, boolean handleConvert)
151159
throws ArangoException {
152160

153-
HttpResponseEntity res = httpManager.doGet(createEndpointUrl(database, "/_api/document"), new MapBuilder(
154-
"collection", collectionName).get());
161+
HttpResponseEntity res = httpManager.doGet(createEndpointUrl(database, "/_api/document"),
162+
new MapBuilder("collection", collectionName).get());
155163

156164
DocumentsEntity entity = createEntity(res, DocumentsEntity.class);
157165
List<String> documents = CollectionUtils.safety(entity.getDocuments());
@@ -171,8 +179,8 @@ public List<String> getDocuments(String database, String collectionName, boolean
171179
@Override
172180
public long checkDocument(String database, String documentHandle) throws ArangoException {
173181
validateDocumentHandle(documentHandle);
174-
HttpResponseEntity res = httpManager
175-
.doHead(createEndpointUrl(database, "/_api/document", documentHandle), null);
182+
HttpResponseEntity res = httpManager.doHead(createEndpointUrl(database, "/_api/document", documentHandle),
183+
null);
176184

177185
DefaultEntity entity = createEntity(res, DefaultEntity.class);
178186
return entity.getEtag();
@@ -205,8 +213,8 @@ public DocumentEntity<?> deleteDocument(String database, String documentHandle,
205213

206214
validateDocumentHandle(documentHandle);
207215
HttpResponseEntity res = httpManager.doDelete(createEndpointUrl(database, "/_api/document", documentHandle),
208-
new MapBuilder().put("rev", rev)
209-
.put("policy", policy == null ? null : policy.name().toLowerCase(Locale.US)).get());
216+
new MapBuilder().put("rev", rev).put("policy", policy == null ? null : policy.name().toLowerCase(Locale.US))
217+
.get());
210218

211219
try {
212220
DocumentEntity<?> entity = createEntity(res, DocumentEntity.class);

0 commit comments

Comments
 (0)