1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40 public interface Accessible extends PermissionGrantTarget {
41
42
43
44
45
46
47 @Fetch(value = Ontology.IS_ACCESSIBLE_TO, ifBelowLevel = 1)
48 @Adjacency(label = Ontology.IS_ACCESSIBLE_TO)
49 Iterable<Accessor> getAccessors();
50
51
52
53
54
55
56
57 @JavaHandler
58 void addAccessor(Accessor accessor);
59
60
61
62
63
64
65 @Adjacency(label = Ontology.IS_ACCESSIBLE_TO)
66 void removeAccessor(Accessor accessor);
67
68
69
70
71
72
73 @Adjacency(label = Ontology.HAS_PERMISSION_SCOPE)
74 PermissionScope getPermissionScope();
75
76
77
78
79
80
81 @JavaHandler
82 void setPermissionScope(PermissionScope scope);
83
84
85
86
87
88
89
90 @JavaHandler
91 Iterable<PermissionScope> getPermissionScopes();
92
93
94
95
96
97
98 @JavaHandler
99 Iterable<SystemEvent> getHistory();
100
101
102
103
104
105
106 @Fetch(value = Ontology.ENTITY_HAS_LIFECYCLE_EVENT, ifLevel = 0)
107 @JavaHandler
108 SystemEvent getLatestEvent();
109
110
111
112
113
114
115 @JavaHandler
116 boolean hasAccessRestriction();
117
118
119
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 }