1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kim.impl.group; |
18 | |
|
19 | |
|
20 | |
import org.apache.commons.collections.CollectionUtils; |
21 | |
import org.apache.commons.lang.StringUtils; |
22 | |
import org.kuali.rice.core.api.criteria.CriteriaLookupService; |
23 | |
import org.kuali.rice.core.api.criteria.GenericQueryResults; |
24 | |
import org.kuali.rice.core.api.criteria.LookupCustomizer; |
25 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
26 | |
import org.kuali.rice.core.api.exception.RiceIllegalArgumentException; |
27 | |
import org.kuali.rice.kim.api.group.Group; |
28 | |
import org.kuali.rice.kim.api.group.GroupMember; |
29 | |
import org.kuali.rice.kim.api.group.GroupMemberQueryResults; |
30 | |
import org.kuali.rice.kim.api.group.GroupQueryResults; |
31 | |
import org.kuali.rice.kim.impl.common.attribute.AttributeTransform; |
32 | |
import org.kuali.rice.kim.util.KIMPropertyConstants; |
33 | |
import org.kuali.rice.kim.util.KimConstants; |
34 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
35 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.*; |
36 | |
|
37 | |
import java.util.ArrayList; |
38 | |
import java.util.Collection; |
39 | |
import java.util.Collections; |
40 | |
import java.util.HashMap; |
41 | |
import java.util.HashSet; |
42 | |
import java.util.List; |
43 | |
import java.util.Map; |
44 | |
import java.util.Set; |
45 | |
|
46 | 20 | public abstract class GroupServiceBase { |
47 | |
protected BusinessObjectService businessObjectService; |
48 | |
private CriteriaLookupService criteriaLookupService; |
49 | |
|
50 | |
|
51 | |
public Group getGroup(String groupId) { |
52 | 7 | return GroupBo.to(getGroupBo(groupId)); |
53 | |
} |
54 | |
|
55 | |
protected GroupBo getGroupBo(String groupId) { |
56 | 11 | if ( StringUtils.isEmpty(groupId) ) { |
57 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
58 | |
} |
59 | 11 | return (GroupBo)businessObjectService.findBySinglePrimaryKey(GroupBo.class, groupId); |
60 | |
|
61 | |
} |
62 | |
|
63 | |
|
64 | |
public boolean isGroupMemberOfGroup(String groupMemberId, String groupId) { |
65 | 0 | if ( StringUtils.isEmpty(groupId) || StringUtils.isEmpty(groupMemberId) ) { |
66 | 0 | throw new RiceIllegalArgumentException("groupMemberId or groupId is blank"); |
67 | |
} |
68 | |
|
69 | 0 | Set<String> visitedGroupIds = new HashSet<String>(); |
70 | 0 | return isMemberOfGroupInternal(groupMemberId, groupId, visitedGroupIds, KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE); |
71 | |
} |
72 | |
|
73 | |
|
74 | |
public boolean isMemberOfGroup(String principalId, String groupId) { |
75 | 2 | if ( principalId == null || groupId == null ) { |
76 | 0 | return false; |
77 | |
} |
78 | 2 | Set<String> visitedGroupIds = new HashSet<String>(); |
79 | 2 | return isMemberOfGroupInternal(principalId, groupId, visitedGroupIds, KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE); |
80 | |
} |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
public Map<String, Group> getGroups(Collection<String> groupIds) { |
87 | 12 | Map<String, Group> result = new HashMap<String, Group>(); |
88 | 12 | if (CollectionUtils.isEmpty(groupIds)) { |
89 | 5 | return result; |
90 | |
} |
91 | 7 | final QueryByCriteria.Builder builder = QueryByCriteria.Builder.create(); |
92 | 7 | builder.setPredicates(and(in("id", groupIds.toArray()), equal("active", "Y"))); |
93 | 7 | GroupQueryResults qr = findGroups(builder.build()); |
94 | 7 | for (Group group : qr.getResults()) { |
95 | 7 | result.put(group.getId(), group); |
96 | |
} |
97 | 7 | return result; |
98 | |
} |
99 | |
|
100 | |
|
101 | |
public Group getGroupByName(String namespaceCode, String groupName) { |
102 | 4 | if ( namespaceCode == null || groupName == null ) { |
103 | 0 | return null; |
104 | |
} |
105 | 4 | Map<String,String> criteria = new HashMap<String,String>(); |
106 | 4 | criteria.put(KimConstants.UniqueKeyConstants.NAMESPACE_CODE, namespaceCode); |
107 | 4 | criteria.put(KimConstants.UniqueKeyConstants.GROUP_NAME, groupName); |
108 | 4 | Collection<GroupBo> groups = businessObjectService.findMatching(GroupBo.class, criteria); |
109 | 4 | if ( groups.size() > 0 ) { |
110 | 3 | return GroupBo.to(groups.iterator().next()); |
111 | |
} |
112 | 1 | return null; |
113 | |
} |
114 | |
|
115 | |
public GroupQueryResults findGroups(final QueryByCriteria queryByCriteria) { |
116 | 9 | if (queryByCriteria == null) { |
117 | 0 | throw new RiceIllegalArgumentException("queryByCriteria is null"); |
118 | |
} |
119 | |
|
120 | 9 | LookupCustomizer.Builder<GroupBo> lc = LookupCustomizer.Builder.create(); |
121 | 9 | lc.setPredicateTransform(AttributeTransform.getInstance()); |
122 | |
|
123 | 9 | GenericQueryResults<GroupBo> results = criteriaLookupService.lookup(GroupBo.class, queryByCriteria, lc.build()); |
124 | |
|
125 | 9 | GroupQueryResults.Builder builder = GroupQueryResults.Builder.create(); |
126 | 9 | builder.setMoreResultsAvailable(results.isMoreResultsAvailable()); |
127 | 9 | builder.setTotalRowCount(results.getTotalRowCount()); |
128 | |
|
129 | 9 | final List<Group.Builder> ims = new ArrayList<Group.Builder>(); |
130 | 9 | for (GroupBo bo : results.getResults()) { |
131 | 13 | ims.add(Group.Builder.create(bo)); |
132 | |
} |
133 | |
|
134 | 9 | builder.setResults(ims); |
135 | 9 | return builder.build(); |
136 | |
} |
137 | |
|
138 | |
public GroupMemberQueryResults findGroupMembers(final QueryByCriteria queryByCriteria) { |
139 | 0 | if (queryByCriteria == null) { |
140 | 0 | throw new RiceIllegalArgumentException("queryByCriteria is null"); |
141 | |
} |
142 | |
|
143 | 0 | GenericQueryResults<GroupMemberBo> results = criteriaLookupService.lookup(GroupMemberBo.class, queryByCriteria); |
144 | |
|
145 | 0 | GroupMemberQueryResults.Builder builder = GroupMemberQueryResults.Builder.create(); |
146 | 0 | builder.setMoreResultsAvailable(results.isMoreResultsAvailable()); |
147 | 0 | builder.setTotalRowCount(results.getTotalRowCount()); |
148 | |
|
149 | 0 | final List<GroupMember.Builder> ims = new ArrayList<GroupMember.Builder>(); |
150 | 0 | for (GroupMemberBo bo : results.getResults()) { |
151 | 0 | ims.add(GroupMember.Builder.create(bo)); |
152 | |
} |
153 | |
|
154 | 0 | builder.setResults(ims); |
155 | 0 | return builder.build(); |
156 | |
} |
157 | |
|
158 | |
|
159 | |
public boolean isMemberOfGroupInternal(String memberId, String groupId, Set<String> visitedGroupIds, String memberType) { |
160 | 3 | if ( memberId == null || groupId == null ) { |
161 | 0 | return false; |
162 | |
} |
163 | |
|
164 | |
|
165 | 3 | Group group = getGroup(groupId); |
166 | 3 | if ( group == null || !group.isActive() ) { |
167 | 0 | return false; |
168 | |
} |
169 | |
|
170 | 3 | List<GroupMember> members = getMembersOfGroup(group.getId()); |
171 | |
|
172 | 3 | for (String groupMemberId : getMemberIdsByType(members, memberType)) { |
173 | 3 | if (groupMemberId.equals(memberId)) { |
174 | 1 | return true; |
175 | |
} |
176 | |
} |
177 | |
|
178 | |
|
179 | 2 | for ( String memberGroupId : getMemberIdsByType(members, KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE) ) { |
180 | 1 | if (!visitedGroupIds.contains(memberGroupId)){ |
181 | 1 | visitedGroupIds.add(memberGroupId); |
182 | 1 | if ( isMemberOfGroupInternal( memberId, memberGroupId, visitedGroupIds, memberType ) ) { |
183 | 0 | return true; |
184 | |
} |
185 | |
} |
186 | |
} |
187 | |
|
188 | |
|
189 | 2 | return false; |
190 | |
} |
191 | |
|
192 | |
protected void getParentGroupsInternal( String groupId, Set<Group> groups ) { |
193 | 6 | Map<String,Group> parentGroups = getDirectParentGroups( groupId ); |
194 | 6 | for ( Group group : parentGroups.values() ) { |
195 | 1 | if ( !groups.contains( group ) ) { |
196 | 1 | groups.add( group ); |
197 | 1 | getParentGroupsInternal( group.getId(), groups ); |
198 | |
} |
199 | |
} |
200 | 6 | } |
201 | |
|
202 | |
protected Map<String,Group> getDirectParentGroups(String groupId) { |
203 | 7 | if ( groupId == null ) { |
204 | 0 | return Collections.emptyMap(); |
205 | |
} |
206 | 7 | Map<String,String> criteria = new HashMap<String,String>(); |
207 | 7 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_ID, groupId); |
208 | 7 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE); |
209 | |
|
210 | 7 | List<GroupMemberBo> groupMembers = (List<GroupMemberBo>)businessObjectService.findMatching(GroupMemberBo.class, criteria); |
211 | 7 | Set<String> matchingGroupIds = new HashSet<String>(); |
212 | |
|
213 | 7 | for ( GroupMemberBo gm : groupMembers ) { |
214 | 2 | if ( gm.isActive() ) { |
215 | 2 | matchingGroupIds.add(gm.getGroupId()); |
216 | |
} |
217 | |
} |
218 | 7 | return getGroups(matchingGroupIds); |
219 | |
} |
220 | |
|
221 | |
public List<GroupMember> getMembersOfGroup(String groupId) { |
222 | 7 | if (groupId == null) { |
223 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
224 | |
} |
225 | 7 | Map<String,String> criteria = new HashMap<String,String>(); |
226 | 7 | criteria.put(KIMPropertyConstants.GroupMember.GROUP_ID, groupId); |
227 | |
|
228 | 7 | Collection<GroupMemberBo> groupMembersBos = businessObjectService.findMatching(GroupMemberBo.class, criteria); |
229 | 7 | List<GroupMember> groupMembers = new ArrayList<GroupMember>(); |
230 | 7 | for (GroupMemberBo groupBo : groupMembersBos) { |
231 | 16 | if (groupBo.isActive()){ |
232 | 16 | groupMembers.add(GroupMemberBo.to(groupBo)); |
233 | |
} |
234 | |
} |
235 | 7 | return groupMembers; |
236 | |
} |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
public void setBusinessObjectService(final BusinessObjectService businessObjectService) { |
244 | 18 | this.businessObjectService = businessObjectService; |
245 | 18 | } |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
public void setCriteriaLookupService(final CriteriaLookupService criteriaLookupService) { |
253 | 11 | this.criteriaLookupService = criteriaLookupService; |
254 | 11 | } |
255 | |
|
256 | |
protected List<Group> toGroupList(List<GroupBo> groupBos) { |
257 | 0 | if (groupBos == null) { |
258 | 0 | return null; |
259 | |
} |
260 | 0 | List<Group> groups = new ArrayList<Group>(); |
261 | 0 | for (GroupBo bo : groupBos) { |
262 | 0 | groups.add(GroupBo.to(bo)); |
263 | |
} |
264 | 0 | return groups; |
265 | |
} |
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
protected List<GroupMember> getMembersByType(Collection<GroupMember> members, String memberType) { |
280 | 0 | List<GroupMember> membersByType = new ArrayList<GroupMember>(); |
281 | 0 | if (members != null) { |
282 | 0 | for (GroupMember member : members) { |
283 | 0 | if (member.getTypeCode().equals(memberType)) { |
284 | 0 | membersByType.add(member); |
285 | |
} |
286 | |
} |
287 | |
} |
288 | 0 | return membersByType; |
289 | |
} |
290 | |
|
291 | |
protected List<String> getMemberIdsByType(Collection<GroupMember> members, String memberType) { |
292 | 7 | List<String> membersIds = new ArrayList<String>(); |
293 | 7 | if (members != null) { |
294 | 7 | for (GroupMember member : members) { |
295 | 13 | if (member.getTypeCode().equals(memberType)) { |
296 | 7 | membersIds.add(member.getMemberId()); |
297 | |
} |
298 | |
} |
299 | |
} |
300 | 7 | return membersIds; |
301 | |
} |
302 | |
} |