View Javadoc

1   /**
2    * Copyright 2005-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.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 org.apache.commons.lang.StringUtils;
19  import org.joda.time.DateTime;
20  import org.kuali.rice.kim.api.group.GroupMember;
21  import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
22  import org.kuali.rice.kim.impl.common.delegate.DelegateTypeBo;
23  import org.kuali.rice.kim.impl.group.GroupMemberBo;
24  
25  import java.sql.Timestamp;
26  import java.util.ArrayList;
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Set;
30  import java.util.concurrent.TimeUnit;
31  
32  public class RoleInternalServiceImpl extends RoleServiceBase implements RoleInternalService{
33      @Override
34      public void principalInactivated(String principalId) {
35          if (StringUtils.isBlank(principalId)) {
36              throw new IllegalArgumentException("principalId is null or blank");
37          }
38  
39          long oneDayInMillis = TimeUnit.DAYS.toMillis(1);
40          Timestamp yesterday = new Timestamp(System.currentTimeMillis() - oneDayInMillis);
41  
42          inactivatePrincipalRoleMemberships(principalId, yesterday);
43          inactivatePrincipalGroupMemberships(principalId, yesterday);
44          inactivatePrincipalDelegations(principalId, yesterday);
45          inactivateApplicationRoleMemberships(principalId, yesterday);
46      }
47  
48      @Override
49      public void roleInactivated(String roleId) {
50          if (StringUtils.isBlank(roleId)) {
51              throw new IllegalArgumentException("roleId is null or blank");
52          }
53  
54          long oneDayInMillis = TimeUnit.DAYS.toMillis(1);
55          Timestamp yesterday = new Timestamp(System.currentTimeMillis() - oneDayInMillis);
56  
57          List<String> roleIds = new ArrayList<String>();
58          roleIds.add(roleId);
59          inactivateRoleMemberships(roleIds, yesterday);
60          inactivateRoleDelegations(roleIds, yesterday);
61          inactivateMembershipsForRoleAsMember(roleIds, yesterday);
62      }
63  
64      private void inactivateRoleMemberships(List<String> roleIds, Timestamp yesterday) {
65          List<RoleMemberBo> roleMemberBoList = getStoredRoleMembersForRoleIds(roleIds, null, null);
66          for (RoleMemberBo roleMemberBo : roleMemberBoList) {
67              roleMemberBo.setActiveToDateValue(yesterday);
68          }
69          getBusinessObjectService().save(roleMemberBoList);
70      }
71  
72      private void inactivateRoleDelegations(List<String> roleIds, Timestamp yesterday) {
73          List<DelegateTypeBo> delegations = getStoredDelegationImplsForRoleIds(roleIds);
74          for (DelegateTypeBo delegation : delegations) {
75              delegation.setActive(false);
76              for (DelegateMemberBo delegationMember : delegation.getMembers()) {
77                  delegationMember.setActiveToDateValue(yesterday);
78              }
79          }
80          getBusinessObjectService().save(delegations);
81      }
82  
83      private void inactivateMembershipsForRoleAsMember(List<String> roleIds, Timestamp yesterday) {
84          List<RoleMemberBo> roleMemberBoList = getStoredRoleMembershipsForRoleIdsAsMembers(roleIds, null);
85          for (RoleMemberBo roleMemberBo : roleMemberBoList) {
86              roleMemberBo.setActiveToDateValue(yesterday);
87          }
88          getBusinessObjectService().save(roleMemberBoList);
89      }
90  
91      @Override
92      public void groupInactivated(String groupId) {
93          if (StringUtils.isBlank(groupId)) {
94              throw new IllegalArgumentException("groupId is null or blank");
95          }
96  
97          long oneDayInMillis = TimeUnit.DAYS.toMillis(1);
98          Timestamp yesterday = new Timestamp(System.currentTimeMillis() - oneDayInMillis);
99  
100         List<String> groupIds = new ArrayList<String>();
101         groupIds.add(groupId);
102         inactivatePrincipalGroupMemberships(groupIds, yesterday);
103         inactivateGroupRoleMemberships(groupIds, yesterday);
104     }
105 
106     protected void inactivateApplicationRoleMemberships(String principalId, Timestamp yesterday) {
107 
108     }
109 
110     protected void inactivatePrincipalRoleMemberships(String principalId, Timestamp yesterday) {
111         // go through all roles and post-date them
112         List<RoleMemberBo> roleMembers = getStoredRolePrincipalsForPrincipalIdAndRoleIds(null, principalId, null);
113         Set<String> roleIds = new HashSet<String>(roleMembers.size());
114         for (RoleMemberBo roleMemberBo : roleMembers) {
115             roleMemberBo.setActiveToDateValue(yesterday);
116             roleIds.add(roleMemberBo.getRoleId()); // add to the set of IDs
117         }
118         getBusinessObjectService().save(roleMembers);
119     }
120 
121     protected void inactivateGroupRoleMemberships(List<String> groupIds, Timestamp yesterday) {
122         List<RoleMemberBo> roleMemberBosOfGroupType = getStoredRoleGroupsForGroupIdsAndRoleIds(null, groupIds, null);
123         for (RoleMemberBo roleMemberbo : roleMemberBosOfGroupType) {
124             roleMemberbo.setActiveToDateValue(yesterday);
125         }
126         getBusinessObjectService().save(roleMemberBosOfGroupType);
127     }
128 
129     protected void inactivatePrincipalGroupMemberships(String principalId, Timestamp yesterday) {
130         List<GroupMember> groupMembers = getRoleDao().getGroupPrincipalsForPrincipalIdAndGroupIds(null, principalId);
131         List<GroupMemberBo> groupMemberBoList = new ArrayList<GroupMemberBo>(groupMembers.size());
132         for (GroupMember gm : groupMembers) {
133             GroupMember.Builder builder = GroupMember.Builder.create(gm);
134             builder.setActiveToDate(new DateTime(yesterday.getTime()));
135             groupMemberBoList.add(GroupMemberBo.from(builder.build()));
136         }
137         getBusinessObjectService().save(groupMemberBoList);
138     }
139 
140     protected void inactivatePrincipalGroupMemberships(List<String> groupIds, Timestamp yesterday) {
141         List<GroupMember> groupMembers = getRoleDao().getGroupMembers(groupIds);
142         List<GroupMemberBo> groupMemberBoList = new ArrayList<GroupMemberBo>(groupMembers.size());
143         for (GroupMember groupMember : groupMembers) {
144             GroupMember.Builder builder = GroupMember.Builder.create(groupMember);
145             builder.setActiveToDate(new DateTime(yesterday.getTime()));
146             groupMemberBoList.add(GroupMemberBo.from(builder.build()));
147         }
148         getBusinessObjectService().save(groupMemberBoList);
149     }
150 
151     protected void inactivatePrincipalDelegations(String principalId, Timestamp yesterday) {
152         List<DelegateMemberBo> delegationMembers = getStoredDelegationPrincipalsForPrincipalIdAndDelegationIds(null,
153                 principalId);
154         for (DelegateMemberBo delegateMemberBo : delegationMembers) {
155             delegateMemberBo.setActiveToDateValue(yesterday);
156         }
157         getBusinessObjectService().save(delegationMembers);
158     }
159 }