Skip to content

Commit

Permalink
added createDocumentRaw(...) and getDocumentRaw(...).
Browse files Browse the repository at this point in the history
  • Loading branch information
a-brandt committed Jan 21, 2016
1 parent 1dc464f commit ac0d119
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 11 deletions.
5 changes: 3 additions & 2 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
c2.7.1 (????-??-??)
v2.7.1 (2016-01-21)
---------------------------
* added examples for new AQL traversal functions (since ArangoDB 2.8)
* added AQL warnings to CursorResult<?> (hasWarning() and getWarnings())

* added createDocumentRaw(...) and getDocumentRaw(...). Examples src/test/java/com/arangodb/example/document/RawDocumentExample.java
* Updated dependencies gson (2.5), httpclient (4.5.1) and slf4j-api (1.7.13)

v2.7.0 (2015-11-20)
---------------------------
Expand Down
14 changes: 11 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<commons-collections4.version>4.0</commons-collections4.version>
<gson.version>2.3.1</gson.version>
<httpclient.version>4.4.1</httpclient.version>
<slf4j-api.version>1.7.7</slf4j-api.version>
<gson.version>2.5</gson.version>
<httpclient.version>4.5.1</httpclient.version>
<slf4j-api.version>1.7.13</slf4j-api.version>
<logback-classic.version>1.1.3</logback-classic.version>
<hamcrest-all.version>1.3</hamcrest-all.version>
<junit.version>4.12</junit.version>
Expand Down Expand Up @@ -246,6 +246,14 @@
<version>${hamcrest-all.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20151123</version>
<scope>test</scope>
</dependency>


</dependencies>

<scm>
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/arangodb/ArangoDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -5765,4 +5765,51 @@ public DefaultEntity killQuery(String database, String id) throws ArangoExceptio
public HttpManager getHttpManager() {
return httpManager;
}

/**
* Creates a document in the collection defined by the collection's name
*
* @param collectionName
* The name of the collection
* @param rawJsonString
* A string containing a JSON object
* @param createCollection
* if set to true the collection is created if it does not exist
* @param waitForSync
* if set to true the response is returned when the server has
* finished.
* @return DocumentEntity<String>
* @throws ArangoException
*/
public DocumentEntity<String> createDocumentRaw(
String collectionName,
String rawJsonString,
Boolean createCollection,
Boolean waitForSync) throws ArangoException {
return documentDriver.createDocumentRaw(getDefaultDatabase(), collectionName, rawJsonString, createCollection,
waitForSync);
}

/**
* Returns the document as a JSON string. Note that the
* *ifNoneMatchRevision* and *ifMatchRevision* can not be used at the same
* time, one of these two has to be null.
*
* @param documentHandle
* The document handle
* @param ifNoneMatchRevision
* if set the document is only returned id it has a different
* revision.
* @param ifMatchRevision
* if set the document is only returned id it has the same
* revision.
* @return a String
* @throws ArangoException
*/
public String getDocumentRaw(String documentHandle, Long ifNoneMatchRevision, Long ifMatchRevision)
throws ArangoException {
return documentDriver.getDocumentRaw(getDefaultDatabase(), documentHandle, ifNoneMatchRevision,
ifMatchRevision);
}

}
4 changes: 3 additions & 1 deletion src/main/java/com/arangodb/InternalDocumentDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ <T> DocumentEntity<T> createDocument(
DocumentEntity<String> createDocumentRaw(
String database,
String collectionName,
String documentKey,
String rawJsonString,
Boolean createCollection,
Boolean waitForSync) throws ArangoException;
Expand Down Expand Up @@ -54,6 +53,9 @@ <T> DocumentEntity<T> getDocument(
Long ifNoneMatchRevision,
Long ifMatchRevision) throws ArangoException;

String getDocumentRaw(String database, String documentHandle, Long ifNoneMatchRevision, Long ifMatchRevision)
throws ArangoException;

DocumentEntity<?> deleteDocument(String database, String documentHandle, Long rev, Policy policy)
throws ArangoException;
}
17 changes: 14 additions & 3 deletions src/main/java/com/arangodb/impl/InternalDocumentDriverImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,10 @@ public <T> DocumentEntity<T> createDocument(
public DocumentEntity<String> createDocumentRaw(
String database,
String collectionName,
String documentKey,
String rawJsonString,
String rawJsonObjectString,
Boolean createCollection,
Boolean waitForSync) throws ArangoException {
return _createDocument(database, collectionName, documentKey, rawJsonString, createCollection, waitForSync,
return _createDocument(database, collectionName, null, rawJsonObjectString, createCollection, waitForSync,
true);
}

Expand Down Expand Up @@ -215,6 +214,18 @@ public <T> DocumentEntity<T> getDocument(
return entity;
}

@Override
public String getDocumentRaw(String database, String documentHandle, Long ifNoneMatchRevision, Long ifMatchRevision)
throws ArangoException {

validateDocumentHandle(documentHandle);
HttpResponseEntity res = httpManager.doGet(createEndpointUrl(database, "/_api/document", documentHandle),
new MapBuilder().put("If-None-Match", ifNoneMatchRevision, true).put("If-Match", ifMatchRevision).get(),
null);

return res.getText();
}

@Override
public DocumentEntity<?> deleteDocument(String database, String documentHandle, Long rev, Policy policy)
throws ArangoException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@

SimplePersonAqlQueryWithLimitExample.class,

AqlQueryWithSpecialReturnTypesExample.class
AqlQueryWithSpecialReturnTypesExample.class,

})
RawDocumentExample.class })

