View Javadoc
1   /**
2    * Copyright 2005-2014 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.opensource.org/licenses/ecl2.php
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.rice.kim.impl.role;
17  
18  import java.sql.Timestamp;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  import java.util.concurrent.TimeUnit;
25  
26  import org.apache.commons.lang.StringUtils;
27  import org.joda.time.DateTime;
28  import org.kuali.rice.core.api.membership.MemberType;
29  import org.kuali.rice.kim.api.group.GroupMember;
30  import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
31  import org.kuali.rice.kim.impl.common.delegate.DelegateTypeBo;
32  import org.kuali.rice.kim.impl.group.GroupMemberBo;
33  
34  public class RoleInternalServiceImpl extends RoleServiceBase implements RoleInternalService{
35      @Override
36      public void principalInactivated(String principalId) {
37          if (StringUtils.isBlank(principalId)) {
38              throw new IllegalArgumentException("principalId is null or blank");
39          }
40  
41          long oneDayInMillis = TimeUnit.DAYS.toMillis(1);
42          Timestamp yesterday = new Timestamp(System.currentTimeMillis() - oneDayInMillis);
43  
44          inactivatePrincipalRoleMemberships(principalId, yesterday);
45          inactivatePrincipalGroupMemberships(principalId, yesterday);
46          inactivatePrincipalDelegations(principalId, yesterday);
47          inactivateApplicationRoleMemberships(principalId, yesterday);
48      }
49  
50      @Override
51      public void roleInactivated(String roleId) {
52          if (StringUtils.isBlank(roleId)) {
53              throw new IllegalArgumentException("roleId is null or blank");
54          }
55  
56          long oneDayInMillis = TimeUnit.DAYS.toMillis(1);
57          Timestamp yesterday = new Timestamp(System.currentTimeMillis() - oneDayInMillis);
58  
59          List<String> roleIds = new ArrayList<String>();
60          roleIds.add(roleId);
61          inactivateRoleMemberships(roleIds, yesterday);
62          inactivateRoleDelegations(roleIds, yesterday);
63          inactivateMembershipsForRoleAsMember(roleIds, yesterday);
64      }
65  
66      private void inactivateRoleMemberships(List<String> roleIds, Timestamp yesterday) {
67          List<RoleMemberBo> roleMemberBoList = getStoredRoleMembersForRoleIds(roleIds, null, null);
68          for (RoleMemberBo roleMemberBo : roleMemberBoList) {
69              roleMemberBo.setActiveToDateValue(yesterday);
70              getDataObjectService().save(roleMemberBo);
71          }
72      }
73  
74      private void inactivateRoleDelegations(List<String> roleIds, Timestamp yesterday) {
75          List<DelegateTypeBo> delegations = getStoredDelegationImplsForRoleIds(roleIds);
76          for (DelegateTypeBo delegation : delegations) {
77              delegation.setActive(false);
78              for (DelegateMemberBo delegationMember : delegation.getMembers()) {
79                  delegationMember.setActiveToDateValue(yesterday);
80                  getDataObjectService().save(delegationMember);
81              }
82          }
83      }
84  
85      private void inactivateMembershipsForRoleAsMember(List<String> roleIds, Timestamp yesterday) {
86          List<RoleMemberBo> roleMemberBoList = getStoredRoleMembershipsForRoleIdsAsMembers(roleIds, null);
87          for (RoleMemberBo roleMemberBo : roleMemberBoList) {
88              roleMemberBo.setActiveToDateValue(yesterday);
89              getDataObjectService().save(roleMemberBo);
90          }
91      }
92  
93      @Override
94      public void groupInactivated(String groupId) {
95          if (StringUtils.isBlank(groupId)) {
96              throw new IllegalArgumentException("groupId is null or blank");
97          }
98  
99          long oneDayInMillis = TimeUnit.DAYS.toMillis(1);
100         Timestamp yesterday = new Timestamp(System.currentTimeMillis() - oneDayInMillis);
101 
102         List<String> groupIds = new ArrayList<String>();
103         groupIds.add(groupId);
104         inactivatePrincipalGroupMemberships(groupIds, yesterday);
105         inactivateGroupRoleMemberships(groupIds, yesterday);
106     }
107 
108     protected void inactivateApplicationRoleMemberships(String principalId, Timestamp yesterday) {
109 
110     }
111 
112     protected void inactivatePrincipalRoleMemberships(String principalId, Timestamp yesterday) {
113         // go through all roles and post-date them
114         List<RoleMemberBo> roleMembers = getStoredRolePrincipalsForPrincipalIdAndRoleIds(null, principalId, null);
115         Set<String> roleIds = new HashSet<String>(roleMembers.size());
116         for (RoleMemberBo roleMemberBo : roleMembers) {
117             roleMemberBo.setActiveToDateValue(yesterday);
118             roleIds.add(roleMemberBo.getRoleId()); // add to the set of IDs
119             getDataObjectService().save(roleMemberBo);
120         }
121     }
122 
123     protected void inactivateGroupRoleMemberships(List<String> groupIds, Timestamp yesterday) {
124         List<RoleMemberBo> roleMemberBosOfGroupType = getStoredRoleGroupsForGroupIdsAndRoleIds(null, groupIds, null);
125         for (RoleMemberBo roleMemberBo : roleMemberBosOfGroupType) {
126             roleMemberBo.setActiveToDateValue(yesterday);
127             getDataObjectService().save(roleMemberBo);
128         }
129     }
130 
131     protected void inactivatePrincipalGroupMemberships(String principalId, Timestamp yesterday) {
132         if ( StringUtils.isBlank(principalId) ) {
133             return;
134         }
135         // get all the groups the person is in
136         List<String> groupIds = getGroupService().getGroupIdsByPrincipalId(principalId);
137         if (groupIds.isEmpty() ) {
138             return;
139         }
140         // get all the member records for those groups
141         Collection<GroupMember> groupMembers = getGroupService().getMembers(groupIds);
142         List<GroupMember> groupPrincipals = new ArrayList<GroupMember>(groupMembers.size());
143         for (GroupMember groupMembershipInfo : groupMembers) {
144             if (MemberType.PRINCIPAL.equals(groupMembershipInfo.getType())
145                     && StringUtils.equals(principalId, groupMembershipInfo.getMemberId())
146                     && groupMembershipInfo.isActive(new DateTime())) {
147                 groupPrincipals.add(groupMembershipInfo);
148                 // FIXME: Is there a reason we are not calling the responsible group service?
149                 //getGroupService().removePrincipalFromGroup(groupMembershipInfo.getMemberId(), groupMembershipInfo.getGroupId());
150             }
151         }
152         // FIXME: Is there a reason we are doing this directly and *not* calling the group service???
153         for (GroupMember gm : groupPrincipals) {
154             GroupMember.Builder builder = GroupMember.Builder.create(gm);
155             builder.setActiveToDate(new DateTime(yesterday.getTime()));
156             getDataObjectService().save(GroupMemberBo.from(builder.build()));
157         }
158     }
159 
160     protected void inactivatePrincipalGroupMemberships(List<String> groupIds, Timestamp yesterday) {
161         if (groupIds == null || groupIds.isEmpty() ) {
162             return;
163         }
164         Collection<GroupMember> groupMemberships = getGroupService().getMembers(groupIds);
165         if ( groupMemberships.isEmpty() ) {
166             return;
167         }
168         List<GroupMember> groupMembers = new ArrayList<GroupMember>();
169         for (GroupMember groupMembershipInfo : groupMemberships) {
170             if (MemberType.GROUP.equals(groupMembershipInfo.getType())
171                     && groupMembershipInfo.isActive(new DateTime())) {
172                 groupMembers.add(groupMembershipInfo);
173             }
174         }
175         // FIXME: Is there a reason we are doing this directly and *not* calling the group service???
176         for (GroupMember groupMember : groupMembers) {
177             GroupMember.Builder builder = GroupMember.Builder.create(groupMember);
178             builder.setActiveToDate(new DateTime(yesterday.getTime()));
179             getDataObjectService().save(GroupMemberBo.from(builder.build()));
180         }
181     }
182 
183     protected void inactivatePrincipalDelegations(String principalId, Timestamp yesterday) {
184         List<DelegateMemberBo> delegationMembers = getStoredDelegationPrincipalsForPrincipalIdAndDelegationIds(null,
185                 principalId);
186         for (DelegateMemberBo delegateMemberBo : delegationMembers) {
187             delegateMemberBo.setActiveToDateValue(yesterday);
188             getDataObjectService().save(delegateMemberBo);
189         }
190     }
191 }