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;
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   * Frame class representing a group of users or other groups
42   * that can be assigned permissions.
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       * Fetch the groups to which this group belongs.
54       *
55       * @return an iterable of group frames
56       */
57      @Fetch(Ontology.ACCESSOR_BELONGS_TO_GROUP)
58      @Adjacency(label = Ontology.ACCESSOR_BELONGS_TO_GROUP)
59      Iterable<Group> getGroups();
60  
61      /**
62       * TODO FIXME use this in case we need AccesibleEnity's instead of Accessors, 
63       */
64      @Adjacency(label = Ontology.ACCESSOR_BELONGS_TO_GROUP, direction = Direction.IN)
65      Iterable<Accessible> getMembersAsEntities();
66  
67      /**
68       * Get members of this group.
69       *
70       * @return an iterable of user or group frames
71       */
72      @Adjacency(label = Ontology.ACCESSOR_BELONGS_TO_GROUP, direction = Direction.IN)
73      Iterable<Accessor> getMembers();
74  
75      /**
76       * Get the number of items within this group.
77       *
78       * @return a count of group members
79       */
80      @Meta(CHILD_COUNT)
81      @JavaHandler
82      int getChildCount();
83  
84      /**
85       * Adds a Accessor as a member to this Group, so it has the permissions of the Group.
86       *
87       * @param accessor a user or group frame
88       */
89      @JavaHandler
90      void addMember(Accessor accessor);
91  
92      /**
93       * Removes a member from this group.
94       *
95       * @param accessor a user or group frame
96       */
97      @JavaHandler
98      void removeMember(Accessor accessor);
99  
100     /**
101      * Get <b>all</b> members of this group, including members of
102      * groups within this group.
103      *
104      * @return an iterable of user or group frames
105      */
106     @JavaHandler
107     Iterable<Accessible> getAllUserProfileMembers();
108 
109     /**
110      * Implementation of complex methods.
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 }