public class DocumentExamplesTestSuite {

Expand Down
146 changes: 146 additions & 0 deletions src/test/java/com/arangodb/example/document/RawDocumentExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright (C) 2015 ArangoDB GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.arangodb.example.document;

import org.json.JSONML;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.arangodb.ArangoDriver;
import com.arangodb.ArangoException;
import com.arangodb.entity.DocumentEntity;

public class RawDocumentExample extends BaseExample {

private static final String DATABASE_NAME = "RawDocument";

private static final String COLLECTION_NAME = "RawDocument";

public ArangoDriver arangoDriver;

@Before
public void _before() {
removeTestDatabase(DATABASE_NAME);

arangoDriver = getArangoDriver(getConfiguration());
createDatabase(arangoDriver, DATABASE_NAME);
createCollection(arangoDriver, COLLECTION_NAME);
}

@Test
public void ReadDocuments() {
//
// You can find the ArangoDB Web interface here:
// http://127.0.0.1:8529/
//
// change the log level to "debug" in /src/test/resource/logback.xml to
// see the HTTP communication

//
printHeadline("create example document 1");
//

String documentHandle1 = null;
String documentHandle2 = null;
String documentHandle3 = null;

String x = "{\"test\":123}";
try {
DocumentEntity<String> entity = arangoDriver.createDocumentRaw(COLLECTION_NAME, x, true, false);
// the DocumentEntity contains the key, document handle and revision
System.out.println("Key: " + entity.getDocumentKey());
System.out.println("Id: " + entity.getDocumentHandle());
System.out.println("Revision: " + entity.getDocumentRevision());
documentHandle1 = entity.getDocumentHandle();
} catch (ArangoException e) {
Assert.fail("Failed to create document. " + e.getMessage());
}

//
printHeadline("read example document 1");
//

try {
String str = arangoDriver.getDocumentRaw(documentHandle1, null, null);
System.out.println("value: " + str);
} catch (ArangoException e) {
Assert.fail("Failed to read document. " + e.getMessage());
}

//
printHeadline("create example document 2 with key");
//

x = "{\"_key\":\"key2\",\"test\":123}";
try {
DocumentEntity<String> entity = arangoDriver.createDocumentRaw(COLLECTION_NAME, x, true, false);
// the DocumentEntity contains the key, document handle and revision
System.out.println("Key: " + entity.getDocumentKey());
System.out.println("Id: " + entity.getDocumentHandle());
System.out.println("Revision: " + entity.getDocumentRevision());
documentHandle2 = entity.getDocumentHandle();
} catch (ArangoException e) {
Assert.fail("Failed to create document. " + e.getMessage());
}

//
printHeadline("read example document 2");
//

try {
String str = arangoDriver.getDocumentRaw(documentHandle2, null, null);
System.out.println("value: " + str);
} catch (ArangoException e) {
Assert.fail("Failed to read document. " + e.getMessage());
}

//
printHeadline("using org.json.JSONML to save a xml file");
//
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> ";
System.out.println("Orig XML value: " + string);
JSONObject jsonObject = JSONML.toJSONObject(string);
try {
DocumentEntity<String> entity = arangoDriver.createDocumentRaw(COLLECTION_NAME, jsonObject.toString(), true,
false);
// the DocumentEntity contains the key, document handle and revision
System.out.println("Key: " + entity.getDocumentKey());
System.out.println("Id: " + entity.getDocumentHandle());
System.out.println("Revision: " + entity.getDocumentRevision());
documentHandle3 = entity.getDocumentHandle();
} catch (ArangoException e) {
Assert.fail("Failed to create document. " + e.getMessage());
}

//
printHeadline("read example and convert it back to XML");
//

try {
String str = arangoDriver.getDocumentRaw(documentHandle3, null, null);
System.out.println("JSON value: " + str);
JSONObject jsonObject2 = new JSONObject(str);
System.out.println("XML value: " + JSONML.toString(jsonObject2));
} catch (ArangoException e) {
Assert.fail("Failed to read document. " + e.getMessage());
}

}

}

0 comments on commit ac0d119

Please sign in to comment.