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;
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.Entities;
29 import eu.ehri.project.definitions.Ontology;
30 import eu.ehri.project.models.annotations.EntityType;
31 import eu.ehri.project.models.annotations.Fetch;
32 import eu.ehri.project.models.annotations.Meta;
33 import eu.ehri.project.models.base.Accessible;
34 import eu.ehri.project.models.base.Accessor;
35 import eu.ehri.project.models.base.ItemHolder;
36 import eu.ehri.project.models.base.Named;
37 import eu.ehri.project.models.base.PermissionScope;
38 import eu.ehri.project.models.utils.JavaHandlerUtils;
39
40
41
42
43
44 @EntityType(EntityClass.GROUP)
45 public interface Group extends Accessor, Accessible,
46 PermissionScope, Named, ItemHolder {
47
48 String ADMIN_GROUP_IDENTIFIER = "admin";
49 String ANONYMOUS_GROUP_IDENTIFIER = "anonymous";
50 String ADMIN_GROUP_NAME = "Administrators";
51
52
53
54
55
56
57 @Fetch(Ontology.ACCESSOR_BELONGS_TO_GROUP)
58 @Adjacency(label = Ontology.ACCESSOR_BELONGS_TO_GROUP)
59 Iterable<Group> getGroups();
60
61
62
63
64 @Adjacency(label = Ontology.ACCESSOR_BELONGS_TO_GROUP, direction = Direction.IN)
65 Iterable<Accessible> getMembersAsEntities();
66
67
68
69
70
71
72 @Adjacency(label = Ontology.ACCESSOR_BELONGS_TO_GROUP, direction = Direction.IN)
73 Iterable<Accessor> getMembers();
74
75
76
77
78
79
80 @Meta(CHILD_COUNT)
81 @JavaHandler
82 int getChildCount();
83
84
85
86
87
88
89 @JavaHandler
90 void addMember(Accessor accessor);
91
92
93
94
95
96
97 @JavaHandler
98 void removeMember(Accessor accessor);
99
100
101
102
103
104
105
106 @JavaHandler
107 Iterable<Accessible> getAllUserProfileMembers();
108
109
110
111
112 abstract class Impl implements JavaHandlerContext<Vertex>, Group {
113
114 public int getChildCount() {
115 return Math.toIntExact(gremlin().inE(Ontology.ACCESSOR_BELONGS_TO_GROUP).count());
116 }
117
118 public void addMember(Accessor accessor) {
119 JavaHandlerUtils.addUniqueRelationship(accessor.asVertex(), it(),
120 Ontology.ACCESSOR_BELONGS_TO_GROUP);
121 }
122
123 public void removeMember(Accessor accessor) {
124 JavaHandlerUtils.removeAllRelationships(accessor.asVertex(),
125 it(), Ontology.ACCESSOR_BELONGS_TO_GROUP);
126 }
127
128 public Iterable<Accessible> getAllUserProfileMembers() {
129 GremlinPipeline<Vertex,Vertex> pipe = gremlin().as("n").in(Ontology.ACCESSOR_BELONGS_TO_GROUP)
130 .loop("n", JavaHandlerUtils.defaultMaxLoops,
131 vertexLoopBundle -> vertexLoopBundle.getObject()
132 .getProperty(EntityType.TYPE_KEY)
133 .equals(Entities.USER_PROFILE));
134 return frameVertices(pipe.dedup());
135 }
136 }
137 }