Skip to content

Commit ac0d119

Browse files
author
a-brandt
committed
added createDocumentRaw(...) and getDocumentRaw(...).
1 parent 1dc464f commit ac0d119

File tree

7 files changed

+226
-11
lines changed

7 files changed

+226
-11
lines changed

ChangeLog

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
c2.7.1 (????-??-??)
1+
v2.7.1 (2016-01-21)
22
---------------------------
33
* added examples for new AQL traversal functions (since ArangoDB 2.8)
44
* added AQL warnings to CursorResult<?> (hasWarning() and getWarnings())
5-
5+
* added createDocumentRaw(...) and getDocumentRaw(...). Examples src/test/java/com/arangodb/example/document/RawDocumentExample.java
6+
* Updated dependencies gson (2.5), httpclient (4.5.1) and slf4j-api (1.7.13)
67

78
v2.7.0 (2015-11-20)
89
---------------------------

pom.xml

+11-3
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@
198198
<properties>
199199
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
200200
<commons-collections4.version>4.0</commons-collections4.version>
201-
<gson.version>2.3.1</gson.version>
202-
<httpclient.version>4.4.1</httpclient.version>
203-
<slf4j-api.version>1.7.7</slf4j-api.version>
201+
<gson.version>2.5</gson.version>
202+
<httpclient.version>4.5.1</httpclient.version>
203+
<slf4j-api.version>1.7.13</slf4j-api.version>
204204
<logback-classic.version>1.1.3</logback-classic.version>
205205
<hamcrest-all.version>1.3</hamcrest-all.version>
206206
<junit.version>4.12</junit.version>
@@ -246,6 +246,14 @@
246246
<version>${hamcrest-all.version}</version>
247247
<scope>test</scope>
248248
</dependency>
249+
<dependency>
250+
<groupId>org.json</groupId>
251+
<artifactId>json</artifactId>
252+
<version>20151123</version>
253+
<scope>test</scope>
254+
</dependency>
255+
256+
249257
</dependencies>
250258

251259
<scm>

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

+47
Original file line numberDiff line numberDiff line change
@@ -5765,4 +5765,51 @@ public DefaultEntity killQuery(String database, String id) throws ArangoExceptio
57655765
public HttpManager getHttpManager() {
57665766
return httpManager;
57675767
}
5768+
5769+
/**
5770+
* Creates a document in the collection defined by the collection's name
5771+
*
5772+
* @param collectionName
5773+
* The name of the collection
5774+
* @param rawJsonString
5775+
* A string containing a JSON object
5776+
* @param createCollection
5777+
* if set to true the collection is created if it does not exist
5778+
* @param waitForSync
5779+
* if set to true the response is returned when the server has
5780+
* finished.
5781+
* @return DocumentEntity<String>
5782+
* @throws ArangoException
5783+
*/
5784+
public DocumentEntity<String> createDocumentRaw(
5785+
String collectionName,
5786+
String rawJsonString,
5787+
Boolean createCollection,
5788+
Boolean waitForSync) throws ArangoException {
5789+
return documentDriver.createDocumentRaw(getDefaultDatabase(), collectionName, rawJsonString, createCollection,
5790+
waitForSync);
5791+
}
5792+
5793+
/**
5794+
* Returns the document as a JSON string. Note that the
5795+
* *ifNoneMatchRevision* and *ifMatchRevision* can not be used at the same
5796+
* time, one of these two has to be null.
5797+
*
5798+
* @param documentHandle
5799+
* The document handle
5800+
* @param ifNoneMatchRevision
5801+
* if set the document is only returned id it has a different
5802+
* revision.
5803+
* @param ifMatchRevision
5804+
* if set the document is only returned id it has the same
5805+
* revision.
5806+
* @return a String
5807+
* @throws ArangoException
5808+
*/
5809+
public String getDocumentRaw(String documentHandle, Long ifNoneMatchRevision, Long ifMatchRevision)
5810+
throws ArangoException {
5811+
return documentDriver.getDocumentRaw(getDefaultDatabase(), documentHandle, ifNoneMatchRevision,
5812+
ifMatchRevision);
5813+
}
5814+
57685815
}

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ <T> DocumentEntity<T> createDocument(
2121
DocumentEntity<String> createDocumentRaw(
2222
String database,
2323
String collectionName,
24-
String documentKey,
2524
String rawJsonString,
2625
Boolean createCollection,
2726
Boolean waitForSync) throws ArangoException;
@@ -54,6 +53,9 @@ <T> DocumentEntity<T> getDocument(
5453
Long ifNoneMatchRevision,
5554
Long ifMatchRevision) throws ArangoException;
5655

56+
String getDocumentRaw(String database, String documentHandle, Long ifNoneMatchRevision, Long ifMatchRevision)
57+
throws ArangoException;
58+
5759
DocumentEntity<?> deleteDocument(String database, String documentHandle, Long rev, Policy policy)
5860
throws ArangoException;
5961
}

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

