Skip to content

Commit 4c92969

Browse files
author
Mark
committed
restore createEdge() method
1 parent 9928107 commit 4c92969

12 files changed

+203
-43
lines changed

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4564,6 +4564,64 @@ public TransactionResultEntity executeTransaction(final TransactionEntity transa
45644564
return this.transactionDriver.executeTransaction(getDefaultDatabase(), transactionEntity);
45654565
}
45664566

4567+
/**
4568+
* Create an edge in an edge collection.
4569+
*
4570+
* @param collectionName
4571+
* name of the edge collection
4572+
* @param value
4573+
* the edge object
4574+
* @param fromHandle
4575+
* id of document 'from'
4576+
* @param toHandle
4577+
* id of document 'to'
4578+
* @param waitForSync
4579+
* wait for sync
4580+
* @return the new created EdgeEntity object
4581+
* @throws ArangoException
4582+
*/
4583+
public <T> EdgeEntity<T> createEdge(
4584+
final String collectionName,
4585+
final T value,
4586+
final String fromHandle,
4587+
final String toHandle,
4588+
final Boolean waitForSync) throws ArangoException {
4589+
4590+
return createEdge(collectionName, null, value, fromHandle, toHandle, waitForSync);
4591+
}
4592+
4593+
/**
4594+
* Create an edge in an edge collection. This method allows to define to
4595+
* documents key. Note that the collection's property
4596+
* CollectionKeyOption.allowUserKeys has to be set accordingly.
4597+
*
4598+
* @param collectionName
4599+
* name of the edge collection
4600+
* @param documentKey
4601+
* the desired document key
4602+
* @param value
4603+
* the edge object
4604+
* @param fromHandle
4605+
* id of document 'from'
4606+
* @param toHandle
4607+
* id of document 'to'
4608+
* @param waitForSync
4609+
* wait for sync
4610+
* @return the new created EdgeEntity object
4611+
* @throws ArangoException
4612+
*/
4613+
public <T> EdgeEntity<T> createEdge(
4614+
final String collectionName,
4615+
final String documentKey,
4616+
final T value,
4617+
final String fromHandle,
4618+
final String toHandle,
4619+
final Boolean waitForSync) throws ArangoException {
4620+
4621+
return documentDriver.createEdge(getDefaultDatabase(), collectionName, documentKey, value, fromHandle, toHandle,
4622+
waitForSync);
4623+
}
4624+
45674625
/**
45684626
* Do a graph traversal.
45694627
*

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44

55
import com.arangodb.entity.DocumentEntity;
6+
import com.arangodb.entity.EdgeEntity;
67
import com.arangodb.impl.BaseDriverInterface;
78

89
/**
@@ -67,4 +68,14 @@ String getDocumentRaw(String database, String documentHandle, Long ifNoneMatchRe
6768
throws ArangoException;
6869

6970
DocumentEntity<?> deleteDocument(String database, String documentHandle, Long rev) throws ArangoException;
71+
72+
<T> EdgeEntity<T> createEdge(
73+
String database,
74+
String collectionName,
75+
String documentKey,
76+
T value,
77+
String fromHandle,
78+
String toHandle,
79+
Boolean waitForSync) throws ArangoException;
80+
7081
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1959,6 +1959,9 @@ public ReplicationLoggerStateEntity.Client deserialize(
19591959
}
19601960

19611961
public static class GraphEntityDeserializer implements JsonDeserializer<GraphEntity> {
1962+
1963+
private static final String COLLECTION = "collection";
1964+
19621965
@Override
19631966
public GraphEntity deserialize(
19641967
final JsonElement json,
@@ -2008,8 +2011,8 @@ private void addEdgeDefinitions(final GraphEntity entity, final JsonArray edgeDe
20082011
for (int i = 0, imax = edgeDefinitions.size(); i < imax; i++) {
20092012
final EdgeDefinitionEntity edgeDefinitionEntity = new EdgeDefinitionEntity();
20102013
final JsonObject edgeDefinition = edgeDefinitions.get(i).getAsJsonObject();
2011-
if (edgeDefinition.has("collection")) {
2012-
edgeDefinitionEntity.setCollection(edgeDefinition.get("collection").getAsString());
2014+
if (edgeDefinition.has(COLLECTION)) {
2015+
edgeDefinitionEntity.setCollection(edgeDefinition.get(COLLECTION).getAsString());
20132016
}
20142017
if (edgeDefinition.has("from")) {
20152018
final List<String> from = new ArrayList<String>();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727
class BaseArangoDriverImpl extends BaseArangoDriver implements BaseDriverInterface {
2828

29+
protected static final String COLLECTION = "collection";
2930
protected ArangoConfigure configure;
3031
protected HttpManager httpManager;
3132
protected AnnotationHandler annotationHandler;

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

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@
2525
import com.arangodb.entity.CursorEntity;
2626
import com.arangodb.entity.DefaultEntity;
2727
import com.arangodb.entity.DocumentEntity;
28+
import com.arangodb.entity.EdgeEntity;
2829
import com.arangodb.entity.EntityFactory;
2930
import com.arangodb.http.HttpManager;
3031
import com.arangodb.http.HttpResponseEntity;
32+
import com.arangodb.util.EdgeUtils;
3133
import com.arangodb.util.MapBuilder;
3234
import com.google.gson.JsonElement;
35+
import com.google.gson.JsonObject;
3336

3437
/**
3538
* @author tamtam180 - kirscheless at gmail.com
@@ -67,7 +70,7 @@ private <T> DocumentEntity<T> internalCreateDocument(
6770
}
6871

6972
final HttpResponseEntity res = httpManager.doPost(createDocumentEndpointUrl(database),
70-
new MapBuilder().put("collection", collectionName).put(WAIT_FOR_SYNC, waitForSync).get(), body);
73+
new MapBuilder().put(COLLECTION, collectionName).put(WAIT_FOR_SYNC, waitForSync).get(), body);
7174

7275
@SuppressWarnings("unchecked")
7376
final DocumentEntity<T> result = createEntity(res, DocumentEntity.class);
@@ -183,7 +186,7 @@ public DocumentEntity<String> updateDocumentRaw(
183186
@Override
184187
public List<String> getDocuments(final String database, final String collectionName) throws ArangoException {
185188
final HttpResponseEntity res = httpManager.doPut(createEndpointUrl(database, "/_api/simple/all-keys"), null,
186-
EntityFactory.toJsonString(new MapBuilder().put("collection", collectionName).put("type", "id").get()));
189+
EntityFactory.toJsonString(new MapBuilder().put(COLLECTION, collectionName).put("type", "id").get()));
187190

188191
@SuppressWarnings("unchecked")
189192
final CursorEntity<String> tmp = createEntity(res, CursorEntity.class, String.class);
@@ -264,4 +267,36 @@ private Map<String, Object> createRevisionCheckHeader(final Long rev) {
264267
}
265268
return header;
266269
}
270+
271+
@Override
272+
public <T> EdgeEntity<T> createEdge(
273+
String database,
274+
String collectionName,
275+
String documentKey,
276+
T value,
277+
String fromHandle,
278+
String toHandle,
279+
Boolean waitForSync) throws ArangoException {
280+
281+
validateCollectionName(collectionName);
282+
283+
JsonObject obj = EdgeUtils.valueToEdgeJsonObject(documentKey, fromHandle, toHandle, value);
284+
285+
final HttpResponseEntity res = httpManager.doPost(createDocumentEndpointUrl(database),
286+
new MapBuilder().put(COLLECTION, collectionName).put(WAIT_FOR_SYNC, waitForSync).get(),
287+
EntityFactory.toJsonString(obj));
288+
289+
@SuppressWarnings("unchecked")
290+
final EdgeEntity<T> entity = createEntity(res, EdgeEntity.class);
291+
292+
if (value != null) {
293+
entity.setEntity(value);
294+
annotationHandler.updateDocumentAttributes(value, entity.getDocumentRevision(), entity.getDocumentHandle(),
295+
entity.getDocumentKey());
296+
}
297+
298+
entity.setFromVertexHandle(fromHandle);
299+
entity.setToVertexHandle(toHandle);
300+
return entity;
301+
}
267302
}

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

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.arangodb.http.HttpManager;
3535
import com.arangodb.http.HttpResponseEntity;
3636
import com.arangodb.util.CollectionUtils;
37+
import com.arangodb.util.EdgeUtils;
3738
import com.arangodb.util.MapBuilder;
3839
import com.arangodb.util.StringUtils;
3940
import com.google.gson.JsonElement;
@@ -218,7 +219,7 @@ public GraphEntity createVertexCollection(
218219

219220
final HttpResponseEntity res = httpManager.doPost(
220221
createGharialEndpointUrl(databaseName, StringUtils.encodeUrl(graphName), VERTEX), null,
221-
EntityFactory.toJsonString(new MapBuilder().put("collection", collectionName).get()));
222+
EntityFactory.toJsonString(new MapBuilder().put(COLLECTION, collectionName).get()));
222223

223224
if (wrongResult(res)) {
224225
throw new ArangoException(UNKNOWN_ERROR);
@@ -534,7 +535,7 @@ public <T> EdgeEntity<T> createEdge(
534535
final T value,
535536
final Boolean waitForSync) throws ArangoException {
536537

537-
final JsonObject obj = valueToEdgeJsonObject(key, fromHandle, toHandle, value);
538+
final JsonObject obj = EdgeUtils.valueToEdgeJsonObject(key, fromHandle, toHandle, value);
538539

539540
validateCollectionName(graphName);
540541
final HttpResponseEntity res = httpManager.doPost(
@@ -554,34 +555,6 @@ public <T> EdgeEntity<T> createEdge(
554555
return entity;
555556
}
556557

557-
private <T> JsonObject valueToEdgeJsonObject(
558-
final String key,
559-
final String fromHandle,
560-
final String toHandle,
561-
final T value) {
562-
JsonObject obj;
563-
if (value == null) {
564-
obj = new JsonObject();
565-
} else {
566-
final JsonElement elem = EntityFactory.toJsonElement(value, false);
567-
if (elem.isJsonObject()) {
568-
obj = elem.getAsJsonObject();
569-
} else {
570-
throw new IllegalArgumentException("value need object type(not support array, primitive, etc..).");
571-
}
572-
}
573-
if (key != null) {
574-
obj.addProperty("_key", key);
575-
}
576-
if (fromHandle != null) {
577-
obj.addProperty("_from", fromHandle);
578-
}
579-
if (toHandle != null) {
580-
obj.addProperty("_to", toHandle);
581-
}
582-
return obj;
583-
}
584-
585558
@SuppressWarnings("unchecked")
586559
@Override
587560
public <T> EdgeEntity<T> getEdge(
@@ -638,7 +611,7 @@ public <T> EdgeEntity<T> replaceEdge(
638611
final Long ifMatchRevision,
639612
final Long ifNoneMatchRevision) throws ArangoException {
640613

641-
final JsonObject obj = valueToEdgeJsonObject(key, fromHandle, toHandle, value);
614+
final JsonObject obj = EdgeUtils.valueToEdgeJsonObject(key, fromHandle, toHandle, value);
642615

643616
validateCollectionName(graphName);
644617
final HttpResponseEntity res = httpManager.doPut(
@@ -678,7 +651,7 @@ public <T> EdgeEntity<T> updateEdge(
678651
final Long ifMatchRevision,
679652
final Long ifNoneMatchRevision) throws ArangoException {
680653

681-
final JsonObject obj = valueToEdgeJsonObject(key, fromHandle, toHandle, value);
654+
final JsonObject obj = EdgeUtils.valueToEdgeJsonObject(key, fromHandle, toHandle, value);
682655

683656
validateCollectionName(graphName);
684657
final HttpResponseEntity res = httpManager.doPatch(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public ImportResultEntity importDocuments(String database, String collection, Co
4343
throws ArangoException {
4444

4545
HttpResponseEntity res = httpManager.doPost(createEndpointUrl(database, "/_api/import"),
46-
new MapBuilder().put("collection", collection).put("type", "array").get(),
46+
new MapBuilder().put(COLLECTION, collection).put("type", "array").get(),
4747
EntityFactory.toJsonString(values));
4848

4949
return createEntity(res, ImportResultEntity.class);
@@ -57,7 +57,7 @@ public ImportResultEntity importDocumentsByHeaderValues(
5757
Collection<? extends Collection<?>> headerValues) throws ArangoException {
5858

5959
HttpResponseEntity res = httpManager.doPost(createEndpointUrl(database, "/_api/import"),
60-
new MapBuilder().put("collection", collection).get(), EntityFactory.toImportHeaderValues(headerValues));
60+
new MapBuilder().put(COLLECTION, collection).get(), EntityFactory.toImportHeaderValues(headerValues));
6161

6262
return createEntity(res, ImportResultEntity.class);
6363

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class InternalIndexDriverImpl extends BaseArangoDriverWithCursorImpl
3939
private static final String SPARSE = "sparse";
4040
private static final String UNIQUE = "unique";
4141
private static final String TYPE = "type";
42-
private static final String COLLECTION = "collection";
4342

4443
InternalIndexDriverImpl(ArangoConfigure configure, HttpManager httpManager) {
4544
super(configure, null, httpManager);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public <T> void getReplicationDump(
7777
DumpHandler<T> handler) throws ArangoException {
7878

7979
HttpResponseEntity res = httpManager.doGet(createEndpointUrl(database, "/_api/replication/dump"),
80-
new MapBuilder().put("collection", collectionName).put("from", from).put("to", to)
81-
.put("chunkSize", chunkSize).put("ticks", ticks).get());
80+
new MapBuilder().put(COLLECTION, collectionName).put("from", from).put("to", to).put("chunkSize", chunkSize)
81+
.put("ticks", ticks).get());
8282

8383
ReplicationDumpHeader header = toReplicationDumpHeader(res);
8484
boolean cont = handler.head(header);

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public class InternalSimpleDriverImpl extends BaseArangoDriverWithCursorImpl
4242
private static final String WAIT_FOR_SYNC = "waitForSync";
4343
private static final String LIMIT = "limit";
4444
private static final String EXAMPLE = "example";
45-
private static final String COLLECTION = "collection";
4645

4746
InternalSimpleDriverImpl(final ArangoConfigure configure, final InternalCursorDriver cursorDriver,
4847
final HttpManager httpManager) {

0 commit comments

Comments
 (0)