View Javadoc

1   package eu.ehri.project.acl.wrapper;
2   
3   import com.tinkerpop.blueprints.Direction;
4   import com.tinkerpop.blueprints.Edge;
5   import com.tinkerpop.blueprints.Features;
6   import com.tinkerpop.blueprints.Graph;
7   import com.tinkerpop.blueprints.GraphQuery;
8   import com.tinkerpop.blueprints.Vertex;
9   import com.tinkerpop.blueprints.util.wrappers.WrappedGraphQuery;
10  import com.tinkerpop.blueprints.util.wrappers.WrapperGraph;
11  import com.tinkerpop.pipes.PipeFunction;
12  import eu.ehri.project.acl.AclManager;
13  import eu.ehri.project.models.base.Accessor;
14  
15  /**
16   * A wrapper graph that hides vertices which are not accessible to the
17   * given accessor.
18   *
19   * @param <T> the underlying graph
20   */
21  public class AclGraph<T extends Graph> implements WrapperGraph<T>, Graph {
22  
23      protected final T baseGraph;
24      private final Accessor accessor;
25      private final PipeFunction<Vertex, Boolean> aclVertexFilter;
26      private final PipeFunction<Edge, Boolean> aclEdgeFilter;
27  
28      public AclGraph(T graph, Accessor accessor) {
29          this.baseGraph = graph;
30          this.accessor = accessor;
31          this.aclVertexFilter = AclManager.getAclFilterFunction(accessor);
32          this.aclEdgeFilter = edge -> aclVertexFilter.compute(edge.getVertex(Direction.OUT))
33                  && aclVertexFilter.compute(edge.getVertex(Direction.IN));
34      }
35  
36      @Override
37      public Features getFeatures() {
38          return baseGraph.getFeatures();
39      }
40  
41      @Override
42      public Vertex addVertex(Object o) {
43          return baseGraph.addVertex(o);
44      }
45  
46      @Override
47      public Vertex getVertex(Object o) {
48          Vertex vertex = baseGraph.getVertex(o);
49          return vertex != null
50                  ? (aclVertexFilter.compute(vertex) ? new AclVertex(vertex, this) : null)
51                  : null;
52      }
53  
54      @Override
55      public void removeVertex(Vertex vertex) {
56          baseGraph.removeVertex(vertex);
57      }
58  
59      @Override
60      public Iterable<Vertex> getVertices() {
61          return new AclVertexIterable(baseGraph.getVertices(), this);
62  
63      }
64  
65      @Override
66      public Iterable<Vertex> getVertices(String s, Object o) {
67          return new AclVertexIterable(baseGraph.getVertices(s, o), this);
68      }
69  
70      @Override
71      public Edge addEdge(Object o, Vertex vertex, Vertex vertex2, String s) {
72          return baseGraph.addEdge(o, vertex, vertex2, s);
73      }
74  
75      @Override
76      public Edge getEdge(Object o) {
77          Edge edge = baseGraph.getEdge(o);
78          return edge != null
79                  ? (aclEdgeFilter.compute(edge) ? new AclEdge(edge, this) : null)
80                  : null;
81      }
82  
83      @Override
84      public void removeEdge(Edge edge) {
85          baseGraph.removeEdge(edge);
86      }
87  
88      @Override
89      public Iterable<Edge> getEdges() {
90          return new AclEdgeIterable(baseGraph.getEdges(), this);
91      }
92  
93      @Override
94      public Iterable<Edge> getEdges(String s, Object o) {
95          return new AclEdgeIterable(baseGraph.getEdges(s, o), this);
96      }
97  
98      @Override
99      public GraphQuery query() {
100         final AclGraph<?> graph = this;
101         return new WrappedGraphQuery(this.baseGraph.query()) {
102             @Override
103             public Iterable<Edge> edges() {
104                 return new AclEdgeIterable(this.query.edges(), graph);
105             }
106 
107             @Override
108             public Iterable<Vertex> vertices() {
109                 return new AclVertexIterable(this.query.vertices(), graph);
110             }
111         };
112     }
113 
114     @Override
115     public void shutdown() {
116         baseGraph.shutdown();
117     }
118 
119     @Override
120     public T getBaseGraph() {
121         return baseGraph;
122     }
123 
124     public Accessor getAccessor() {
125         return accessor;
126     }
127 
128     @Override
129     public String toString() {
130         return "aclgraph(" + accessor.getId() + ")[" + baseGraph + "]";
131     }
132 
133     public boolean evaluateVertex(Vertex vertex) {
134         return aclVertexFilter.compute(vertex);
135     }
136 
137     public boolean evaluateEdge(Edge edge) {
138         return aclEdgeFilter.compute(edge);
139     }
140 
141     /**
142      * Get an ACL-aware vertex from a generic one.
143      *
144      * @param vertex a vertex
145      * @return a vertex with ACL behaviour
146      */
147     public AclVertex aclVertex(Vertex vertex) {
148         return new AclVertex(vertex, this);
149     }
150 }