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 eu.ehri.project.definitions.Entities;
28 import eu.ehri.project.definitions.Ontology;
29 import eu.ehri.project.models.annotations.EntityType;
30 import eu.ehri.project.models.annotations.Fetch;
31 import eu.ehri.project.models.annotations.Meta;
32 import eu.ehri.project.models.base.Accessor;
33 import eu.ehri.project.models.base.Actioner;
34 import eu.ehri.project.models.base.Annotatable;
35 import eu.ehri.project.models.base.Versioned;
36 import eu.ehri.project.models.base.Watchable;
37
38 import static eu.ehri.project.definitions.Ontology.ACCESSOR_BELONGS_TO_GROUP;
39 import static eu.ehri.project.definitions.Ontology.USER_FOLLOWS_USER;
40 import static eu.ehri.project.definitions.Ontology.USER_WATCHING_ITEM;
41 import static eu.ehri.project.models.utils.JavaHandlerUtils.*;
42
43
44
45
46 @EntityType(EntityClass.USER_PROFILE)
47 public interface UserProfile extends Accessor, Actioner, Versioned, Annotatable {
48
49 String FOLLOWER_COUNT = "followers";
50 String FOLLOWING_COUNT = "following";
51 String WATCHING_COUNT = "watching";
52
53 @Meta(FOLLOWER_COUNT)
54 @JavaHandler
55 int getFollowerCount();
56
57 @Meta(FOLLOWING_COUNT)
58 @JavaHandler
59 int getFollowingCount();
60
61 @Meta(WATCHING_COUNT)
62 @JavaHandler
63 int getWatchingCount();
64
65
66
67
68
69
70 @Fetch(Ontology.ACCESSOR_BELONGS_TO_GROUP)
71 @Adjacency(label = ACCESSOR_BELONGS_TO_GROUP)
72 Iterable<Group> getGroups();
73
74
75
76
77
78
79 @Adjacency(label = USER_FOLLOWS_USER, direction = Direction.IN)
80 Iterable<UserProfile> getFollowers();
81
82
83
84
85
86
87 @Adjacency(label = USER_FOLLOWS_USER, direction = Direction.OUT)
88 Iterable<UserProfile> getFollowing();
89
90
91
92
93
94
95 @JavaHandler
96 void addFollowing(UserProfile user);
97
98
99
100
101
102
103 @JavaHandler
104 void removeFollowing(UserProfile user);
105
106
107
108
109
110
111
112 @JavaHandler
113 boolean isFollowing(UserProfile otherUser);
114
115
116
117
118
119
120
121 @JavaHandler
122 boolean isFollower(UserProfile otherUser);
123
124
125
126
127
128
129 @Adjacency(label = USER_WATCHING_ITEM, direction = Direction.OUT)
130 Iterable<Watchable> getWatching();
131
132
133
134
135
136
137 @Adjacency(label = Ontology.LINK_HAS_LINKER, direction = Direction.IN)
138 Iterable<Link> getLinks();
139
140
141
142
143
144
145 @Adjacency(label = Ontology.ANNOTATOR_HAS_ANNOTATION)
146 Iterable<Annotation> getAnnotations();
147
148
149
150
151
152
153 @Adjacency(label = Ontology.VC_HAS_AUTHOR, direction = Direction.IN)
154 Iterable<VirtualUnit> getVirtualUnits();
155
156
157
158
159
160
161 @JavaHandler
162 void addWatching(Watchable item);
163
164
165
166
167
168
169 @JavaHandler
170 void removeWatching(Watchable item);
171
172
173
174
175
176
177 @JavaHandler
178 void addBlocked(UserProfile user);
179
180
181
182
183
184
185 @JavaHandler
186 void removeBlocked(UserProfile user);
187
188
189
190
191
192
193 @Adjacency(label = Ontology.USER_BLOCKS_USER)
194 Iterable<UserProfile> getBlocked();
195
196
197
198
199
200
201
202 @JavaHandler
203 boolean isBlocking(UserProfile user);
204
205
206
207
208
209
210 @JavaHandler
211 Iterable<UserProfile> coGroupMembers();
212
213
214
215
216
217
218
219 @JavaHandler
220 boolean isWatching(Watchable item);
221
222
223 abstract class Impl implements JavaHandlerContext<Vertex>, UserProfile {
224
225 @Override
226 public int getFollowerCount() {
227 return Math.toIntExact(gremlin().inE(USER_FOLLOWS_USER).count());
228 }
229
230 @Override
231 public int getFollowingCount() {
232 return Math.toIntExact(gremlin().outE(USER_FOLLOWS_USER).count());
233 }
234
235 @Override
236 public int getWatchingCount() {
237 return Math.toIntExact(gremlin().outE(USER_WATCHING_ITEM).count());
238 }
239
240 @Override
241 public void addFollowing(UserProfile user) {
242 addUniqueRelationship(it(), user.asVertex(), USER_FOLLOWS_USER);
243 }
244
245 @Override
246 public void removeFollowing(UserProfile user) {
247 removeAllRelationships(it(), user.asVertex(), USER_FOLLOWS_USER);
248 }
249
250 @Override
251 public boolean isFollowing(UserProfile otherUser) {
252 return hasRelationship(it(), otherUser.asVertex(), USER_FOLLOWS_USER);
253 }
254
255 @Override
256 public boolean isFollower(UserProfile otherUser) {
257 return hasRelationship(otherUser.asVertex(), it(), USER_FOLLOWS_USER);
258 }
259
260 @Override
261 public void addWatching(Watchable item) {
262 addUniqueRelationship(it(), item.asVertex(), USER_WATCHING_ITEM);
263 }
264
265 @Override
266 public void removeWatching(Watchable item) {
267 removeAllRelationships(it(), item.asVertex(), USER_WATCHING_ITEM);
268 }
269
270 @Override
271 public boolean isWatching(Watchable item) {
272 return hasRelationship(it(), item.asVertex(), USER_WATCHING_ITEM);
273 }
274
275 @Override
276 public Iterable<UserProfile> coGroupMembers() {
277 return frameVertices(gremlin().as("n")
278 .out(Ontology.ACCESSOR_BELONGS_TO_GROUP)
279 .loop("n", defaultMaxLoops, noopLoopFunc)
280 .in(Ontology.ACCESSOR_BELONGS_TO_GROUP).filter(vertex -> {
281
282 if (it().equals(vertex)) {
283 return false;
284 }
285
286 String type = vertex.getProperty(EntityType.TYPE_KEY);
287 return Entities.USER_PROFILE.equals(type);
288 }));
289 }
290
291 @Override
292 public void addBlocked(UserProfile user) {
293 addUniqueRelationship(it(), user.asVertex(),
294 Ontology.USER_BLOCKS_USER);
295 }
296
297 @Override
298 public void removeBlocked(UserProfile user) {
299 removeAllRelationships(it(), user.asVertex(),
300 Ontology.USER_BLOCKS_USER);
301 }
302
303 @Override
304 public boolean isBlocking(UserProfile user) {
305 return hasRelationship(it(), user.asVertex(),
306 Ontology.USER_BLOCKS_USER);
307 }
308 }
309 }