+14-3
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,10 @@ public <T> DocumentEntity<T> createDocument(
100100
public DocumentEntity<String> createDocumentRaw(
101101
String database,
102102
String collectionName,
103-
String documentKey,
104-
String rawJsonString,
103+
String rawJsonObjectString,
105104
Boolean createCollection,
106105
Boolean waitForSync) throws ArangoException {
107-
return _createDocument(database, collectionName, documentKey, rawJsonString, createCollection, waitForSync,
106+
return _createDocument(database, collectionName, null, rawJsonObjectString, createCollection, waitForSync,
108107
true);
109108
}
110109

@@ -215,6 +214,18 @@ public <T> DocumentEntity<T> getDocument(
215214
return entity;
216215
}
217216

217+
@Override
218+
public String getDocumentRaw(String database, String documentHandle, Long ifNoneMatchRevision, Long ifMatchRevision)
219+
throws ArangoException {
220+
221+
validateDocumentHandle(documentHandle);
222+
HttpResponseEntity res = httpManager.doGet(createEndpointUrl(database, "/_api/document", documentHandle),
223+
new MapBuilder().put("If-None-Match", ifNoneMatchRevision, true).put("If-Match", ifMatchRevision).get(),
224+
null);
225+
226+
return res.getText();
227+
}
228+
218229
@Override
219230
public DocumentEntity<?> deleteDocument(String database, String documentHandle, Long rev, Policy policy)
220231
throws ArangoException {

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545

4646
SimplePersonAqlQueryWithLimitExample.class,
4747

48-
AqlQueryWithSpecialReturnTypesExample.class
48+
AqlQueryWithSpecialReturnTypesExample.class,
4949

50-
})
50+
RawDocumentExample.class })
5151

