001    /**
002     * Copyright 2005-2013 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.responsibility;
017    
018    import org.kuali.rice.kew.api.KewApiServiceLocator;
019    import org.kuali.rice.kim.api.KimConstants;
020    import org.kuali.rice.kim.api.responsibility.Responsibility;
021    import org.kuali.rice.kim.api.responsibility.ResponsibilityService;
022    import org.kuali.rice.kim.api.role.RoleResponsibility;
023    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
024    import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
025    import org.kuali.rice.kim.impl.role.RoleMemberBo;
026    import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
027    import org.kuali.rice.krad.service.BusinessObjectService;
028    import org.kuali.rice.krad.service.KRADServiceLocator;
029    
030    import java.util.ArrayList;
031    import java.util.Collections;
032    import java.util.HashSet;
033    import java.util.List;
034    import java.util.Map;
035    import java.util.Set;
036    
037    public class ResponsibilityInternalServiceImpl implements ResponsibilityInternalService {
038    
039            private BusinessObjectService businessObjectService;
040        private ResponsibilityService responsibilityService;
041    
042        @Override
043            public RoleMemberBo saveRoleMember(RoleMemberBo roleMember){
044    
045                    //need to find what responsibilities changed so we can notify interested clients.  Like workflow.
046            List<RoleResponsibility> oldRoleResp = getRoleResponsibilities(roleMember.getRoleId());
047    
048            // add row to member table
049            RoleMemberBo member = getBusinessObjectService().save( roleMember );
050    
051            //need to find what responsibilities changed so we can notify interested clients.  Like workflow.
052            // the new member has been added
053            List<RoleResponsibility> newRoleResp = getRoleResponsibilities(roleMember.getRoleId());
054    
055            updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(oldRoleResp, newRoleResp));
056    
057            return member;
058            }
059    
060        @Override
061        public DelegateMemberBo saveDelegateMember(DelegateMemberBo delegateMember) {
062    
063            // add row to member table
064            DelegateMemberBo member = getBusinessObjectService().save(delegateMember);
065    
066            return member;
067        }
068    
069        @Override
070            public void removeRoleMember(RoleMemberBo roleMember){
071                    //need to find what responsibilities changed so we can notify interested clients.  Like workflow.
072            List<RoleResponsibility> oldRoleResp = getRoleResponsibilities(roleMember.getRoleId());
073    
074            // need to set end date to inactivate, not delete
075            roleMember.setActiveToDateValue(new java.sql.Timestamp(System.currentTimeMillis()));
076            getBusinessObjectService().save( roleMember );
077    
078            //need to find what responsibilities changed so we can notify interested clients.  Like workflow.
079            // the new member has been added
080            List<RoleResponsibility> newRoleResp = getRoleResponsibilities(roleMember.getRoleId());
081    
082            updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(oldRoleResp, newRoleResp));
083            }
084    
085        @Override
086            @SuppressWarnings("unchecked")
087            public void updateActionRequestsForRoleChange(String roleId) {
088            List<RoleResponsibility> newRoleResp = getRoleResponsibilities(roleId);
089                    
090            updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(Collections.EMPTY_LIST, newRoleResp));
091            }
092            
093    
094            /**
095             * This overridden method ...
096             *
097             * @see ResponsibilityInternalService#updateActionRequestsForResponsibilityChange(java.util.Set)
098             */
099        @Override
100            public void updateActionRequestsForResponsibilityChange(Set<String> responsibilityIds) {
101            KewApiServiceLocator.getResponsibilityChangeQueue().responsibilitiesChanged(responsibilityIds);
102            }
103    
104            @SuppressWarnings("unchecked")
105            public List<RoleResponsibility> getRoleResponsibilities(String roleId){
106                    List<RoleResponsibilityBo> rrBoList =
107                                    (List<RoleResponsibilityBo>)getBusinessObjectService()
108                                    .findMatching(RoleResponsibilityBo.class, Collections.singletonMap(KimConstants.PrimaryKeyConstants.SUB_ROLE_ID, roleId));
109                    List<RoleResponsibility> result = new ArrayList<RoleResponsibility>( rrBoList.size() );
110                    for ( RoleResponsibilityBo bo : rrBoList ) {
111                            result.add( RoleResponsibilityBo.to(bo) );
112                    }
113                    return result;
114        }
115    
116             /**
117        *
118        * This method compares the two lists of responsibilitiy IDs and does a union.  returns a unique list of responsibility ids.
119        *
120        * @param oldRespList
121        * @param newRespList
122        * @return
123        */
124       protected Set<String> getChangedRoleResponsibilityIds(
125                            List<RoleResponsibility> oldRespList,
126                            List<RoleResponsibility> newRespList) {
127                    Set<String> lRet = new HashSet<String>();
128    
129                    for (RoleResponsibility resp : oldRespList) {
130                            lRet.add(resp.getResponsibilityId());
131                    }
132                    for (RoleResponsibility resp : newRespList) {
133                            lRet.add(resp.getResponsibilityId());
134                    }
135    
136                    return lRet;
137            }
138    
139            protected BusinessObjectService getBusinessObjectService() {
140                    if ( businessObjectService == null ) {
141                            businessObjectService = KRADServiceLocator.getBusinessObjectService();
142                    }
143                    return businessObjectService;
144            }
145    
146        protected ResponsibilityService getResponsibilityService() {
147                    if ( responsibilityService == null ) {
148                            responsibilityService = KimApiServiceLocator.getResponsibilityService();
149                    }
150                    return responsibilityService;
151            }
152    
153        @Override
154        public boolean areActionsAtAssignmentLevel(Responsibility responsibility ) {
155            Map<String, String> details = responsibility.getAttributes();
156            if ( details == null ) {
157                    return false;
158            }
159            String actionDetailsAtRoleMemberLevel = details.get( KimConstants.AttributeConstants.ACTION_DETAILS_AT_ROLE_MEMBER_LEVEL );
160            return Boolean.valueOf(actionDetailsAtRoleMemberLevel);
161        }
162    
163        @Override
164        public boolean areActionsAtAssignmentLevelById( String responsibilityId ) {
165            Responsibility responsibility = getResponsibilityService().getResponsibility(responsibilityId);
166            if ( responsibility == null ) {
167                    return false;
168            }
169            return areActionsAtAssignmentLevel(responsibility);
170        }
171    
172    }