Skip to content

Commit ac7062a

Browse files
authored
Merge pull request #440 from ali-ince/1.5-add-passing-bookmarks-example
Add example for passing bookmarks
2 parents 026e684 + 2eb94a3 commit ac7062a

File tree

2 files changed

+155
-0
lines changed

2 files changed

+155
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2002-2017 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.neo4j.docs.driver;
20+
21+
// tag::pass-bookmarks-import[]
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
import org.neo4j.driver.v1.AccessMode;
26+
import org.neo4j.driver.v1.Record;
27+
import org.neo4j.driver.v1.Session;
28+
import org.neo4j.driver.v1.StatementResult;
29+
import org.neo4j.driver.v1.Transaction;
30+
31+
import static org.neo4j.driver.v1.Values.parameters;
32+
// end::pass-bookmarks-import[]
33+
34+
public class PassBookmarkExample extends BaseApplication
35+
{
36+
37+
public PassBookmarkExample( String uri, String user, String password )
38+
{
39+
super( uri, user, password );
40+
}
41+
42+
// tag::pass-bookmarks[]
43+
// Create a company node
44+
private StatementResult addCompany( final Transaction tx, final String name )
45+
{
46+
return tx.run( "CREATE (:Company {name: $name})", parameters( "name", name ) );
47+
}
48+
49+
// Create a person node
50+
private StatementResult addPerson( final Transaction tx, final String name )
51+
{
52+
return tx.run( "CREATE (:Person {name: $name})", parameters( "name", name ) );
53+
}
54+
55+
// Create an employment relationship to a pre-existing company node.
56+
// This relies on the person first having been created.
57+
private StatementResult employ( final Transaction tx, final String person, final String company )
58+
{
59+
return tx.run( "MATCH (person:Person {name: $person_name}) " +
60+
"MATCH (company:Company {name: $company_name}) " +
61+
"CREATE (person)-[:WORKS_FOR]->(company)",
62+
parameters( "person_name", person, "company_name", company ) );
63+
}
64+
65+
// Create a friendship between two people.
66+
private StatementResult makeFriends( final Transaction tx, final String person1, final String person2 )
67+
{
68+
return tx.run( "MATCH (a:Person {name: $person_1}) " +
69+
"MATCH (b:Person {name: $person_2}) " +
70+
"MERGE (a)-[:KNOWS]->(b)",
71+
parameters( "person_1", person1, "person_2", person2 ) );
72+
}
73+
74+
// Match and display all friendships.
75+
private StatementResult printFriends( final Transaction tx )
76+
{
77+
StatementResult result = tx.run( "MATCH (a)-[:KNOWS]->(b) RETURN a.name, b.name" );
78+
while ( result.hasNext() )
79+
{
80+
Record record = result.next();
81+
System.out.println( String.format( "%s knows %s", record.get( "a.name" ).asString(), record.get( "b.name" ).toString() ) );
82+
}
83+
return result;
84+
}
85+
86+
public void addEmployAndMakeFriends()
87+
{
88+
// To collect the session bookmarks
89+
List<String> savedBookmarks = new ArrayList<>();
90+
91+
// Create the first person and employment relationship.
92+
try ( Session session1 = driver.session( AccessMode.WRITE ) )
93+
{
94+
session1.writeTransaction( tx -> addCompany( tx, "Wayne Enterprises" ) );
95+
session1.writeTransaction( tx -> addPerson( tx, "Alice" ) );
96+
session1.writeTransaction( tx -> employ( tx, "Alice", "Wayne Enterprises" ) );
97+
98+
savedBookmarks.add( session1.lastBookmark() );
99+
}
100+
101+
// Create the second person and employment relationship.
102+
try ( Session session2 = driver.session( AccessMode.WRITE ) )
103+
{
104+
session2.writeTransaction( tx -> addCompany( tx, "LexCorp" ) );
105+
session2.writeTransaction( tx -> addPerson( tx, "Bob" ) );
106+
session2.writeTransaction( tx -> employ( tx, "Bob", "LexCorp" ) );
107+
108+
savedBookmarks.add( session2.lastBookmark() );
109+
}
110+
111+
// Create a friendship between the two people created above.
112+
try ( Session session3 = driver.session( AccessMode.WRITE, savedBookmarks ) )
113+
{
114+
session3.writeTransaction( tx -> makeFriends( tx, "Alice", "Bob" ) );
115+
116+
session3.readTransaction( this::printFriends );
117+
}
118+
}
119+
// end::pass-bookmarks[]
120+
}

examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import static org.hamcrest.Matchers.greaterThan;
3939
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
4040
import static org.hamcrest.Matchers.instanceOf;
41+
import static org.hamcrest.Matchers.is;
4142
import static org.junit.Assert.assertThat;
4243
import static org.junit.Assert.assertTrue;
4344
import static org.neo4j.driver.v1.Values.parameters;
@@ -97,6 +98,11 @@ private int personCount( String name )
9798
return readInt( "MATCH (a:Person {name: $name}) RETURN count(a)", parameters( "name", name ) );
9899
}
99100

101+
private int companyCount( String name )
102+
{
103+
return readInt( "MATCH (a:Company {name: $name}) RETURN count(a)", parameters( "name", name ) );
104+
}
105+
100106
@Before
101107
public void setUp()
102108
{
@@ -345,4 +351,33 @@ public void testShouldRunTransactionFunctionExample() throws Exception
345351
assertThat( personCount( "Alice" ), greaterThan( 0 ) );
346352
}
347353
}
354+
355+
@Test
356+
public void testPassBookmarksExample() throws Exception
357+
{
358+
try ( PassBookmarkExample example = new PassBookmarkExample( uri, USER, PASSWORD ) )
359+
{
360+
// When
361+
example.addEmployAndMakeFriends();
362+
363+
// Then
364+
assertThat( companyCount( "Wayne Enterprises" ), is( 1 ) );
365+
assertThat( companyCount( "LexCorp" ), is( 1 ) );
366+
assertThat( personCount( "Alice" ), is( 1 ) );
367+
assertThat( personCount( "Bob" ), is( 1 ) );
368+
369+
int employeeCountOfWayne = readInt(
370+
"MATCH (emp:Person)-[WORKS_FOR]->(com:Company) WHERE com.name = 'Wayne Enterprises' RETURN count(emp)" );
371+
assertThat( employeeCountOfWayne, is( 1 ) );
372+
373+
int employeeCountOfLexCorp = readInt(
374+
"MATCH (emp:Person)-[WORKS_FOR]->(com:Company) WHERE com.name = 'LexCorp' RETURN count(emp)" );
375+
assertThat( employeeCountOfLexCorp, is( 1 ) );
376+
377+
int friendCount = readInt(
378+
"MATCH (a:Person {name: 'Alice'})-[:KNOWS]->(b:Person {name: 'Bob'}) RETURN count(a)" );
379+
assertThat( friendCount, is( 1 ) );
380+
}
381+
}
382+
348383
}

0 commit comments

Comments
 (0)