|
| 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 | +} |
0 commit comments