View Javadoc

1   /*
2    * Copyright 2015 Data Archiving and Networked Services (an institute of
3    * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4    * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5    *
6    * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7    * the European Commission - subsequent versions of the EUPL (the "Licence");
8    * You may not use this work except in compliance with the Licence.
9    * You may obtain a copy of the Licence at:
10   *
11   * https://joinup.ec.europa.eu/software/page/eupl
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the Licence is distributed on an "AS IS" basis,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the Licence for the specific language governing
17   * permissions and limitations under the Licence.
18   */
19  
20  package eu.ehri.project.models.base;
21  
22  import com.tinkerpop.blueprints.Direction;
23  import com.tinkerpop.blueprints.Vertex;
24  import com.tinkerpop.frames.Adjacency;
25  import com.tinkerpop.frames.modules.javahandler.JavaHandler;
26  import com.tinkerpop.frames.modules.javahandler.JavaHandlerContext;
27  import com.tinkerpop.gremlin.java.GremlinPipeline;
28  import eu.ehri.project.definitions.Ontology;
29  import eu.ehri.project.models.annotations.Fetch;
30  import eu.ehri.project.models.events.SystemEvent;
31  import eu.ehri.project.models.utils.JavaHandlerUtils;
32  
33  import static eu.ehri.project.models.utils.JavaHandlerUtils.addSingleRelationship;
34  import static eu.ehri.project.models.utils.JavaHandlerUtils.addUniqueRelationship;
35  import static eu.ehri.project.models.utils.JavaHandlerUtils.hasEdge;
36  
37  /**
38   * An entity that can be accessed by specific {@link Accessor}s.
39   */
40  public interface Accessible extends PermissionGrantTarget {
41  
42      /**
43       * Fetch accessors to which this item is restricted.
44       *
45       * @return an iterable of accessor frames
46       */
47      @Fetch(value = Ontology.IS_ACCESSIBLE_TO, ifBelowLevel = 1)
48      @Adjacency(label = Ontology.IS_ACCESSIBLE_TO)
49      Iterable<Accessor> getAccessors();
50  
51      /**
52       * only Accessor accessor can access this Accessible item.
53       * This is NOT the way to add an Accessor to a Group, use Group.addMember()
54       *
55       * @param accessor an accessor that can access the current item
56       */
57      @JavaHandler
58      void addAccessor(Accessor accessor);
59  
60      /**
61       * Remove an accessor from having access to this item.
62       *
63       * @param accessor an accessor frame
64       */
65      @Adjacency(label = Ontology.IS_ACCESSIBLE_TO)
66      void removeAccessor(Accessor accessor);
67  
68      /**
69       * Fetch the permission scope to which this item belongs, if any.
70       *
71       * @return a permission scope frame, or null
72       */
73      @Adjacency(label = Ontology.HAS_PERMISSION_SCOPE)
74      PermissionScope getPermissionScope();
75  
76      /**
77       * Set this item's permission scope.
78       *
79       * @param scope a permission scope frame
80       */
81      @JavaHandler
82      void setPermissionScope(PermissionScope scope);
83  
84      /**
85       * Get the full hierarchy of permission scopes to which this
86       * item belongs.
87       *
88       * @return an iterable of permission scope frames
89       */
90      @JavaHandler
91      Iterable<PermissionScope> getPermissionScopes();
92  
93      /**
94       * Fetch a list of Actions for this entity in order.
95       *
96       * @return a list of system event frames
97       */
98      @JavaHandler
99      Iterable<SystemEvent> getHistory();
100 
101     /**
102      * Fetch the most recent event that affected this item.
103      *
104      * @return a system event frame, or null
105      */
106     @Fetch(value = Ontology.ENTITY_HAS_LIFECYCLE_EVENT, ifLevel = 0)
107     @JavaHandler
108     SystemEvent getLatestEvent();
109 
110     /**
111      * Determine if this item has access restrictions.
112      *
113      * @return true, if the item is restricted, otherwise false
114      */
115     @JavaHandler
116     boolean hasAccessRestriction();
117 
118     /**
119      * Implementation of complex methods.
120      */
121     abstract class Impl implements JavaHandlerContext<Vertex>, Accessible {
122 
123         public void addAccessor(Accessor accessor) {
124             addUniqueRelationship(it(), accessor.asVertex(),
125                     Ontology.IS_ACCESSIBLE_TO);
126         }
127 
128         public void setPermissionScope(PermissionScope scope) {
129             addSingleRelationship(it(), scope.asVertex(),
130                     Ontology.HAS_PERMISSION_SCOPE);
131         }
132 
133         public SystemEvent getLatestEvent() {
134             GremlinPipeline<Vertex, Vertex> out = gremlin()
135                     .out(Ontology.ENTITY_HAS_LIFECYCLE_EVENT)
136                     .out(Ontology.ENTITY_HAS_EVENT);
137             return (SystemEvent) (out.hasNext() ? frame(out.next()) : null);
138         }
139 
140         public Iterable<PermissionScope> getPermissionScopes() {
141             return frameVertices(gremlin().as("n")
142                     .out(Ontology.HAS_PERMISSION_SCOPE)
143                     .loop("n", JavaHandlerUtils.defaultMaxLoops, JavaHandlerUtils.noopLoopFunc));
144         }
145 
146         public Iterable<SystemEvent> getHistory() {
147             return frameVertices(gremlin().as("n").out(Ontology.ENTITY_HAS_LIFECYCLE_EVENT)
148                     .loop("n", JavaHandlerUtils.noopLoopFunc, JavaHandlerUtils.noopLoopFunc)
149                     .out(Ontology.ENTITY_HAS_EVENT));
150         }
151 
152         public boolean hasAccessRestriction() {
153             return hasEdge(it(), Direction.OUT, Ontology.IS_ACCESSIBLE_TO)
154                     && !hasEdge(it(), Direction.OUT, Ontology.PROMOTED_BY);
155         }
156     }
157 }