| 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.exception.RiceIllegalArgumentException; |
| 22 | |
import org.kuali.rice.core.api.mo.common.Attributes; |
| 23 | |
import org.kuali.rice.kim.api.group.Group; |
| 24 | |
import org.kuali.rice.kim.api.group.GroupMember; |
| 25 | |
import org.kuali.rice.kim.api.group.GroupService; |
| 26 | |
import org.kuali.rice.kim.util.KIMPropertyConstants; |
| 27 | |
import org.kuali.rice.kim.util.KimConstants; |
| 28 | |
|
| 29 | |
import java.util.ArrayList; |
| 30 | |
import java.util.Collection; |
| 31 | |
import java.util.Collections; |
| 32 | |
import java.util.HashMap; |
| 33 | |
import java.util.HashSet; |
| 34 | |
import java.util.List; |
| 35 | |
import java.util.Map; |
| 36 | |
import java.util.Set; |
| 37 | |
|
| 38 | |
|
| 39 | 20 | public class GroupServiceImpl extends GroupServiceBase implements GroupService { |
| 40 | |
private GroupDao groupDao; |
| 41 | |
|
| 42 | |
@Override |
| 43 | |
public List<Group> getGroupsForPrincipal(String principalId) throws RiceIllegalArgumentException { |
| 44 | 1 | if ( StringUtils.isEmpty(principalId) ) { |
| 45 | 0 | throw new RiceIllegalArgumentException("principalId is blank"); |
| 46 | |
} |
| 47 | 1 | return getGroupsForPrincipalByNamespace( principalId, null ); |
| 48 | |
} |
| 49 | |
|
| 50 | |
@Override |
| 51 | |
public List<Group> getGroupsForPrincipalByNamespace(String principalId, String namespaceCode) throws RiceIllegalArgumentException { |
| 52 | 4 | if ( StringUtils.isEmpty(principalId) ) { |
| 53 | 0 | throw new RiceIllegalArgumentException("principalId is blank"); |
| 54 | |
} |
| 55 | 4 | Collection<Group> directGroups = getDirectGroupsForPrincipal( principalId, namespaceCode ); |
| 56 | 4 | Set<Group> groups = new HashSet<Group>(); |
| 57 | 4 | groups.addAll(directGroups); |
| 58 | 4 | for ( Group group : directGroups ) { |
| 59 | 4 | groups.add( group ); |
| 60 | 4 | groups.addAll( getParentGroups( group.getId() ) ); |
| 61 | |
} |
| 62 | 4 | return new ArrayList<Group>( groups ); |
| 63 | |
} |
| 64 | |
|
| 65 | |
@Override |
| 66 | |
public List<String> lookupGroupIds(Map<String, String> searchCriteria) { |
| 67 | 1 | List<Group> groupList = this.lookupGroups(searchCriteria); |
| 68 | 1 | List<String> result = new ArrayList<String>(); |
| 69 | |
|
| 70 | 1 | for (Group group : groupList) { |
| 71 | 3 | result.add(group.getId()); |
| 72 | |
} |
| 73 | |
|
| 74 | 1 | return result; |
| 75 | |
} |
| 76 | |
|
| 77 | |
@Override |
| 78 | |
public List<Group> lookupGroups(Map<String, String> searchCriteria) { |
| 79 | 2 | List<GroupBo> groupBos = groupDao.getGroups(searchCriteria); |
| 80 | 2 | List<Group> groups = toGroupList(groupBos); |
| 81 | 2 | if (groups == null) { |
| 82 | 0 | return Collections.emptyList(); |
| 83 | |
} |
| 84 | 2 | return groups; |
| 85 | |
} |
| 86 | |
|
| 87 | |
@Override |
| 88 | |
public boolean isDirectMemberOfGroup(String principalId, String groupId) throws RiceIllegalArgumentException { |
| 89 | 2 | if ( StringUtils.isEmpty(groupId) ) { |
| 90 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
| 91 | |
} |
| 92 | 2 | if ( StringUtils.isEmpty(principalId) ) { |
| 93 | 0 | throw new RiceIllegalArgumentException("principalId is blank"); |
| 94 | |
} |
| 95 | 2 | Map<String,String> criteria = new HashMap<String,String>(); |
| 96 | 2 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId); |
| 97 | 2 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE); |
| 98 | 2 | criteria.put(KIMPropertyConstants.GroupMember.GROUP_ID, groupId); |
| 99 | |
|
| 100 | 2 | Collection<GroupMemberBo> groupMembers = businessObjectService.findMatching(GroupMemberBo.class, criteria); |
| 101 | 2 | for ( GroupMemberBo gm : groupMembers ) { |
| 102 | 1 | if ( gm.isActive() ) { |
| 103 | 1 | return true; |
| 104 | |
} |
| 105 | |
} |
| 106 | 1 | return false; |
| 107 | |
} |
| 108 | |
|
| 109 | |
@Override |
| 110 | |
public List<String> getGroupIdsForPrincipal(String principalId) throws RiceIllegalArgumentException { |
| 111 | 1 | if ( StringUtils.isEmpty(principalId) ) { |
| 112 | 0 | throw new RiceIllegalArgumentException("principalId is blank"); |
| 113 | |
} |
| 114 | 1 | return getGroupIdsForPrincipalByNamespace(principalId, null); |
| 115 | |
} |
| 116 | |
|
| 117 | |
@Override |
| 118 | |
public List<String> getGroupIdsForPrincipalByNamespace(String principalId, String namespaceCode) throws RiceIllegalArgumentException { |
| 119 | 2 | if ( StringUtils.isEmpty(principalId) ) { |
| 120 | 0 | throw new RiceIllegalArgumentException("principalId is blank"); |
| 121 | |
} |
| 122 | 2 | List<String> result = new ArrayList<String>(); |
| 123 | |
|
| 124 | 2 | if (principalId != null) { |
| 125 | 2 | List<Group> groupList = getGroupsForPrincipalByNamespace(principalId, namespaceCode); |
| 126 | |
|
| 127 | 2 | for (Group group : groupList) { |
| 128 | 2 | result.add(group.getId()); |
| 129 | |
} |
| 130 | |
} |
| 131 | |
|
| 132 | 2 | return result; |
| 133 | |
} |
| 134 | |
|
| 135 | |
@Override |
| 136 | |
public List<String> getDirectGroupIdsForPrincipal(String principalId) throws RiceIllegalArgumentException { |
| 137 | 1 | if ( StringUtils.isEmpty(principalId) ) { |
| 138 | 0 | throw new RiceIllegalArgumentException("principalId is blank"); |
| 139 | |
} |
| 140 | 1 | List<String> result = new ArrayList<String>(); |
| 141 | |
|
| 142 | 1 | if (principalId != null) { |
| 143 | 1 | Collection<Group> groupList = getDirectGroupsForPrincipal(principalId); |
| 144 | |
|
| 145 | 1 | for (Group g : groupList) { |
| 146 | 1 | result.add(g.getId()); |
| 147 | |
} |
| 148 | |
} |
| 149 | |
|
| 150 | 1 | return result; |
| 151 | |
} |
| 152 | |
|
| 153 | |
@Override |
| 154 | |
public List<String> getMemberPrincipalIds(String groupId) throws RiceIllegalArgumentException { |
| 155 | 1 | if ( StringUtils.isEmpty(groupId) ) { |
| 156 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
| 157 | |
} |
| 158 | 1 | Set<String> visitedGroupIds = new HashSet<String>(); |
| 159 | 1 | return getMemberPrincipalIdsInternal(groupId, visitedGroupIds); |
| 160 | |
} |
| 161 | |
|
| 162 | |
@Override |
| 163 | |
public List<String> getDirectMemberPrincipalIds(String groupId) throws RiceIllegalArgumentException { |
| 164 | 1 | if ( StringUtils.isEmpty(groupId) ) { |
| 165 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
| 166 | |
} |
| 167 | |
|
| 168 | 1 | return this.getMemberIdsByType(getMembersOfGroup(groupId), KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE); |
| 169 | |
} |
| 170 | |
|
| 171 | |
@Override |
| 172 | |
public List<String> getMemberGroupIds(String groupId) throws RiceIllegalArgumentException { |
| 173 | |
|
| 174 | |
|
| 175 | |
|
| 176 | |
|
| 177 | |
|
| 178 | |
|
| 179 | |
|
| 180 | 1 | if ( StringUtils.isEmpty(groupId) ) { |
| 181 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
| 182 | |
} |
| 183 | 1 | List<GroupBo> groups = getMemberGroupBos( groupId ); |
| 184 | 1 | ArrayList<String> groupIds = new ArrayList<String>( groups.size() ); |
| 185 | 1 | for ( GroupBo group : groups ) { |
| 186 | 1 | if ( group.isActive() ) { |
| 187 | 1 | groupIds.add( group.getId() ); |
| 188 | |
} |
| 189 | |
} |
| 190 | 1 | return groupIds; |
| 191 | |
} |
| 192 | |
|
| 193 | |
|
| 194 | |
protected List<GroupBo> getMemberGroupBos(String groupId) { |
| 195 | 1 | if ( groupId == null ) { |
| 196 | 0 | return Collections.emptyList(); |
| 197 | |
} |
| 198 | 1 | Set<GroupBo> groups = new HashSet<GroupBo>(); |
| 199 | |
|
| 200 | 1 | GroupBo group = getGroupBo(groupId); |
| 201 | 1 | getMemberGroupsInternal(group, groups); |
| 202 | |
|
| 203 | 1 | return new ArrayList<GroupBo>(groups); |
| 204 | |
} |
| 205 | |
|
| 206 | |
protected void getMemberGroupsInternal( GroupBo group, Set<GroupBo> groups ) { |
| 207 | 2 | if ( group == null ) { |
| 208 | 0 | return; |
| 209 | |
} |
| 210 | 2 | List<String> groupIds = group.getMemberGroupIds(); |
| 211 | |
|
| 212 | 2 | for (String id : groupIds) { |
| 213 | 1 | GroupBo memberGroup = getGroupBo(id); |
| 214 | |
|
| 215 | 1 | if ( memberGroup.isActive() && !groups.contains( memberGroup ) ) { |
| 216 | 1 | groups.add(memberGroup); |
| 217 | 1 | getMemberGroupsInternal(memberGroup,groups); |
| 218 | |
} |
| 219 | 1 | } |
| 220 | |
|
| 221 | 2 | } |
| 222 | |
|
| 223 | |
@Override |
| 224 | |
public List<String> getDirectMemberGroupIds(String groupId) { |
| 225 | 1 | if ( groupId == null ) { |
| 226 | 0 | return Collections.emptyList(); |
| 227 | |
} |
| 228 | |
|
| 229 | 1 | return this.getMemberIdsByType(getMembersOfGroup(groupId), KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE); |
| 230 | |
} |
| 231 | |
|
| 232 | |
@Override |
| 233 | |
public List<String> getParentGroupIds(String groupId) throws RiceIllegalArgumentException { |
| 234 | 1 | if ( StringUtils.isEmpty(groupId) ) { |
| 235 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
| 236 | |
} |
| 237 | 1 | List<String> result = new ArrayList<String>(); |
| 238 | 1 | if (groupId != null) { |
| 239 | 1 | List<Group> groupList = getParentGroups(groupId); |
| 240 | |
|
| 241 | 1 | for (Group group : groupList) { |
| 242 | 1 | result.add(group.getId()); |
| 243 | |
} |
| 244 | |
} |
| 245 | |
|
| 246 | 1 | return result; |
| 247 | |
} |
| 248 | |
|
| 249 | |
@Override |
| 250 | |
public List<String> getDirectParentGroupIds(String groupId) throws RiceIllegalArgumentException { |
| 251 | 1 | if ( StringUtils.isEmpty(groupId) ) { |
| 252 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
| 253 | |
} |
| 254 | 1 | List<String> result = new ArrayList<String>(); |
| 255 | 1 | if (groupId != null) { |
| 256 | 1 | Map<String,Group> groupList = getDirectParentGroups(groupId); |
| 257 | 1 | result.addAll(groupList.keySet()); |
| 258 | |
} |
| 259 | |
|
| 260 | 1 | return result; |
| 261 | |
} |
| 262 | |
|
| 263 | |
@Override |
| 264 | |
public Attributes getAttributes(String groupId) throws RiceIllegalArgumentException { |
| 265 | 0 | if ( StringUtils.isEmpty(groupId) ) { |
| 266 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
| 267 | |
} |
| 268 | |
|
| 269 | 0 | if (groupId == null) { |
| 270 | 0 | return Attributes.empty(); |
| 271 | |
} |
| 272 | |
|
| 273 | 0 | Group group = getGroup(groupId); |
| 274 | 0 | if (group != null) { |
| 275 | 0 | return group.getAttributes(); |
| 276 | |
} |
| 277 | 0 | return null; |
| 278 | |
} |
| 279 | |
|
| 280 | |
@Override |
| 281 | |
public List<GroupMember> getMembers(List<String> groupIds) { |
| 282 | 1 | if (CollectionUtils.isEmpty(groupIds)) { |
| 283 | 0 | throw new RiceIllegalArgumentException("groupIds is empty"); |
| 284 | |
} |
| 285 | |
|
| 286 | |
|
| 287 | 1 | List<GroupMember> groupMembers = new ArrayList<GroupMember>(); |
| 288 | 1 | for (String groupId : groupIds) { |
| 289 | 2 | groupMembers.addAll(getMembersOfGroup(groupId)); |
| 290 | |
} |
| 291 | 1 | return groupMembers; |
| 292 | |
} |
| 293 | |
|
| 294 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
|
| 301 | |
|
| 302 | |
|
| 303 | |
|
| 304 | |
|
| 305 | |
protected List<Group> getParentGroups(String groupId) throws RiceIllegalArgumentException { |
| 306 | 5 | if ( StringUtils.isEmpty(groupId) ) { |
| 307 | 0 | throw new RiceIllegalArgumentException("groupId is blank"); |
| 308 | |
} |
| 309 | 5 | Set<Group> groups = new HashSet<Group>(); |
| 310 | 5 | getParentGroupsInternal( groupId, groups ); |
| 311 | 5 | return new ArrayList<Group>( groups ); |
| 312 | |
} |
| 313 | |
|
| 314 | |
protected List<String> getMemberPrincipalIdsInternal(String groupId, Set<String> visitedGroupIds) { |
| 315 | 2 | if ( groupId == null ) { |
| 316 | 0 | return Collections.emptyList(); |
| 317 | |
} |
| 318 | 2 | Set<String> ids = new HashSet<String>(); |
| 319 | 2 | GroupBo group = getGroupBo(groupId); |
| 320 | 2 | if ( group == null || !group.isActive()) { |
| 321 | 0 | return Collections.emptyList(); |
| 322 | |
} |
| 323 | |
|
| 324 | |
|
| 325 | |
|
| 326 | 2 | ids.addAll( group.getMemberPrincipalIds()); |
| 327 | 2 | visitedGroupIds.add(group.getId()); |
| 328 | |
|
| 329 | 2 | for (String memberGroupId : group.getMemberGroupIds()) { |
| 330 | 1 | if (!visitedGroupIds.contains(memberGroupId)){ |
| 331 | 1 | ids.addAll(getMemberPrincipalIdsInternal(memberGroupId, visitedGroupIds)); |
| 332 | |
} |
| 333 | |
} |
| 334 | |
|
| 335 | 2 | return new ArrayList<String>(ids); |
| 336 | |
} |
| 337 | |
|
| 338 | |
protected Collection<Group> getDirectGroupsForPrincipal( String principalId ) { |
| 339 | 1 | return getDirectGroupsForPrincipal( principalId, null ); |
| 340 | |
} |
| 341 | |
|
| 342 | |
@SuppressWarnings("unchecked") |
| 343 | |
protected Collection<Group> getDirectGroupsForPrincipal( String principalId, String namespaceCode ) { |
| 344 | 5 | if ( principalId == null ) { |
| 345 | 0 | return Collections.emptyList(); |
| 346 | |
} |
| 347 | 5 | Map<String,Object> criteria = new HashMap<String,Object>(); |
| 348 | 5 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_ID, principalId); |
| 349 | 5 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE); |
| 350 | 5 | Collection<GroupMemberBo> groupMembers = businessObjectService.findMatching(GroupMemberBo.class, criteria); |
| 351 | 5 | Set<String> groupIds = new HashSet<String>( groupMembers.size() ); |
| 352 | |
|
| 353 | 5 | for ( GroupMemberBo gm : groupMembers ) { |
| 354 | 5 | if ( gm.isActive() ) { |
| 355 | 5 | groupIds.add( gm.getGroupId() ); |
| 356 | |
} |
| 357 | |
} |
| 358 | |
|
| 359 | 5 | Map<String,Group> groups = getGroups(groupIds); |
| 360 | 5 | List<Group> result = new ArrayList<Group>( groups.size() ); |
| 361 | |
|
| 362 | 5 | for ( Group group : groups.values() ) { |
| 363 | 5 | if ( group.isActive() ) { |
| 364 | 5 | if ( StringUtils.isBlank(namespaceCode) || StringUtils.equals(namespaceCode, group.getNamespaceCode() ) ) { |
| 365 | 5 | result.add(group); |
| 366 | |
} |
| 367 | |
} |
| 368 | |
} |
| 369 | 5 | return result; |
| 370 | |
} |
| 371 | |
|
| 372 | |
public void setGroupDao(final GroupDao groupDao) { |
| 373 | 2 | this.groupDao = groupDao; |
| 374 | 2 | } |
| 375 | |
} |