This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
BatchRestAPI.java
118 lines (95 loc) · 4.27 KB
/
BatchRestAPI.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package org.neo4j.rest.graphdb;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.PropertyContainer;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.index.IndexHits;
import org.neo4j.helpers.collection.MapUtil;
import org.neo4j.rest.graphdb.index.IndexInfo;
import org.neo4j.rest.graphdb.index.SimpleIndexHits;
import java.util.Map;
public class BatchRestAPI extends RestAPI {
public BatchRestAPI( String uri ) {
super(uri);
}
public BatchRestAPI( String uri, String user, String password ) {
super(uri, user, password);
}
public BatchRestAPI(String uri, ExecutingRestRequest executingRestRequest){
super(uri);
this.restRequest = new RecordingRestRequest(executingRestRequest, new RestOperations());
}
@Override
protected RestRequest createRestRequest( String uri, String user, String password){
return new RecordingRestRequest(new ExecutingRestRequest(uri, user, password),new RestOperations());
}
@Override
public Node createRestNode(RequestResult requestResult) {
final long batchId = requestResult.getBatchId();
Node node = new RestNode("{"+batchId+"}", this);
(getRecordingRequest()).getOperations().addToRestOperation(batchId, node, new RestEntityExtractor(this));
return node;
}
@Override
public RestRelationship createRelationship(Node startNode, Node endNode, RelationshipType type, Map<String, Object> props) {
final RestRequest restRequest = ((RestNode)startNode).getRestRequest();
Map<String, Object> data = MapUtil.map("to", ((RestNode)endNode).getUri(), "type", type.name());
if (props!=null && props.size()>0) {
data.put("data",props);
}
//RequestResult requestResult = restRequest.post(restRequest.getUri()+"/relationships", data);
RequestResult requestResult = restRequest.post( "relationships", data);
return createRestRelationship(requestResult, startNode);
}
@Override
public RestRelationship createRestRelationship(RequestResult requestResult, Node startNode) {
final long batchId = requestResult.getBatchId();
RestRelationship relationship = new RestRelationship("{"+batchId+"}", this);
getRecordingRequest().getOperations().addToRestOperation(batchId, relationship, new RestEntityExtractor(this));
return relationship;
}
private RecordingRestRequest getRecordingRequest() {
return (RecordingRestRequest)this.restRequest;
}
public RestOperations getRecordedOperations(){
return (getRecordingRequest()).getOperations();
}
public void stop() {
getRecordingRequest().stop();
}
@SuppressWarnings("unchecked")
public Iterable<Relationship> wrapRelationships( RequestResult requestResult ) {
final long batchId = requestResult.getBatchId();
final BatchIterable<Relationship> result = new BatchIterable<Relationship>(requestResult);
getRecordingRequest().getOperations().addToRestOperation(batchId, result, new RelationshipIterableConverter(this));
return result;
}
public <S extends PropertyContainer> IndexHits<S> queryIndex(String indexPath, Class<S> entityType) {
RequestResult response = restRequest.get(indexPath);
final long batchId = response.getBatchId();
final SimpleIndexHits<S> result = new SimpleIndexHits<S>(batchId, entityType, this);
getRecordingRequest().getOperations().addToRestOperation(batchId, result, new RestIndexHitsConverter(this,entityType));
return result;
}
public IndexInfo indexInfo(final String indexType) {
return new BatchIndexInfo();
}
private static class BatchIndexInfo implements IndexInfo {
@Override
public boolean checkConfig(String indexName, Map<String, String> config) {
return true;
}
@Override
public String[] indexNames() {
return new String[0];
}
@Override
public boolean exists(String indexName) {
return true;
}
@Override
public Map<String, String> getConfig(String name) {
return null;
}
}
}