1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.neo4j.server.plugins;
21
22 import org.neo4j.graphalgo.GraphAlgoFactory;
23 import org.neo4j.graphalgo.PathFinder;
24 import org.neo4j.graphdb.GraphDatabaseService;
25 import org.neo4j.graphdb.Node;
26 import org.neo4j.graphdb.Path;
27 import org.neo4j.graphdb.Relationship;
28 import org.neo4j.graphdb.RelationshipType;
29 import org.neo4j.graphdb.Transaction;
30 import org.neo4j.helpers.Predicate;
31 import org.neo4j.helpers.collection.FilteringIterable;
32 import org.neo4j.kernel.Traversal;
33
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Set;
38
39 @Description( "Here you can describe your plugin. It will show up in the description of the methods." )
40 public class Plugin extends ServerPlugin
41 {
42 public static final String GET_REFERENCE_NODE = "reference_node_uri";
43 public static final String GET_CONNECTED_NODES = "connected_nodes";
44 static String _string;
45 static Byte _byte;
46 static Character _character;
47 static Integer _integer;
48 static Short _short;
49 static Long _long;
50 static Float _float;
51 static Double _double;
52 static Boolean _boolean;
53 static Long optional;
54 static Set<String> stringSet;
55 static List<String> stringList;
56 static String[] stringArray;
57 public static int[] intArray;
58
59 @Description( "Get the reference node from the graph database" )
60 @PluginTarget( GraphDatabaseService.class )
61 @Name( GET_REFERENCE_NODE )
62 public Node getReferenceNode( @Source GraphDatabaseService graphDb )
63 {
64 return graphDb.getReferenceNode();
65 }
66
67 @Name( GET_CONNECTED_NODES )
68 @PluginTarget( Node.class )
69 public Iterable<Node> getAllConnectedNodes( @Source Node start )
70 {
71 ArrayList<Node> nodes = new ArrayList<Node>();
72
73 for ( Relationship rel : start.getRelationships() )
74 {
75 nodes.add( rel.getOtherNode( start ) );
76 }
77
78 return nodes;
79 }
80
81 @PluginTarget( Node.class )
82 public Iterable<Relationship> getRelationshipsBetween( final @Source Node start,
83 final @Parameter( name = "other" ) Node end )
84 {
85 return new FilteringIterable<Relationship>( start.getRelationships(),
86 new Predicate<Relationship>()
87 {
88 @Override
89 public boolean accept( Relationship item )
90 {
91 return item.getOtherNode( start ).equals( end );
92 }
93 } );
94 }
95
96 @PluginTarget( Node.class )
97 public Iterable<Relationship> createRelationships( @Source Node start,
98 @Parameter( name = "type" ) RelationshipType type,
99 @Parameter( name = "nodes" ) Iterable<Node> nodes )
100 {
101 List<Relationship> result = new ArrayList<Relationship>();
102 Transaction tx = start.getGraphDatabase().beginTx();
103 try
104 {
105 for ( Node end : nodes )
106 {
107 result.add( start.createRelationshipTo( end, type ) );
108 }
109 tx.success();
110 }
111 finally
112 {
113 tx.finish();
114 }
115 return result;
116 }
117
118 @PluginTarget( Node.class )
119 public Node getThisNodeOrById( @Source Node start, @Parameter( name = "id", optional = true ) Long id )
120 {
121 optional = id;
122
123 if ( id == null )
124 {
125 return start;
126 }
127
128 return start.getGraphDatabase().getNodeById( id );
129 }
130
131 @PluginTarget( GraphDatabaseService.class )
132 public Node methodWithIntParam(
133 @Source GraphDatabaseService db,
134 @Parameter( name = "id", optional = false ) int id )
135 {
136 return db.getNodeById( id );
137 }
138
139 @PluginTarget( Relationship.class )
140 public Iterable<Node> methodOnRelationship( @Source Relationship rel )
141 {
142 return Arrays.asList( rel.getNodes() );
143 }
144
145 @PluginTarget( GraphDatabaseService.class )
146 public Node methodWithAllParams(
147 @Source GraphDatabaseService db,
148 @Parameter( name = "id", optional = false ) String a,
149 @Parameter( name = "id2", optional = false ) Byte b,
150 @Parameter( name = "id3", optional = false ) Character c,
151 @Parameter( name = "id4", optional = false ) Short d,
152 @Parameter( name = "id5", optional = false ) Integer e,
153 @Parameter( name = "id6", optional = false ) Long f,
154 @Parameter( name = "id7", optional = false ) Float g,
155 @Parameter( name = "id8", optional = false ) Double h,
156 @Parameter( name = "id9", optional = false ) Boolean i )
157 {
158 _string = a;
159 _byte = b;
160 _character = c;
161 _short = d;
162 _integer = e;
163 _long = f;
164 _float = g;
165 _double = h;
166 _boolean = i;
167
168 return db.getReferenceNode();
169 }
170
171 @PluginTarget( GraphDatabaseService.class )
172 public Node methodWithSet( @Source GraphDatabaseService db,
173 @Parameter( name = "strings", optional = false ) Set<String> params )
174 {
175 stringSet = params;
176 return db.getReferenceNode();
177 }
178
179 @PluginTarget( GraphDatabaseService.class )
180 public Node methodWithList( @Source GraphDatabaseService db,
181 @Parameter( name = "strings", optional = false ) List<String> params )
182 {
183 stringList = params;
184 return db.getReferenceNode();
185 }
186
187 @PluginTarget( GraphDatabaseService.class )
188 public Node methodWithArray( @Source GraphDatabaseService db,
189 @Parameter( name = "strings", optional = false ) String[] params )
190 {
191 stringArray = params;
192 return db.getReferenceNode();
193 }
194
195 @PluginTarget( GraphDatabaseService.class )
196 public Node methodWithIntArray( @Source GraphDatabaseService db,
197 @Parameter( name = "ints", optional = false ) int[] params )
198 {
199 intArray = params;
200 return db.getReferenceNode();
201 }
202
203 @PluginTarget( GraphDatabaseService.class )
204 public Node methodWithOptionalArray( @Source GraphDatabaseService db,
205 @Parameter( name = "ints", optional = true ) int[] params )
206 {
207 intArray = params;
208 return db.getReferenceNode();
209 }
210
211 @PluginTarget( Node.class )
212 public Path pathToReference( @Source Node me )
213 {
214 PathFinder<Path> finder = GraphAlgoFactory.shortestPath( Traversal.expanderForAllTypes(), 6 );
215 return finder.findSinglePath( me.getGraphDatabase().getReferenceNode(), me );
216 }
217 }