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 eu.ehri.project.definitions.Ontology;
28 import eu.ehri.project.models.UserProfile;
29 import eu.ehri.project.models.annotations.Fetch;
30
31 import static eu.ehri.project.models.utils.JavaHandlerUtils.*;
32
33
34
35
36 public interface Promotable extends Accessible {
37 String PROMOTION_SCORE = "_promotionScore";
38
39 @Fetch(value = Ontology.PROMOTED_BY, numLevels = 1)
40 @Adjacency(label = Ontology.PROMOTED_BY, direction = Direction.OUT)
41 Iterable<UserProfile> getPromoters();
42
43 @Fetch(value = Ontology.DEMOTED_BY, numLevels = 1)
44 @Adjacency(label = Ontology.DEMOTED_BY, direction = Direction.OUT)
45 Iterable<UserProfile> getDemoters();
46
47 @JavaHandler
48 void addPromotion(UserProfile user);
49
50 @JavaHandler
51 void addDemotion(UserProfile user);
52
53 @JavaHandler
54 void removePromotion(UserProfile user);
55
56 @JavaHandler
57 void removeDemotion(UserProfile user);
58
59 @JavaHandler
60 boolean isPromoted();
61
62 @JavaHandler
63 boolean isPromotedBy(UserProfile user);
64
65 @JavaHandler
66 boolean isPromotable();
67
68 @JavaHandler
69 int getPromotionScore();
70
71
72
73
74 abstract class Impl implements JavaHandlerContext<Vertex>, Promotable {
75
76 private void updatePromotionScoreCache() {
77 int score = Math.toIntExact(gremlin().out(Ontology.PROMOTED_BY).count()
78 - gremlin().out(Ontology.DEMOTED_BY).count());
79 it().setProperty(PROMOTION_SCORE, score);
80 }
81
82 @Override
83 public void addPromotion(UserProfile user) {
84 addUniqueRelationship(it(), user.asVertex(), Ontology.PROMOTED_BY);
85 removeAllRelationships(it(), user.asVertex(), Ontology.DEMOTED_BY);
86 updatePromotionScoreCache();
87 }
88
89 @Override
90 public void removePromotion(UserProfile user) {
91 removeAllRelationships(it(), user.asVertex(), Ontology.PROMOTED_BY);
92 updatePromotionScoreCache();
93 }
94
95 @Override
96 public void addDemotion(UserProfile user) {
97 addUniqueRelationship(it(), user.asVertex(), Ontology.DEMOTED_BY);
98 removeAllRelationships(it(), user.asVertex(), Ontology.PROMOTED_BY);
99 updatePromotionScoreCache();
100 }
101
102 @Override
103 public void removeDemotion(UserProfile user) {
104 removeAllRelationships(it(), user.asVertex(), Ontology.DEMOTED_BY);
105 updatePromotionScoreCache();
106 }
107
108 @Override
109 public boolean isPromoted() {
110 return gremlin().out(Ontology.PROMOTED_BY).count() > gremlin().out(Ontology.DEMOTED_BY).count();
111 }
112
113 @Override
114 public int getPromotionScore() {
115 Integer score = it().getProperty(PROMOTION_SCORE);
116 return score == null
117 ? Math.toIntExact(gremlin().out(Ontology.PROMOTED_BY).count()
118 - gremlin().out(Ontology.DEMOTED_BY).count())
119 : score;
120 }
121
122 @Override
123 public boolean isPromotedBy(UserProfile user) {
124 return hasRelationship(it(), user.asVertex(), Ontology.PROMOTED_BY);
125 }
126
127 @Override
128 public boolean isPromotable() {
129 Boolean promotable = it().getProperty(Ontology.IS_PROMOTABLE);
130 return promotable != null && promotable;
131 }
132 }
133 }