001    /**
002     * Copyright 2005-2015 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.rice.kim.impl.role;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.joda.time.DateTime;
020    import org.kuali.rice.kim.api.group.GroupMember;
021    import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
022    import org.kuali.rice.kim.impl.common.delegate.DelegateTypeBo;
023    import org.kuali.rice.kim.impl.group.GroupMemberBo;
024    
025    import java.sql.Timestamp;
026    import java.util.ArrayList;
027    import java.util.HashSet;
028    import java.util.List;
029    import java.util.Set;
030    import java.util.concurrent.TimeUnit;
031    
032    public class RoleInternalServiceImpl extends RoleServiceBase implements RoleInternalService{
033        @Override
034        public void principalInactivated(String principalId) {
035            if (StringUtils.isBlank(principalId)) {
036                throw new IllegalArgumentException("principalId is null or blank");
037            }
038    
039            long oneDayInMillis = TimeUnit.DAYS.toMillis(1);
040            Timestamp yesterday = new Timestamp(System.currentTimeMillis() - oneDayInMillis);
041    
042            inactivatePrincipalRoleMemberships(principalId, yesterday);
043            inactivatePrincipalGroupMemberships(principalId, yesterday);
044            inactivatePrincipalDelegations(principalId, yesterday);
045            inactivateApplicationRoleMemberships(principalId, yesterday);
046        }
047    
048        @Override
049        public void roleInactivated(String roleId) {
050            if (StringUtils.isBlank(roleId)) {
051                throw new IllegalArgumentException("roleId is null or blank");
052            }
053    
054            long oneDayInMillis = TimeUnit.DAYS.toMillis(1);
055            Timestamp yesterday = new Timestamp(System.currentTimeMillis() - oneDayInMillis);
056    
057            List<String> roleIds = new ArrayList<String>();
058            roleIds.add(roleId);
059            inactivateRoleMemberships(roleIds, yesterday);
060            inactivateRoleDelegations(roleIds, yesterday);
061            inactivateMembershipsForRoleAsMember(roleIds, yesterday);
062        }
063    
064        private void inactivateRoleMemberships(List<String> roleIds, Timestamp yesterday) {
065            List<RoleMemberBo> roleMemberBoList = getStoredRoleMembersForRoleIds(roleIds, null, null);
066            for (RoleMemberBo roleMemberBo : roleMemberBoList) {
067                roleMemberBo.setActiveToDateValue(yesterday);
068            }
069            getBusinessObjectService().save(roleMemberBoList);
070        }
071    
072        private void inactivateRoleDelegations(List<String> roleIds, Timestamp yesterday) {
073            List<DelegateTypeBo> delegations = getStoredDelegationImplsForRoleIds(roleIds);
074            for (DelegateTypeBo delegation : delegations) {
075                delegation.setActive(false);
076                for (DelegateMemberBo delegationMember : delegation.getMembers()) {
077                    delegationMember.setActiveToDateValue(yesterday);
078                }
079            }
080            getBusinessObjectService().save(delegations);
081        }
082    
083        private void inactivateMembershipsForRoleAsMember(List<String> roleIds, Timestamp yesterday) {
084            List<RoleMemberBo> roleMemberBoList = getStoredRoleMembershipsForRoleIdsAsMembers(roleIds, null);
085            for (RoleMemberBo roleMemberBo : roleMemberBoList) {
086                roleMemberBo.setActiveToDateValue(yesterday);
087            }
088            getBusinessObjectService().save(roleMemberBoList);
089        }
090    
091        @Override
092        public void groupInactivated(String groupId) {
093            if (StringUtils.isBlank(groupId)) {
094                throw new IllegalArgumentException("groupId is null or blank");
095            }
096    
097            long oneDayInMillis = TimeUnit.DAYS.toMillis(1);
098            Timestamp yesterday = new Timestamp(System.currentTimeMillis() - oneDayInMillis);
099    
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    }