Skip to content

Commit 1daf5e1

Browse files
author
a-brandt
committed
bugfix properties of BaseDocuments
1 parent ccfa855 commit 1daf5e1

File tree

2 files changed

+194
-25
lines changed

2 files changed

+194
-25
lines changed

Diff for: src/main/java/com/arangodb/entity/EntityDeserializers.java

+24-25
Original file line numberDiff line numberDiff line change
@@ -612,15 +612,12 @@ public DocumentEntity<?> deserialize(JsonElement json, Type typeOfT, JsonDeseria
612612
Class<?> clazz = getParameterized();
613613
if (clazz != null) {
614614
entity.entity = context.deserialize(obj, clazz);
615-
// if
616-
// (clazz.getName().equalsIgnoreCase(BaseDocument.class.getName()))
617-
// {
618-
// iterate all key/value pairs of the jsonObject and
619-
// determine its class(String, Number, Boolean, HashMap,
620-
// List)
621-
// ((BaseDocument)
622-
// entity.entity).setProperties(DeserializeSingleEntry.deserializeJsonObject(obj));
623-
// }
615+
if (clazz.getName().equalsIgnoreCase(BaseDocument.class.getName())) {
616+
// iterate all key/value pairs of the jsonObject and
617+
// determine its class(String, Number, Boolean, HashMap,
618+
// List)
619+
((BaseDocument) entity.entity).setProperties(DeserializeSingleEntry.deserializeJsonObject(obj));
620+
}
624621
}
625622

626623
return entity;
@@ -936,8 +933,8 @@ public StatisticsEntity deserialize(JsonElement json, Type typeOfT, JsonDeserial
936933
}
937934
}
938935