5252
public class DocumentExamplesTestSuite {
5353

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright (C) 2015 ArangoDB GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.arangodb.example.document;
18+
19+
import org.json.JSONML;
20+
import org.json.JSONObject;
21+
import org.junit.Assert;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
import com.arangodb.ArangoDriver;
26+
import com.arangodb.ArangoException;
27+
import com.arangodb.entity.DocumentEntity;
28+
29+
public class RawDocumentExample extends BaseExample {
30+
31+
private static final String DATABASE_NAME = "RawDocument";
32+
33+
private static final String COLLECTION_NAME = "RawDocument";
34+
35+
public ArangoDriver arangoDriver;
36+
37+
@Before
38+
public void _before() {
39+
removeTestDatabase(DATABASE_NAME);
40+
41+
arangoDriver = getArangoDriver(getConfiguration());
42+
createDatabase(arangoDriver, DATABASE_NAME);
43+
createCollection(arangoDriver, COLLECTION_NAME);
44+
}
45+
46+
@Test
47+
public void ReadDocuments() {
48+
//
49+
// You can find the ArangoDB Web interface here:
50+
// http://127.0.0.1:8529/
51+
//
52+
// change the log level to "debug" in /src/test/resource/logback.xml to
53+
// see the HTTP communication
54+
55+
//
56+
printHeadline("create example document 1");
57+
//
58+
59+
String documentHandle1 = null;
60+
String documentHandle2 = null;
61+
String documentHandle3 = null;
62+
63+
String x = "{\"test\":123}";
64+
try {
65+
DocumentEntity<String> entity = arangoDriver.createDocumentRaw(COLLECTION_NAME, x, true, false);
66+
// the DocumentEntity contains the key, document handle and revision
67+
System.out.println("Key: " + entity.getDocumentKey());
68+
System.out.println("Id: " + entity.getDocumentHandle());
69+
System.out.println("Revision: " + entity.getDocumentRevision());
70+
documentHandle1 = entity.getDocumentHandle();
71+
} catch (ArangoException e) {
72+
Assert.fail("Failed to create document. " + e.getMessage());
73+
}
74+
75+
//
76+
printHeadline("read example document 1");
77+
//
78+
79+
try {
80+
String str = arangoDriver.getDocumentRaw(documentHandle1, null, null);
81+
System.out.println("value: " + str);
82+
} catch (ArangoException e) {
83+
Assert.fail("Failed to read document. " + e.getMessage());
84+
}
85+
86+
//
87+
printHeadline("create example document 2 with key");
88+
//
89+
90+
x = "{\"_key\":\"key2\",\"test\":123}";
91+
try {
92+
DocumentEntity<String> entity = arangoDriver.createDocumentRaw(COLLECTION_NAME, x, true, false);
93+
// the DocumentEntity contains the key, document handle and revision
94+
System.out.println("Key: " + entity.getDocumentKey());
95+
System.out.println("Id: " + entity.getDocumentHandle());
96+
System.out.println("Revision: " + entity.getDocumentRevision());
97+
documentHandle2 = entity.getDocumentHandle();
98+
} catch (ArangoException e) {
99+
Assert.fail("Failed to create document. " + e.getMessage());
100+
}
101+
102+
//
103+
printHeadline("read example document 2");
104+
//
105+
106+
try {
107+
String str = arangoDriver.getDocumentRaw(documentHandle2, null, null);
108+
System.out.println("value: " + str);
109+
} catch (ArangoException e) {
110+
Assert.fail("Failed to read document. " + e.getMessage());
111+
}
112+
113+
//
114+
printHeadline("using org.json.JSONML to save a xml file");
115+
//
116+
String string = "<recipe name=\"bread\" prep_time=\"5 mins\" cook_time=\"3 hours\"> <title>Basic bread</title> <ingredient amount=\"8\" unit=\"dL\">Flour</ingredient> <ingredient amount=\"10\" unit=\"grams\">Yeast</ingredient> <ingredient amount=\"4\" unit=\"dL\" state=\"warm\">Water</ingredient> <ingredient amount=\"1\" unit=\"teaspoon\">Salt</ingredient> <instructions> <step>Mix all ingredients together.</step> <step>Knead thoroughly.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Knead again.</step> <step>Place in a bread baking tin.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Bake in the oven at 180(degrees)C for 30 minutes.</step> </instructions> </recipe> ";
117+
System.out.println("Orig XML value: " + string);
118+
JSONObject jsonObject = JSONML.toJSONObject(string);
119+
try {
120+
DocumentEntity<String> entity = arangoDriver.createDocumentRaw(COLLECTION_NAME, jsonObject.toString(), true,
121+
false);
122+
// the DocumentEntity contains the key, document handle and revision
123+
System.out.println("Key: " + entity.getDocumentKey());
124+
System.out.println("Id: " + entity.getDocumentHandle());
125+
System.out.println("Revision: " + entity.getDocumentRevision());
126+
documentHandle3 = entity.getDocumentHandle();
127+
} catch (ArangoException e) {
128+
Assert.fail("Failed to create document. " + e.getMessage());
129+
}
130+
131+
//
132+
printHeadline("read example and convert it back to XML");
133+
//
134+
135+
try {
136+
String str = arangoDriver.getDocumentRaw(documentHandle3, null, null);
137+
System.out.println("JSON value: " + str);
138+
JSONObject jsonObject2 = new JSONObject(str);
139+
System.out.println("XML value: " + JSONML.toString(jsonObject2));
140+
} catch (ArangoException e) {
141+
Assert.fail("Failed to read document. " + e.getMessage());
142+
}
143+
144+
}
145+
146+
}

0 commit comments

Comments
 (0)