1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.neo4j.examples.server;
20
21 import java.net.URI;
22 import java.net.URISyntaxException;
23
24 import javax.ws.rs.core.MediaType;
25
26 import com.sun.jersey.api.client.Client;
27 import com.sun.jersey.api.client.ClientResponse;
28 import com.sun.jersey.api.client.WebResource;
29
30 public class CreateSimpleGraph {
31
32 private static final String SERVER_ROOT_URI = "http://localhost:7474/db/data/";
33
34 public static void main(String[] args) throws URISyntaxException {
35 checkDatabaseIsRunning();
36
37
38 URI firstNode = createNode();
39 addProperty(firstNode, "name", "Joe Strummer");
40 URI secondNode = createNode();
41 addProperty(secondNode, "band", "The Clash");
42
43
44
45 URI relationshipUri = addRelationship(firstNode, secondNode, "singer", "{ \"from\" : \"1976\", \"until\" : \"1986\" }");
46
47
48
49 addMetadataToProperty(relationshipUri, "stars", "5");
50
51
52
53 findSingersInBands(firstNode);
54
55 }
56
57 private static void findSingersInBands(URI startNode) throws URISyntaxException {
58
59
60 TraversalDescription t = new TraversalDescription();
61 t.setOrder(TraversalDescription.DEPTH_FIRST);
62 t.setUniqueness(TraversalDescription.NODE);
63 t.setMaxDepth(10);
64 t.setReturnFilter(TraversalDescription.ALL);
65 t.setRelationships(new Relationship("singer", Relationship.OUT));
66
67
68
69 URI traverserUri = new URI(startNode.toString() + "/traverse/node");
70 WebResource resource = Client.create().resource(traverserUri);
71 String jsonTraverserPayload = t.toJson();
72 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).entity(jsonTraverserPayload).post(ClientResponse.class);
73
74 System.out.println(String.format("POST [%s] to [%s], status code [%d], returned data: " + System.getProperty("line.separator") + "%s", jsonTraverserPayload, traverserUri, response.getStatus(), response.getEntity(String.class)));
75
76 }
77
78
79 private static void addMetadataToProperty(URI relationshipUri, String name, String value) throws URISyntaxException {
80 URI propertyUri = new URI(relationshipUri.toString() + "/properties");
81 WebResource resource = Client.create().resource(propertyUri);
82
83 String entity = toJsonNameValuePairCollection(name, value);
84 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).entity(entity).put(ClientResponse.class);
85
86 System.out.println(String.format("PUT [%s] to [%s], status code [%d]", entity, propertyUri, response.getStatus()));
87 }
88
89
90 private static String toJsonNameValuePairCollection(String name, String value) {
91 return String.format("{ \"%s\" : \"%s\" }", name, value);
92 }
93
94 private static URI createNode() {
95
96 final String nodeEntryPointUri = SERVER_ROOT_URI + "node";
97
98 WebResource resource = Client.create().resource(nodeEntryPointUri);
99 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).entity("{}").post(ClientResponse.class);
100
101 System.out.println(String.format("POST to [%s], status code [%d], location header [%s]", nodeEntryPointUri, response.getStatus(), response.getLocation().toString()));
102
103 return response.getLocation();
104
105 }
106
107
108 private static URI addRelationship(URI startNode, URI endNode, String relationshipType, String jsonAttributes) throws URISyntaxException {
109 URI fromUri = new URI(startNode.toString() + "/relationships");
110 String relationshipJson = generateJsonRelationship(endNode, relationshipType, jsonAttributes);
111
112 WebResource resource = Client.create().resource(fromUri);
113 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).entity(relationshipJson).post(ClientResponse.class);
114
115 System.out.println(String.format("POST to [%s], status code [%d], location header [%s]", fromUri, response.getStatus(), response.getLocation().toString()));
116
117 return response.getLocation();
118 }
119
120
121 private static String generateJsonRelationship(URI endNode, String relationshipType, String ... jsonAttributes) {
122 StringBuilder sb = new StringBuilder();
123 sb.append("{ \"to\" : \"");
124 sb.append(endNode.toString());
125 sb.append("\", ");
126
127 sb.append("\"type\" : \"");
128 sb.append(relationshipType);
129 if(jsonAttributes == null || jsonAttributes.length < 1) {
130 sb.append("\"");
131 } else {
132 sb.append("\", \"data\" : ");
133 for(int i = 0; i < jsonAttributes.length; i++) {
134 sb.append(jsonAttributes[i]);
135 if(i < jsonAttributes.length -1) {
136 sb.append(", ");
137 }
138 }
139 }
140
141 sb.append(" }");
142 return sb.toString();
143 }
144
145 private static void addProperty(URI nodeUri, String propertyName, String propertyValue) {
146
147 String propertyUri = nodeUri.toString() + "/properties/" + propertyName;
148
149 WebResource resource = Client.create().resource(propertyUri);
150 ClientResponse response = resource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).entity("\"" + propertyValue + "\"").put(ClientResponse.class);
151
152 System.out.println(String.format("PUT to [%s], status code [%d]", propertyUri, response.getStatus()));
153
154 }
155
156 private static void checkDatabaseIsRunning() {
157
158 WebResource resource = Client.create().resource(SERVER_ROOT_URI);
159 ClientResponse response = resource.get(ClientResponse.class);
160
161 System.out.println(String.format("GET on [%s], status code [%d]", SERVER_ROOT_URI, response.getStatus()));
162
163 }
164 }