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