939-
public static class StatisticsDescriptionEntityDeserializer implements
940-
JsonDeserializer<StatisticsDescriptionEntity> {
936+
public static class StatisticsDescriptionEntityDeserializer
937+
implements JsonDeserializer<StatisticsDescriptionEntity> {
941938
Type cutsTypes = new TypeToken<BigDecimal[]>() {
942939
}.getType();
943940

@@ -1307,8 +1304,10 @@ public static class ReplicationInventoryEntityDeserializer implements JsonDeseri
13071304
}.getType();
13081305

13091306
@Override
1310-
public ReplicationInventoryEntity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
1311-
throws JsonParseException {
1307+
public ReplicationInventoryEntity deserialize(
1308+
JsonElement json,
1309+
Type typeOfT,
1310+
JsonDeserializationContext context) throws JsonParseException {
13121311

13131312
if (json.isJsonNull()) {
13141313
return null;
@@ -1332,8 +1331,8 @@ public ReplicationInventoryEntity deserialize(JsonElement json, Type typeOfT, Js
13321331
col.parameter.version = parameters.getAsJsonPrimitive("version").getAsInt();
13331332
}
13341333
if (parameters.has("type")) {
1335-
col.parameter.type = CollectionType.valueOf(parameters.getAsJsonPrimitive("type")
1336-
.getAsInt());
1334+
col.parameter.type = CollectionType
1335+
.valueOf(parameters.getAsJsonPrimitive("type").getAsInt());
13371336
}
13381337
if (parameters.has("cid")) {
13391338
col.parameter.cid = parameters.getAsJsonPrimitive("cid").getAsLong();
@@ -1461,8 +1460,8 @@ public MapAsEntity deserialize(JsonElement json, Type typeOfT, JsonDeserializati
14611460
}
14621461
}
14631462

1464-
public static class ReplicationLoggerConfigEntityDeserializer implements
1465-
JsonDeserializer<ReplicationLoggerConfigEntity> {
1463+
public static class ReplicationLoggerConfigEntityDeserializer
1464+
implements JsonDeserializer<ReplicationLoggerConfigEntity> {
14661465
@Override
14671466
public ReplicationLoggerConfigEntity deserialize(
14681467
JsonElement json,
@@ -1493,8 +1492,8 @@ public ReplicationLoggerConfigEntity deserialize(
14931492
}
14941493
}
14951494

1496-
public static class ReplicationApplierConfigEntityDeserializer implements
1497-
JsonDeserializer<ReplicationApplierConfigEntity> {
1495+
public static class ReplicationApplierConfigEntityDeserializer
1496+
implements JsonDeserializer<ReplicationApplierConfigEntity> {
14981497
@Override
14991498
public ReplicationApplierConfigEntity deserialize(
15001499
JsonElement json,
@@ -1621,8 +1620,8 @@ public ReplicationApplierState deserialize(JsonElement json, Type typeOfT, JsonD
16211620
}
16221621
}
16231622

1624-
public static class ReplicationApplierStateEntityDeserializer implements
1625-
JsonDeserializer<ReplicationApplierStateEntity> {
1623+
public static class ReplicationApplierStateEntityDeserializer
1624+
implements JsonDeserializer<ReplicationApplierStateEntity> {
16261625
@Override
16271626
public ReplicationApplierStateEntity deserialize(
16281627
JsonElement json,
@@ -1658,8 +1657,8 @@ public ReplicationApplierStateEntity deserialize(
16581657
}
16591658
}
16601659

1661-
public static class ReplicationLoggerStateEntityDeserializer implements
1662-
JsonDeserializer<ReplicationLoggerStateEntity> {
1660+
public static class ReplicationLoggerStateEntityDeserializer
1661+
implements JsonDeserializer<ReplicationLoggerStateEntity> {
16631662
private Type clientsType = new TypeToken<List<Client>>() {
16641663
}.getType();
16651664

@@ -1694,8 +1693,8 @@ public ReplicationLoggerStateEntity deserialize(
16941693
}
16951694
}
16961695

1697-
public static class ReplicationLoggerStateEntityClientDeserializer implements
1698-
JsonDeserializer<ReplicationLoggerStateEntity.Client> {
1696+
public static class ReplicationLoggerStateEntityClientDeserializer
1697+
implements JsonDeserializer<ReplicationLoggerStateEntity.Client> {
16991698
@Override
17001699
public ReplicationLoggerStateEntity.Client deserialize(
17011700
JsonElement json,

Diff for: src/test/java/com/arangodb/example/FirstProject.java

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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;
18+
19+
import java.util.Iterator;
20+
import java.util.Map;
21+
22+
import com.arangodb.ArangoConfigure;
23+
import com.arangodb.ArangoDriver;
24+
import com.arangodb.ArangoException;
25+
import com.arangodb.DocumentCursor;
26+
import com.arangodb.entity.BaseDocument;
27+
import com.arangodb.entity.CollectionEntity;
28+
import com.arangodb.entity.DocumentEntity;
29+
import com.arangodb.util.MapBuilder;
30+
31+
/**
32+
* My first ArangoDB project
33+
*/
34+
public class FirstProject {
35+
36+
public static void main(String[] args) {
37+
38+
//
39+
// You can find the ArangoDB Web interface here:
40+
// http://127.0.0.1:8529/_db/mydb/
41+
//
42+
43+
// Lets configure and open a connection to start ArangoDB.
44+
ArangoConfigure configure = new ArangoConfigure();
45+
configure.init();
46+
ArangoDriver arangoDriver = new ArangoDriver(configure);
47+
// The default connection is to http://127.0.0.1:8529.
48+
49+
// Lets configure and open a connection to start ArangoDB.
50+
String dbName = "mydb";
51+
try {
52+
arangoDriver.createDatabase(dbName);
53+
System.out.println("Database created: " + dbName);
54+
} catch (Exception e) {
55+
System.out.println("Failed to create database " + dbName + "; " + e.getMessage());
56+
}
57+
58+
// You can set the new database as default database for the driver:
59+
arangoDriver.setDefaultDatabase(dbName);
60+
61+
String collectionName = "firstCollection";
62+
try {
63+
CollectionEntity myArangoCollection = arangoDriver.createCollection(collectionName);
64+
System.out.println("Collection created: " + myArangoCollection.getName());
65+
} catch (Exception e) {
66+
System.out.println("Failed to create colleciton " + collectionName + "; " + e.getMessage());
67+
}
68+
69+
// create a document
70+
BaseDocument myObject = new BaseDocument();
71+
myObject.setDocumentKey("myKey");
72+
myObject.addAttribute("a", "Foo");
73+
myObject.addAttribute("b", 42);
74+
try {
75+
arangoDriver.createDocument(collectionName, myObject);
76+
System.out.println("Document created");
77+
} catch (ArangoException e) {
78+
System.out.println("Failed to create document. " + e.getMessage());
79+
}
80+
81+
// read a document
82+
DocumentEntity<BaseDocument> myDocument = null;
83+
BaseDocument myObject2 = null;
84+
try {
85+
myDocument = arangoDriver.getDocument(collectionName, "myKey", BaseDocument.class);
86+
myObject2 = myDocument.getEntity();
87+
System.out.println("Key: " + myObject2.getDocumentKey());
88+
System.out.println("Attribute 'a': " + myObject2.getProperties().get("a"));
89+
System.out.println("Attribute 'b': " + myObject2.getProperties().get("b"));
90+
System.out.println("Attribute 'c': " + myObject2.getProperties().get("c"));
91+
} catch (ArangoException e) {
92+
System.out.println("Failed to get document. " + e.getMessage());
93+
}
94+
95+
// update a document
96+
try {
97+
myObject2.addAttribute("c", "Bar");
98+
arangoDriver.updateDocument(myDocument.getDocumentHandle(), myObject2);
99+
} catch (ArangoException e) {
100+
System.out.println("Failed to update document. " + e.getMessage());
101+
}
102+
103+
// read document again
104+
try {
105+
myDocument = arangoDriver.getDocument(collectionName, "myKey", BaseDocument.class);
106+
System.out.println("Key: " + myObject2.getDocumentKey());
107+
System.out.println("Attribute 'a': " + myObject2.getProperties().get("a"));
108+
System.out.println("Attribute 'b': " + myObject2.getProperties().get("b"));
109+
System.out.println("Attribute 'c': " + myObject2.getProperties().get("c"));
110+
} catch (ArangoException e) {
111+
System.out.println("Failed to get document. " + e.getMessage());
112+
}
113+
114+
// delete document
115+
try {
116+
arangoDriver.deleteDocument(myDocument.getDocumentHandle());
117+
} catch (ArangoException e) {
118+
System.out.println("Failed to delete document. " + e.getMessage());
119+
}
120+
121+
// create some example entries
122+
try {
123+
for (Integer i = 0; i < 10; i++) {
124+
BaseDocument baseDocument = new BaseDocument();
125+
baseDocument.setDocumentKey(i.toString());
126+
baseDocument.addAttribute("name", "Homer");
127+
baseDocument.addAttribute("b", i + 42);
128+
arangoDriver.createDocument(collectionName, baseDocument);
129+
}
130+
} catch (ArangoException e) {
131+
System.out.println("Failed to create document. " + e.getMessage());
132+
}
133+
134+
// Get all documents with the name "Homer" from collection
135+
// "firstCollection" and iterate over the result:
136+
try {
137+
String query = "FOR t IN firstCollection FILTER t.name == @name RETURN t";
138+
Map<String, Object> bindVars = new MapBuilder().put("name", "Homer").get();
139+
DocumentCursor<BaseDocument> cursor = arangoDriver.executeDocumentQuery(query, bindVars, null,
140+
BaseDocument.class);
141+
142+
Iterator<BaseDocument> iterator = cursor.entityIterator();
143+
while (iterator.hasNext()) {
144+
BaseDocument aDocument = iterator.next();
145+
System.out.println("Key: " + aDocument.getDocumentKey());
146+
}
147+
} catch (ArangoException e) {
148+
System.out.println("Failed to execute query. " + e.getMessage());
149+
}
150+
151+
// Now we will delete the document created before:
152+
try {
153+
String query = "FOR t IN firstCollection FILTER t.name == @name "
154+
+ "REMOVE t IN firstCollection LET removed = OLD RETURN removed";
155+
Map<String, Object> bindVars = new MapBuilder().put("name", "Homer").get();
156+
DocumentCursor<BaseDocument> cursor = arangoDriver.executeDocumentQuery(query, bindVars, null,
157+
BaseDocument.class);
158+
159+
Iterator<BaseDocument> iterator = cursor.entityIterator();
160+
while (iterator.hasNext()) {
161+
BaseDocument aDocument = iterator.next();
162+
System.out.println("Removed document: " + aDocument.getDocumentKey());
163+
}
164+
165+
} catch (ArangoException e) {
166+
System.out.println("Failed to execute query. " + e.getMessage());
167+
}
168+
}
169+
170+
}

0 commit comments

Comments
 (0)