    1   /**
    2    * Licensed to Neo Technology under one or more contributor
    3    * license agreements. See the NOTICE file distributed with
    4    * this work for additional information regarding copyright
    5    * ownership. Neo Technology licenses this file to you under
    6    * the Apache License, Version 2.0 (the "License"); you may
    7    * not use this file except in compliance with the License.
    8    * You may obtain a copy of the License at
    9    *
    10   * http://www.apache.org/licenses/LICENSE-2.0
    11   *
    12   * Unless required by applicable law or agreed to in writing,
    13   * software distributed under the License is distributed on an
    14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    15   * KIND, either express or implied. See the License for the
    16   * specific language governing permissions and limitations
    17   * under the License.
    18   */
    19
    20  package org.neo4j.examples;
    21
    22  import java.io.File;
    23
    24  import org.junit.AfterClass;
    25  import org.junit.BeforeClass;
    26  import org.junit.Test;
    27  import org.neo4j.graphdb.Direction;
    28  import org.neo4j.graphdb.GraphDatabaseService;
    29  import org.neo4j.graphdb.Node;
    30  import org.neo4j.graphdb.Relationship;
    31  import org.neo4j.graphdb.RelationshipType;
    32  import org.neo4j.graphdb.ReturnableEvaluator;
    33  import org.neo4j.graphdb.StopEvaluator;
    34  import org.neo4j.graphdb.Transaction;
    35  import org.neo4j.graphdb.TraversalPosition;
    36  import org.neo4j.graphdb.Traverser;
    37  import org.neo4j.graphdb.index.Index;
    38  import org.neo4j.kernel.EmbeddedGraphDatabase;
    39
    40  public class RolesOldTest
    41  {
    42      private static final String GROUP = "group";
    43      private static final String USER = "user";
    44      private static final String NAME = "name";
    45
    46      public enum RoleRels implements RelationshipType
    47      {
    48          ROOT,
    49          PART_OF,
    50          MEMBER_OF;
    51      }
    52
    53      private static final String ROLES_DB = "target/roles-db";
    54      private static GraphDatabaseService graphDb;
    55      private static Index<Node> index;
    56
    57      @BeforeClass
    58      public static void setUp()
    59      {
    60          deleteFileOrDirectory( new File( ROLES_DB ) );
    61          graphDb = new EmbeddedGraphDatabase( ROLES_DB );
    62          index = graphDb.index().forNodes( "nodes" );
    63          registerShutdownHook();
    64          createNodespace();
    65      }
    66
    67      @AfterClass
    68      public static void tearDown()
    69      {
    70          graphDb.shutdown();
    71      }
    72
    73      private static void createNodespace()
    74      {
    75          Transaction tx = graphDb.beginTx();
    76          try
    77          {
    78              // add the top level groups
    79              Node admins = createTopLevelGroup( "Admins" );
    80              Node users = createTopLevelGroup( "Users" );
    81
    82              // add other groups
    83              Node helpDesk = createGroup( "HelpDesk", admins );
    84              Node managers = createGroup( "Managers", users );
    85              Node technicians = createGroup( "Technicians", users );
    86              Node abcTechnicians = createGroup( "ABCTechnicians", technicians );
    87
    88              // add the users
    89              createUser( "Ali", admins, users );
    90              createUser( "Burcu", users );
    91              createUser( "Can", users );
    92              createUser( "Demet", helpDesk );
    93              createUser( "Engin", helpDesk, users );
    94              createUser( "Fuat", managers );
    95              createUser( "Gul", managers );
    96              createUser( "Hakan", technicians );
    97              createUser( "Irmak", technicians );
    98              createUser( "Jale", abcTechnicians );
    99
    100             tx.success();
    101         }
    102         finally
    103         {
    104             tx.finish();
    105         }
    106     }
    107
    108     public static void createRoles()
    109     {
    110         Transaction tx = graphDb.beginTx();
    111         try
    112         {
    113             tx.success();
    114         }
    115         finally
    116         {
    117             tx.finish();
    118         }
    119     }
    120
    121     private static Node createTopLevelGroup( final String name )
    122     {
    123         return createNode( name, RoleRels.ROOT, GROUP,
    124                 graphDb.getReferenceNode() );
    125     }
    126
    127     private static Node createGroup( final String name,
    128             final Node... containedIn )
    129     {
    130         return createNode( name, RoleRels.PART_OF, GROUP, containedIn );
    131     }
    132
    133     private static Node createUser( final String name,
    134             final Node... containedIn )
    135     {
    136         return createNode( name, RoleRels.MEMBER_OF, USER, containedIn );
    137     }
    138
    139     private static Node createNode( final String name,
    140             final RelationshipType relType, final String category,
    141             final Node... containedIn )
    142     {
    143         Node node = graphDb.createNode();
    144         node.setProperty( NAME, name );
    145         index.add( node, category, name );
    146         for ( Node parent : containedIn )
    147         {
    148             node.createRelationshipTo( parent, relType );
    149         }
    150         return node;
    151     }
    152
    153     private static Node getUserByName( final String name )
    154     {
    155         return getNodeByName( USER, name );
    156     }
    157
    158     private static Node getGroupByName( String name )
    159     {
    160         return getNodeByName( GROUP, name );
    161     }
    162
    163     private static Node getNodeByName( final String category, final String name )
    164     {
    165         return index.get( category, name ).getSingle();
    166     }
    167
    168     @Test
    169     public void getAllAdmins()
    170     {
    171         Transaction tx = graphDb.beginTx();
    172         try
    173         {
    174             System.out.println( "All admins:" );
    175             // START SNIPPET: get-admins
    176             Node admins = getGroupByName( "Admins" );
    177             Traverser traverser = admins.traverse(
    178                     Traverser.Order.BREADTH_FIRST, StopEvaluator.END_OF_GRAPH,
    179                     ReturnableEvaluator.ALL_BUT_START_NODE, RoleRels.PART_OF,
    180                     Direction.INCOMING, RoleRels.MEMBER_OF, Direction.INCOMING );
    181             for ( Node part : traverser )
    182             {
    183                 System.out.println( part.getProperty( NAME )
    184                                     + " "
    185                                     + ( traverser.currentPosition().depth() - 1 ) );
    186             }
    187             // END SNIPPET: get-admins
    188             tx.success();
    189         }
    190         finally
    191         {
    192             tx.finish();
    193         }
    194     }
    195
    196     @Test
    197     public void getJalesMemberships() throws Exception
    198     {
    199         Transaction tx = graphDb.beginTx();
    200         try
    201         {
    202             System.out.println( "Jale's memberships:" );
    203             // START SNIPPET: get-user-memberships
    204             Node jale = getUserByName( "Jale" );
    205             Traverser traverser = jale.traverse( Traverser.Order.DEPTH_FIRST,
    206                     StopEvaluator.END_OF_GRAPH,
    207                     ReturnableEvaluator.ALL_BUT_START_NODE, RoleRels.MEMBER_OF,
    208                     Direction.OUTGOING, RoleRels.PART_OF, Direction.OUTGOING );
    209             for ( Node membership : traverser )
    210             {
    211                 System.out.println( membership.getProperty( NAME )
    212                                     + " "
    213                                     + ( traverser.currentPosition().depth() - 1 ) );
    214             }
    215             // END SNIPPET: get-user-memberships
    216             tx.success();
    217         }
    218         finally
    219         {
    220             tx.finish();
    221         }
    222     }
    223
    224     @Test
    225     public void getAllGroups() throws Exception
    226     {
    227         Transaction tx = graphDb.beginTx();
    228         try
    229         {
    230             System.out.println( "All groups:" );
    231             // START SNIPPET: get-groups
    232             Node referenceNode = graphDb.getReferenceNode();
    233             Traverser traverser = referenceNode.traverse(
    234                     Traverser.Order.BREADTH_FIRST, StopEvaluator.END_OF_GRAPH,
    235                     ReturnableEvaluator.ALL_BUT_START_NODE, RoleRels.ROOT,
    236                     Direction.INCOMING, RoleRels.PART_OF, Direction.INCOMING );
    237             for ( Node group : traverser )
    238             {
    239                 System.out.println( group.getProperty( NAME ) );
    240             }
    241             // END SNIPPET: get-groups
    242             tx.success();
    243         }
    244         finally
    245         {
    246             tx.finish();
    247         }
    248     }
    249
    250     @Test
    251     public void getAllMembers() throws Exception
    252     {
    253         Transaction tx = graphDb.beginTx();
    254         try
    255         {
    256             System.out.println( "All members:" );
    257             // START SNIPPET: get-members
    258             Node referenceNode = graphDb.getReferenceNode();
    259             Traverser traverser = referenceNode.traverse(
    260                     Traverser.Order.BREADTH_FIRST, StopEvaluator.END_OF_GRAPH,
    261                     new ReturnableEvaluator()
    262                     {
    263                         public boolean isReturnableNode(
    264                                 TraversalPosition currentPos )
    265                         {
    266                             if ( currentPos.isStartNode() )
    267                             {
    268                                 return false;
    269                             }
    270                             Relationship rel = currentPos.lastRelationshipTraversed();
    271                             return rel.isType( RoleRels.MEMBER_OF );
    272                         }
    273                     }, RoleRels.ROOT,
    274                     Direction.INCOMING, RoleRels.PART_OF, Direction.INCOMING,
    275                     RoleRels.MEMBER_OF, Direction.INCOMING );
    276             for ( Node group : traverser )
    277             {
    278                 System.out.println( group.getProperty( NAME ) );
    279             }
    280             // END SNIPPET: get-members
    281             tx.success();
    282         }
    283         finally
    284         {
    285             tx.finish();
    286         }
    287     }
    288
    289     private static void registerShutdownHook()
    290     {
    291         // Registers a shutdown hook for the Neo4j instance so that it
    292         // shuts down nicely when the VM exits (even if you "Ctrl-C" the
    293         // running example before it's completed)
    294         Runtime.getRuntime().addShutdownHook( new Thread()
    295         {
    296             @Override
    297             public void run()
    298             {
    299                 graphDb.shutdown();
    300             }
    301         } );
    302     }
    303
    304     private static void deleteFileOrDirectory( final File file )
    305     {
    306         if ( !file.exists() )
    307         {
    308             return;
    309         }
    310
    311         if ( file.isDirectory() )
    312         {
    313             for ( File child : file.listFiles() )
    314             {
    315                 deleteFileOrDirectory( child );
    316             }
    317         }
    318         else
    319         {
    320             file.delete();
    321         }
    322     }
    323 }