View Javadoc

1   /*
2    * Copyright 2011 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may	obtain a copy of the License at
7    *
8    * 	http://www.osedu.org/licenses/ECL-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.student.kim.permission.mock;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.UUID;
24  import org.kuali.rice.core.api.criteria.QueryByCriteria;
25  import org.kuali.rice.core.api.exception.RiceIllegalArgumentException;
26  
27  
28  import org.kuali.rice.kim.api.KimConstants;
29  import org.kuali.rice.kim.api.group.Group;
30  import org.kuali.rice.kim.api.group.GroupMember;
31  import org.kuali.rice.kim.api.group.GroupMemberQueryResults;
32  import org.kuali.rice.kim.api.group.GroupQueryResults;
33  import org.kuali.rice.kim.api.group.GroupService;
34  import org.kuali.rice.kim.api.role.Role;
35  import org.kuali.student.common.mock.MockService;
36  
37  import javax.jws.WebParam;
38  
39  /**
40   * @author nwright
41   */
42  public class GroupServiceMockImpl implements GroupService, MockService {
43  
44      private transient Map<String, Group> groupCache = new HashMap<String, Group>();
45      private transient Map<String, GroupMember> groupMembershipCache = new HashMap<String, GroupMember>();
46  
47      
48      @Override
49  	public void clear() {
50      	this.groupCache.clear();
51      	this.groupMembershipCache.clear();
52      	
53  	}
54  
55  	@Override
56      public List<String> getDirectGroupIdsByPrincipalId(String principalId) {
57          List<String> groups = new ArrayList<String>();
58          for (GroupMember groupMembership : this.groupMembershipCache.values()) {
59              if (groupMembership.getType().getCode().equals(KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) {
60                  if (groupMembership.getMemberId().equals(principalId)) {
61                      groups.add(groupMembership.getGroupId());
62                  }
63              }
64          }
65          return groups;
66      }
67  
68      @Override
69      public List<String> getDirectMemberGroupIds(String groupId) {
70          List<String> members = new ArrayList<String>();
71          for (GroupMember groupMembership : this.groupMembershipCache.values()) {
72              if (groupMembership.getType().getCode().equals(KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) {
73                  if (groupMembership.getGroupId().equals(groupId)) {
74                      members.add(groupMembership.getMemberId());
75                  }
76              }
77          }
78          return members;
79      }
80  
81      @Override
82      public List<String> getDirectMemberPrincipalIds(String groupId) {
83          List<String> members = new ArrayList<String>();
84          for (GroupMember groupMembership : this.groupMembershipCache.values()) {
85              if (groupMembership.getType().getCode().equals(KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) {
86                  if (groupMembership.getGroupId().equals(groupId)) {
87                      members.add(groupMembership.getMemberId());
88                  }
89              }
90          }
91          return members;
92      }
93  
94      @Override
95      public List<String> getDirectParentGroupIds(String groupId) {
96          List<String> members = new ArrayList<String>();
97          for (GroupMember groupMembership : this.groupMembershipCache.values()) {
98              if (groupMembership.getType().getCode().equals(KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) {
99                  if (groupMembership.getMemberId().equals(groupId)) {
100                     members.add(groupMembership.getGroupId());
101                 }
102             }
103         }
104         return members;
105     }
106 
107 
108     @Override
109     public GroupMemberQueryResults findGroupMembers(QueryByCriteria queryByCriteria) {
110         throw new UnsupportedOperationException("Not supported yet.");
111     }
112 
113     @Override
114     public Map<String, String> getAttributes(String groupId) throws RiceIllegalArgumentException {
115         throw new UnsupportedOperationException("Not supported yet.");
116     }
117      
118 
119     @Override
120     public List<Group> getGroups(Collection<String> groupIds) {
121         List<Group> groups = new ArrayList<Group>();
122         for (String groupId : groupIds) {
123             Group info = this.getGroup(groupId);
124             groups.add(info);
125         }
126         return groups;
127     }
128 
129     
130     
131     @Override
132     public List<String> getGroupIdsByPrincipalId(String principalId) {
133         List<String> allGroups = new ArrayList<String>();
134         List<String> directGroups = getDirectGroupIdsByPrincipalId(principalId);
135         allGroups.addAll(directGroups);
136         for (String groupId : directGroups) {
137             List<String> ancestors = this.getParentGroupIds(groupId);
138             allGroups.addAll(ancestors);
139         }
140         return allGroups;
141     }
142 
143     @Override
144     public List<String> getGroupIdsByPrincipalIdAndNamespaceCode(String principalId,
145             String namespaceCode) {
146         throw new UnsupportedOperationException("Not supported yet.");
147     }
148 
149     @Override
150     public Group getGroup(String groupId) {
151         return this.groupCache.get(groupId);
152     }
153 
154     @Override
155     public Group getGroupByNamespaceCodeAndName(String namespaceCode, String groupName) {
156         for (Group group : this.groupCache.values()) {
157             if (namespaceCode.equals(group.getNamespaceCode())) {
158                 if (groupName.equals(group.getName())) {
159                     return group;
160                 }
161             }
162         }
163         return null;
164     }
165 
166   
167 
168     @Override
169     public List<GroupMember> getMembers(List<String> groupIds) {
170         List<GroupMember> infos = new ArrayList<GroupMember>();
171         for (GroupMember info : this.groupMembershipCache.values()) {
172             if (groupIds.contains(info.getGroupId())) {
173                 infos.add(info);
174             }
175         }
176         return infos;
177     }
178 
179     @Override
180     public List<GroupMember> getMembersOfGroup(String groupId) {
181         List<GroupMember> infos = new ArrayList<GroupMember>();
182         for (GroupMember info : this.groupMembershipCache.values()) {
183             if (groupId.equals(info.getGroupId())) {
184                 infos.add(info);
185             }
186         }
187         return infos;
188     }
189 
190     @Override
191     public List<Group> getGroupsByPrincipalId(String principalId) throws RiceIllegalArgumentException {
192         Collection<String> groupIds = this.getGroupIdsByPrincipalId(principalId);
193         List<Group> groups = new ArrayList<Group>();
194         for (String groupId : groupIds) {
195             Group group = this.getGroup(groupId);
196             groups.add(group);
197         }
198         return groups;
199     }
200 
201     @Override
202     public List<Group> getGroupsByPrincipalIdAndNamespaceCode(String principalId,
203             String namespaceCode) {
204         List<Group> groups = new ArrayList<Group>();
205         for (Group group : this.getGroupsByPrincipalId(principalId)) {
206             if (namespaceCode.equals(group.getNamespaceCode())) {
207                 groups.add(group);
208             }
209         }
210         return groups;
211     }
212 
213     @Override
214     public List<String> getMemberGroupIds(String groupId) {
215         List<String> groupIds = new ArrayList<String>();
216         for (GroupMember info : this.groupMembershipCache.values()) {
217             if (groupId.equals(info.getGroupId())) {
218                 if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) {
219                     groupIds.add(info.getMemberId());
220                     groupIds.addAll(this.getMemberGroupIds(info.getMemberId()));
221                 }
222 
223             }
224         }
225         return groupIds;
226     }
227 
228     @Override
229     public List<String> getMemberPrincipalIds(String groupId) {
230         List<String> principalIds = new ArrayList<String>();
231         for (GroupMember info : this.groupMembershipCache.values()) {
232             if (groupId.equals(info.getGroupId())) {
233                 if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) {
234                     principalIds.addAll(this.getMemberPrincipalIds(info.getMemberId()));
235                 } else if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) {
236                     principalIds.add(info.getMemberId());
237                 }
238             }
239         }
240         return principalIds;
241     }
242 
243     @Override
244     public List<String> getParentGroupIds(String groupId) {
245         List<String> groupIds = new ArrayList<String>();
246         for (GroupMember info : this.groupMembershipCache.values()) {
247             if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) {
248                 if (groupId.equals(info.getMemberId())) {
249                     groupIds.add(info.getGroupId());
250                     groupIds.addAll(this.getParentGroupIds(info.getGroupId()));
251                 }
252             }
253         }
254         return groupIds;
255     }
256 
257     @Override
258     public boolean isDirectMemberOfGroup(String principalId, String groupId) {
259         for (GroupMember info : this.groupMembershipCache.values()) {
260             if (groupId.equals(info.getGroupId())) {
261                 if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE)) {
262                     if (principalId.equals(info.getMemberId())) {
263                         return true;
264                     }
265                 }
266             }
267         }
268         return false;
269     }
270 
271     // no longer provided in api
272     public boolean isGroupActive(String groupId) {
273         return this.getGroup(groupId).isActive();
274     }
275 
276     @Override
277     public boolean isGroupMemberOfGroup(String groupMemberId, String groupId) {
278         for (String directChildGroupId : this.getDirectMemberGroupIds(groupId)) {
279             if (directChildGroupId.equals(groupMemberId)) {
280                 return true;
281             }
282             if (this.isGroupMemberOfGroup(groupMemberId, directChildGroupId)) {
283                 return true;
284             }
285         }
286         return false;
287     }
288 
289     @Override
290     public boolean isMemberOfGroup(String principalId, String groupId) {
291         return this.getMemberPrincipalIds(groupId).contains(principalId);
292     }
293 
294     @Override
295     public List<String> findGroupIds(QueryByCriteria queryByCriteria) {
296         throw new UnsupportedOperationException("Not supported yet.");
297     }
298 
299     @Override
300     public GroupQueryResults findGroups(QueryByCriteria queryByCriteria) {
301         throw new UnsupportedOperationException("Not supported yet.");
302     }
303 
304     
305 
306     @Override
307     public boolean addGroupToGroup(String childId, String parentId)
308             throws UnsupportedOperationException {
309         if (childId.equals(parentId)) {
310             throw new IllegalArgumentException("Can't add group to itself.");
311         }
312         if (isGroupMemberOfGroup(parentId, childId)) {
313             throw new IllegalArgumentException("Circular group reference.");
314         }
315         if (this.getDirectMemberGroupIds(parentId).contains(childId)) {
316             return false;
317         }
318         GroupMember.Builder GMB = GroupMember.Builder.create(parentId,
319         		childId,
320                 KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE);
321         GMB.setId(UUID.randomUUID().toString());
322         GroupMember groupMembership = GMB.build();
323         this.groupMembershipCache.put(groupMembership.getMemberId(), groupMembership);
324         return true;
325     }
326 
327     @Override
328     public boolean addPrincipalToGroup(String principalId, String groupId)
329             throws UnsupportedOperationException {
330         if (this.getDirectMemberPrincipalIds(groupId).contains(principalId)) {
331             return false;
332         }
333         GroupMember.Builder GMB = GroupMember.Builder.create(groupId,
334                 principalId,
335                 KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE);
336         GMB.setId(UUID.randomUUID().toString());
337         GroupMember groupMembership = GMB.build();
338         
339         
340         
341         this.groupMembershipCache.put(groupMembership.getMemberId(), groupMembership);
342         return true;
343     }
344 
345     @Override
346     public Group createGroup(Group group)
347             throws UnsupportedOperationException {
348         Group copy = new MockHelper().copy(group);
349         if (copy.getId() != null) {
350             if (this.getGroup(copy.getId()) != null) {
351                 throw new IllegalArgumentException("duplicate id");
352             }
353         } else {
354             Group.Builder.create(group) .setId(UUID.randomUUID().toString());
355         }
356         if (this.getGroupByNamespaceCodeAndName(copy.getNamespaceCode(), copy.getName()) != null) {
357             throw new IllegalArgumentException("name in use");
358         }
359         this.groupCache.put(copy.getId(), copy);
360         return copy;
361 
362     }
363 
364     @Override
365     public void removeAllMembers(String groupId) throws UnsupportedOperationException {
366         for (GroupMember info : this.groupMembershipCache.values()) {
367             if (info.getGroupId().equals(groupId)) {
368                 groupMembershipCache.remove(info.getMemberId());
369             }
370         }
371     }
372 
373     @Override
374     public boolean removeGroupFromGroup(String childId, String parentId) throws UnsupportedOperationException {
375         for (GroupMember info : this.groupMembershipCache.values()) {
376             if (info.getGroupId().equals(parentId)) {
377                 if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) {
378                     if (info.getMemberId().equals(childId)) {
379                         groupMembershipCache.remove(info.getMemberId());
380                         return true;
381                     }
382                 }
383             }
384         }
385         return false;
386     }
387 
388     @Override
389     public boolean removePrincipalFromGroup(String principalId, String groupId)
390             throws UnsupportedOperationException {
391         for (GroupMember info : this.groupMembershipCache.values()) {
392             if (info.getGroupId().equals(groupId)) {
393                 //or MemberType.GROUP.getCode()
394                 if (info.getType().getCode().equals(KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE)) {
395                     if (this.removePrincipalFromGroup(principalId, info.getMemberId())) {
396                         return true;
397                     }
398                     if (info.getMemberId().equals(principalId)) {
399                         groupMembershipCache.remove(info.getMemberId());
400                         return true;
401                     }
402                 }
403             }
404         }
405         return false;
406     }
407 
408     @Override
409     public Group updateGroup(String groupId, Group Group) throws UnsupportedOperationException {
410         if (groupId == null) {
411             throw new IllegalArgumentException("Group id cannot be null");
412         }
413         if (Group.getId() == null) {
414             throw new IllegalArgumentException("New group id cannot be null");
415         }
416         Group existing = this.getGroup(groupId);
417         if (existing == null) {
418             throw new IllegalArgumentException("group id not found");
419         }
420         Group matching = this.getGroupByNamespaceCodeAndName(Group.getNamespaceCode(), Group.getName());
421         if (matching != null) {
422             if (matching != existing) {
423                 throw new IllegalArgumentException("name in use");
424             }
425         }
426         Group copy = new MockHelper().copy(Group);
427         this.groupCache.put(copy.getId(), copy);
428         return copy;
429     }
430     
431     @Override
432     // Redirects to the above method in order to satisfy the new requirements of interface GroupUpdateService
433     public Group updateGroup(Group Group) throws UnsupportedOperationException {
434     	return updateGroup(Group.getId(), Group);
435     }
436 
437     @Override
438     public GroupMember createGroupMember(GroupMember groupMember) throws RiceIllegalArgumentException{
439         throw new UnsupportedOperationException("Not supported yet.");
440     }
441 
442     @Override
443     public GroupMember updateGroupMember(GroupMember groupMember) throws RiceIllegalArgumentException{
444         throw new UnsupportedOperationException("Not supported yet.");
445     }
446 
447 }
448