Skip to content

Commit 6172559

Browse files
author
a-brandt
committed
added updateDocumentRaw() and replaceDocumentRaw()
1 parent 082ac5c commit 6172559

File tree

4 files changed

+236
-73
lines changed

4 files changed

+236
-73
lines changed

src/main/java/com/arangodb/ArangoDriver.java

Lines changed: 142 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -162,79 +162,6 @@ public ArangoDriver(ArangoConfigure configure, String database) {
162162
this.createModuleDrivers(false);
163163
}
164164

165-
private void createModuleDrivers(boolean createProxys) {
166-
if (!createProxys) {
167-
this.cursorDriver = ImplFactory.createCursorDriver(configure, this.httpManager);
168-
this.batchDriver = ImplFactory.createBatchDriver(configure, this.httpManager);
169-
this.collectionDriver = ImplFactory.createCollectionDriver(configure, this.httpManager);
170-
this.documentDriver = ImplFactory.createDocumentDriver(configure, this.httpManager);
171-
this.indexDriver = ImplFactory.createIndexDriver(configure, this.httpManager);
172-
this.adminDriver = ImplFactory.createAdminDriver(configure, this.httpManager);
173-
this.aqlFunctionsDriver = ImplFactory.createAqlFunctionsDriver(configure, this.httpManager);
174-
this.simpleDriver = ImplFactory.createSimpleDriver(configure, cursorDriver, this.httpManager);
175-
this.usersDriver = ImplFactory.createUsersDriver(configure, this.httpManager);
176-
this.importDriver = ImplFactory.createImportDriver(configure, this.httpManager);
177-
this.databaseDriver = ImplFactory.createDatabaseDriver(configure, this.httpManager);
178-
this.endpointDriver = ImplFactory.createEndpointDriver(configure, this.httpManager);
179-
this.replicationDriver = ImplFactory.createReplicationDriver(configure, this.httpManager);
180-
this.graphDriver = ImplFactory.createGraphDriver(configure, cursorDriver, this.httpManager);
181-
this.edgeDriver = ImplFactory.createEdgeDriver(configure, cursorDriver, this.httpManager);
182-
this.jobsDriver = ImplFactory.createJobsDriver(configure, this.httpManager);
183-
this.transactionDriver = ImplFactory.createTransactionDriver(configure, this.httpManager);
184-
this.traversalDriver = ImplFactory.createTraversalDriver(configure, httpManager);
185-
this.queryCacheDriver = ImplFactory.createQueryCacheDriver(configure, httpManager);
186-
} else {
187-
this.transactionDriver = (InternalTransactionDriver) Proxy.newProxyInstance(
188-
InternalTransactionDriver.class.getClassLoader(), new Class<?>[] { InternalTransactionDriver.class },
189-
new InvocationHandlerImpl(this.transactionDriver));
190-
this.jobsDriver = (InternalJobsDriver) Proxy.newProxyInstance(InternalJobsDriver.class.getClassLoader(),
191-
new Class<?>[] { InternalJobsDriver.class }, new InvocationHandlerImpl(this.jobsDriver));
192-
this.cursorDriver = (InternalCursorDriver) Proxy.newProxyInstance(
193-
InternalCursorDriver.class.getClassLoader(), new Class<?>[] { InternalCursorDriver.class },
194-
new InvocationHandlerImpl(this.cursorDriver));
195-
this.collectionDriver = (InternalCollectionDriver) Proxy.newProxyInstance(
196-
InternalCollectionDriver.class.getClassLoader(), new Class<?>[] { InternalCollectionDriver.class },
197-
new InvocationHandlerImpl(this.collectionDriver));
198-
this.documentDriver = (InternalDocumentDriver) Proxy.newProxyInstance(
199-
InternalDocumentDriver.class.getClassLoader(), new Class<?>[] { InternalDocumentDriver.class },
200-
new InvocationHandlerImpl(this.documentDriver));
201-
this.indexDriver = (InternalIndexDriver) Proxy.newProxyInstance(InternalIndexDriver.class.getClassLoader(),
202-
new Class<?>[] { InternalIndexDriver.class }, new InvocationHandlerImpl(this.indexDriver));
203-
this.adminDriver = (InternalAdminDriver) Proxy.newProxyInstance(InternalAdminDriver.class.getClassLoader(),
204-
new Class<?>[] { InternalAdminDriver.class }, new InvocationHandlerImpl(this.adminDriver));
205-
this.aqlFunctionsDriver = (InternalAqlFunctionsDriver) Proxy.newProxyInstance(
206-
InternalAqlFunctionsDriver.class.getClassLoader(), new Class<?>[] { InternalAqlFunctionsDriver.class },
207-
new InvocationHandlerImpl(this.aqlFunctionsDriver));
208-
this.simpleDriver = (InternalSimpleDriver) Proxy.newProxyInstance(
209-
InternalSimpleDriver.class.getClassLoader(), new Class<?>[] { InternalSimpleDriver.class },
210-
new InvocationHandlerImpl(this.simpleDriver));
211-
this.usersDriver = (InternalUsersDriver) Proxy.newProxyInstance(InternalUsersDriver.class.getClassLoader(),
212-
new Class<?>[] { InternalUsersDriver.class }, new InvocationHandlerImpl(this.usersDriver));
213-
this.importDriver = (InternalImportDriver) Proxy.newProxyInstance(
214-
InternalImportDriver.class.getClassLoader(), new Class<?>[] { InternalImportDriver.class },
215-
new InvocationHandlerImpl(this.importDriver));
216-
this.databaseDriver = (InternalDatabaseDriver) Proxy.newProxyInstance(
217-
InternalDatabaseDriver.class.getClassLoader(), new Class<?>[] { InternalDatabaseDriver.class },
218-
new InvocationHandlerImpl(this.databaseDriver));
219-
this.endpointDriver = (InternalEndpointDriver) Proxy.newProxyInstance(
220-
InternalEndpointDriver.class.getClassLoader(), new Class<?>[] { InternalEndpointDriver.class },
221-
new InvocationHandlerImpl(this.endpointDriver));
222-
this.replicationDriver = (InternalReplicationDriver) Proxy.newProxyInstance(
223-
InternalReplicationDriver.class.getClassLoader(), new Class<?>[] { InternalReplicationDriver.class },
224-
new InvocationHandlerImpl(this.replicationDriver));
225-
this.graphDriver = (InternalGraphDriver) Proxy.newProxyInstance(InternalGraphDriver.class.getClassLoader(),
226-
new Class<?>[] { InternalGraphDriver.class }, new InvocationHandlerImpl(this.graphDriver));
227-
this.edgeDriver = (InternalEdgeDriver) Proxy.newProxyInstance(InternalEdgeDriver.class.getClassLoader(),
228-
new Class<?>[] { InternalEdgeDriver.class }, new InvocationHandlerImpl(this.edgeDriver));
229-
this.traversalDriver = (InternalTraversalDriver) Proxy.newProxyInstance(
230-
InternalTraversalDriver.class.getClassLoader(), new Class<?>[] { InternalTraversalDriver.class },
231-
new InvocationHandlerImpl(this.traversalDriver));
232-
this.queryCacheDriver = (InternalQueryCacheDriver) Proxy.newProxyInstance(
233-
InternalQueryCacheDriver.class.getClassLoader(), new Class<?>[] { InternalQueryCacheDriver.class },
234-
new InvocationHandlerImpl(this.queryCacheDriver));
235-
}
236-
}
237-
238165
/**
239166
* This method enables batch execution. Until 'cancelBatchMode' or
240167
* 'executeBatch' is called every other call is stacked and will be either
@@ -5771,6 +5698,148 @@ public CursorRawResult executeAqlQueryRaw(
57715698
getAqlQueryOptions(aqlQueryOptions));
57725699
}
57735700

5701+
/**
5702+
* This method replaces the content of the document defined by
5703+
* documentHandle. This method offers a parameter rev (revision). If the
5704+
* revision of the document on the server does not match the given revision
5705+
* the policy parameter is used. If it is set to *last* the operation is
5706+
* performed anyway. if it is set to *error* an error is thrown.
5707+
*
5708+
* @param documentHandle
5709+
* The document's handle.
5710+
* @param rawJsonString
5711+
* A string containing a JSON object
5712+
* @param rev
5713+
* the desired revision.
5714+
* @param policy
5715+
* The update policy
5716+
* @param waitForSync
5717+
* if set to true the response is returned when the server has
5718+
* finished.
5719+
* @return DocumentEntity<String>
5720+
* @throws ArangoException
5721+
*/
5722+
public DocumentEntity<String> replaceDocumentRaw(
5723+
String documentHandle,
5724+
String rawJsonString,
5725+
Long rev,
5726+
Policy policy,
5727+
Boolean waitForSync) throws ArangoException {
5728+
return documentDriver.replaceDocumentRaw(getDefaultDatabase(), documentHandle, rawJsonString, rev, policy,
5729+
waitForSync);
5730+
}
5731+
5732+
/**
5733+
* This method updates a document defined by documentHandle. This method
5734+
* offers a parameter rev (revision). If the revision of the document on the
5735+
* server does not match the given revision the policy parameter is used. If
5736+
* it is set to *last* the operation is performed anyway. if it is set to
5737+
* *error* an error is thrown.
5738+
*
5739+
* @param documentHandle
5740+
* The document handle.
5741+
* @param rawJsonString
5742+
* A string containing a JSON object
5743+
* @param rev
5744+
* The desired revision
5745+
* @param policy
5746+
* The update policy
5747+
* @param waitForSync
5748+
* if set to true the response is returned when the server has
5749+
* finished.
5750+
* @param keepNull
5751+
* If true null values are kept.
5752+
* @return DocumentEntity<String>
5753+
* @throws ArangoException
5754+
*/
5755+
public DocumentEntity<String> updateDocumentRaw(
5756+
String documentHandle,
5757+
String rawJsonString,
5758+
Long rev,
5759+
Policy policy,
5760+
Boolean waitForSync,
5761+
Boolean keepNull) throws ArangoException {
5762+
return documentDriver.updateDocumentRaw(getDefaultDatabase(), documentHandle, rawJsonString, rev, policy,
5763+
waitForSync, keepNull);
5764+
}
5765+
5766+
//
5767+
// private functions
5768+
//
5769+
5770+
private void createModuleDrivers(boolean createProxys) {
5771+
if (!createProxys) {
5772+
this.cursorDriver = ImplFactory.createCursorDriver(configure, this.httpManager);
5773+
this.batchDriver = ImplFactory.createBatchDriver(configure, this.httpManager);
5774+
this.collectionDriver = ImplFactory.createCollectionDriver(configure, this.httpManager);
5775+
this.documentDriver = ImplFactory.createDocumentDriver(configure, this.httpManager);
5776+
this.indexDriver = ImplFactory.createIndexDriver(configure, this.httpManager);
5777+
this.adminDriver = ImplFactory.createAdminDriver(configure, this.httpManager);
5778+
this.aqlFunctionsDriver = ImplFactory.createAqlFunctionsDriver(configure, this.httpManager);
5779+
this.simpleDriver = ImplFactory.createSimpleDriver(configure, cursorDriver, this.httpManager);
5780+
this.usersDriver = ImplFactory.createUsersDriver(configure, this.httpManager);
5781+
this.importDriver = ImplFactory.createImportDriver(configure, this.httpManager);
5782+
this.databaseDriver = ImplFactory.createDatabaseDriver(configure, this.httpManager);
5783+
this.endpointDriver = ImplFactory.createEndpointDriver(configure, this.httpManager);
5784+
this.replicationDriver = ImplFactory.createReplicationDriver(configure, this.httpManager);
5785+
this.graphDriver = ImplFactory.createGraphDriver(configure, cursorDriver, this.httpManager);
5786+
this.edgeDriver = ImplFactory.createEdgeDriver(configure, cursorDriver, this.httpManager);
5787+
this.jobsDriver = ImplFactory.createJobsDriver(configure, this.httpManager);
5788+
this.transactionDriver = ImplFactory.createTransactionDriver(configure, this.httpManager);
5789+
this.traversalDriver = ImplFactory.createTraversalDriver(configure, httpManager);
5790+
this.queryCacheDriver = ImplFactory.createQueryCacheDriver(configure, httpManager);
5791+
} else {
5792+
this.transactionDriver = (InternalTransactionDriver) Proxy.newProxyInstance(
5793+
InternalTransactionDriver.class.getClassLoader(), new Class<?>[] { InternalTransactionDriver.class },
5794+
new InvocationHandlerImpl(this.transactionDriver));
5795+
this.jobsDriver = (InternalJobsDriver) Proxy.newProxyInstance(InternalJobsDriver.class.getClassLoader(),
5796+
new Class<?>[] { InternalJobsDriver.class }, new InvocationHandlerImpl(this.jobsDriver));
5797+
this.cursorDriver = (InternalCursorDriver) Proxy.newProxyInstance(
5798+
InternalCursorDriver.class.getClassLoader(), new Class<?>[] { InternalCursorDriver.class },
5799+
new InvocationHandlerImpl(this.cursorDriver));
5800+
this.collectionDriver = (InternalCollectionDriver) Proxy.newProxyInstance(
5801+
InternalCollectionDriver.class.getClassLoader(), new Class<?>[] { InternalCollectionDriver.class },
5802+
new InvocationHandlerImpl(this.collectionDriver));
5803+
this.documentDriver = (InternalDocumentDriver) Proxy.newProxyInstance(
5804+
InternalDocumentDriver.class.getClassLoader(), new Class<?>[] { InternalDocumentDriver.class },
5805+
new InvocationHandlerImpl(this.documentDriver));
5806+
this.indexDriver = (InternalIndexDriver) Proxy.newProxyInstance(InternalIndexDriver.class.getClassLoader(),
5807+
new Class<?>[] { InternalIndexDriver.class }, new InvocationHandlerImpl(this.indexDriver));
5808+
this.adminDriver = (InternalAdminDriver) Proxy.newProxyInstance(InternalAdminDriver.class.getClassLoader(),
5809+
new Class<?>[] { InternalAdminDriver.class }, new InvocationHandlerImpl(this.adminDriver));
5810+
this.aqlFunctionsDriver = (InternalAqlFunctionsDriver) Proxy.newProxyInstance(
5811+
InternalAqlFunctionsDriver.class.getClassLoader(), new Class<?>[] { InternalAqlFunctionsDriver.class },
5812+
new InvocationHandlerImpl(this.aqlFunctionsDriver));
5813+
this.simpleDriver = (InternalSimpleDriver) Proxy.newProxyInstance(
5814+
InternalSimpleDriver.class.getClassLoader(), new Class<?>[] { InternalSimpleDriver.class },
5815+
new InvocationHandlerImpl(this.simpleDriver));
5816+
this.usersDriver = (InternalUsersDriver) Proxy.newProxyInstance(InternalUsersDriver.class.getClassLoader(),
5817+
new Class<?>[] { InternalUsersDriver.class }, new InvocationHandlerImpl(this.usersDriver));
5818+
this.importDriver = (InternalImportDriver) Proxy.newProxyInstance(
5819+
InternalImportDriver.class.getClassLoader(), new Class<?>[] { InternalImportDriver.class },
5820+
new InvocationHandlerImpl(this.importDriver));
5821+
this.databaseDriver = (InternalDatabaseDriver) Proxy.newProxyInstance(
5822+
InternalDatabaseDriver.class.getClassLoader(), new Class<?>[] { InternalDatabaseDriver.class },
5823+
new InvocationHandlerImpl(this.databaseDriver));
5824+
this.endpointDriver = (InternalEndpointDriver) Proxy.newProxyInstance(
5825+
InternalEndpointDriver.class.getClassLoader(), new Class<?>[] { InternalEndpointDriver.class },
5826+
new InvocationHandlerImpl(this.endpointDriver));
5827+
this.replicationDriver = (InternalReplicationDriver) Proxy.newProxyInstance(
5828+
InternalReplicationDriver.class.getClassLoader(), new Class<?>[] { InternalReplicationDriver.class },
5829+
new InvocationHandlerImpl(this.replicationDriver));
5830+
this.graphDriver = (InternalGraphDriver) Proxy.newProxyInstance(InternalGraphDriver.class.getClassLoader(),
5831+
new Class<?>[] { InternalGraphDriver.class }, new InvocationHandlerImpl(this.graphDriver));
5832+
this.edgeDriver = (InternalEdgeDriver) Proxy.newProxyInstance(InternalEdgeDriver.class.getClassLoader(),
5833+
new Class<?>[] { InternalEdgeDriver.class }, new InvocationHandlerImpl(this.edgeDriver));
5834+
this.traversalDriver = (InternalTraversalDriver) Proxy.newProxyInstance(
5835+
InternalTraversalDriver.class.getClassLoader(), new Class<?>[] { InternalTraversalDriver.class },
5836+
new InvocationHandlerImpl(this.traversalDriver));
5837+
this.queryCacheDriver = (InternalQueryCacheDriver) Proxy.newProxyInstance(
5838+
InternalQueryCacheDriver.class.getClassLoader(), new Class<?>[] { InternalQueryCacheDriver.class },
5839+
new InvocationHandlerImpl(this.queryCacheDriver));
5840+
}
5841+
}
5842+
57745843
private AqlQueryOptions getAqlQueryOptions(AqlQueryOptions aqlQueryOptions) {
57755844
if (aqlQueryOptions == null) {
57765845
return getDefaultAqlQueryOptions();

src/main/java/com/arangodb/InternalDocumentDriver.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ <T> DocumentEntity<T> replaceDocument(
3333
Policy policy,
3434
Boolean waitForSync) throws ArangoException;
3535

36+
DocumentEntity<String> replaceDocumentRaw(
37+
String database,
38+
String documentHandle,
39+
String rawJsonString,
40+
Long rev,
41+
Policy policy,
42+
Boolean waitForSync) throws ArangoException;
43+
3644
<T> DocumentEntity<T> updateDocument(
3745
String database,
3846
String documentHandle,
@@ -42,6 +50,15 @@ <T> DocumentEntity<T> updateDocument(
4250
Boolean waitForSync,
4351
Boolean keepNull) throws ArangoException;
4452

53+
DocumentEntity<String> updateDocumentRaw(
54+
String database,
55+
String documentHandle,
56+
String rawJsonString,
57+
Long rev,
58+
Policy policy,
59+
Boolean waitForSync,
60+
Boolean keepNull) throws ArangoException;
61+
4562
List<String> getDocuments(String database, String collectionName, boolean handleConvert) throws ArangoException;
4663

4764
long checkDocument(String database, String documentHandle) throws ArangoException;

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,27 @@ public <T> DocumentEntity<T> replaceDocument(
138138
return result;
139139
}
140140

141+
@Override
142+
public DocumentEntity<String> replaceDocumentRaw(
143+
String database,
144+
String documentHandle,
145+
String rawJsonString,
146+
Long rev,
147+
Policy policy,
148+
Boolean waitForSync) throws ArangoException {
149+
150+
validateDocumentHandle(documentHandle);
151+
HttpResponseEntity res = httpManager.doPut(
152+
createDocumentEndpointUrl(database, documentHandle), new MapBuilder().put("rev", rev)
153+
.put(POLICY, policy == null ? null : policy.name()).put(WAIT_FOR_SYNC, waitForSync).get(),
154+
rawJsonString);
155+
156+
@SuppressWarnings("unchecked")
157+
DocumentEntity<String> result = createEntity(res, DocumentEntity.class);
158+
result.setEntity(rawJsonString);
159+
return result;
160+
}
161+
141162
@Override
142163
public <T> DocumentEntity<T> updateDocument(
143164
String database,
@@ -162,6 +183,28 @@ public <T> DocumentEntity<T> updateDocument(
162183
return result;
163184
}
164185

186+
@Override
187+
public DocumentEntity<String> updateDocumentRaw(
188+
String database,
189+
String documentHandle,
190+
String rawJsonString,
191+
Long rev,
192+
Policy policy,
193+
Boolean waitForSync,
194+
Boolean keepNull) throws ArangoException {
195+
196+
validateDocumentHandle(documentHandle);
197+
HttpResponseEntity res = httpManager.doPatch(createDocumentEndpointUrl(database, documentHandle),
198+
new MapBuilder().put("rev", rev).put(POLICY, policy == null ? null : policy.name())
199+
.put(WAIT_FOR_SYNC, waitForSync).put("keepNull", keepNull).get(),
200+
rawJsonString);
201+
202+
@SuppressWarnings("unchecked")
203+
DocumentEntity<String> result = createEntity(res, DocumentEntity.class);
204+
result.setEntity(rawJsonString);
205+
return result;
206+
}
207+
165208
@Override
166209
public List<String> getDocuments(String database, String collectionName, boolean handleConvert)
167210
throws ArangoException {

src/test/java/com/arangodb/example/document/RawDocumentExample.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,40 @@ public void ReadDocuments() {
126126
Assert.assertEquals(ErrorNums.ERROR_HTTP_NOT_FOUND, e.getCode());
127127
}
128128

129+
//
130+
printHeadline("update attributes of a document");
131+
//
132+
133+
x = "{\"test\":1234}";
134+
try {
135+
DocumentEntity<String> updateDocumentRaw = arangoDriver.updateDocumentRaw(documentHandle2, x, null, null,
136+
false, true);
137+
// print new document revision
138+
System.out.println("rev: " + updateDocumentRaw.getDocumentRevision());
139+
// show request result (you have to use getDocumentRaw to get
140+
// all attributes of the changed object):
141+
System.out.println("value: " + updateDocumentRaw.getEntity());
142+
} catch (ArangoException e) {
143+
Assert.assertEquals(ErrorNums.ERROR_HTTP_NOT_FOUND, e.getCode());
144+
}
145+
146+
//
147+
printHeadline("replace a document");
148+
//
149+
150+
x = "{\"hund\":321,\"katze\":321,\"maus\":777}";
151+
try {
152+
DocumentEntity<String> replaceDocumentRaw = arangoDriver.replaceDocumentRaw(documentHandle2, x, null, null,
153+
false);
154+
// print new document revision
155+
System.out.println("rev: " + replaceDocumentRaw.getDocumentRevision());
156+
// show request result (you have to use getDocumentRaw to get
157+
// all attributes of the replaced object):
158+
System.out.println("value: " + replaceDocumentRaw.getEntity());
159+
} catch (ArangoException e) {
160+
Assert.assertEquals(ErrorNums.ERROR_HTTP_NOT_FOUND, e.getCode());
161+
}
162+
129163
//
130164
printHeadline("using org.json.JSONML to save a xml file");
131165
//

0 commit comments

Comments
 (0)