View Javadoc

1   /**
2    * Copyright 2005-2013 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.responsibility;
17  
18  import org.kuali.rice.kew.api.KewApiServiceLocator;
19  import org.kuali.rice.kim.api.KimConstants;
20  import org.kuali.rice.kim.api.responsibility.Responsibility;
21  import org.kuali.rice.kim.api.responsibility.ResponsibilityService;
22  import org.kuali.rice.kim.api.role.RoleResponsibility;
23  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24  import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
25  import org.kuali.rice.kim.impl.role.RoleMemberBo;
26  import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
27  import org.kuali.rice.krad.service.BusinessObjectService;
28  import org.kuali.rice.krad.service.KRADServiceLocator;
29  
30  import java.util.ArrayList;
31  import java.util.Collections;
32  import java.util.HashSet;
33  import java.util.List;
34  import java.util.Map;
35  import java.util.Set;
36  
37  public class ResponsibilityInternalServiceImpl implements ResponsibilityInternalService {
38  
39  	private BusinessObjectService businessObjectService;
40      private ResponsibilityService responsibilityService;
41  
42      @Override
43  	public RoleMemberBo saveRoleMember(RoleMemberBo roleMember){
44  
45  		//need to find what responsibilities changed so we can notify interested clients.  Like workflow.
46      	List<RoleResponsibility> oldRoleResp = getRoleResponsibilities(roleMember.getRoleId());
47  
48      	// add row to member table
49      	RoleMemberBo member = getBusinessObjectService().save( roleMember );
50  
51      	//need to find what responsibilities changed so we can notify interested clients.  Like workflow.
52      	// the new member has been added
53      	List<RoleResponsibility> newRoleResp = getRoleResponsibilities(roleMember.getRoleId());
54  
55      	updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(oldRoleResp, newRoleResp));
56  
57          return member;
58  	}
59  
60      @Override
61      public DelegateMemberBo saveDelegateMember(DelegateMemberBo delegateMember) {
62  
63          // add row to member table
64          DelegateMemberBo member = getBusinessObjectService().save(delegateMember);
65  
66          return member;
67      }
68  
69      @Override
70  	public void removeRoleMember(RoleMemberBo roleMember){
71  		//need to find what responsibilities changed so we can notify interested clients.  Like workflow.
72      	List<RoleResponsibility> oldRoleResp = getRoleResponsibilities(roleMember.getRoleId());
73  
74      	// need to set end date to inactivate, not delete
75          roleMember.setActiveToDateValue(new java.sql.Timestamp(System.currentTimeMillis()));
76      	getBusinessObjectService().save( roleMember );
77  
78      	//need to find what responsibilities changed so we can notify interested clients.  Like workflow.
79      	// the new member has been added
80      	List<RoleResponsibility> newRoleResp = getRoleResponsibilities(roleMember.getRoleId());
81  
82      	updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(oldRoleResp, newRoleResp));
83  	}
84  
85      @Override
86  	@SuppressWarnings("unchecked")
87  	public void updateActionRequestsForRoleChange(String roleId) {
88      	List<RoleResponsibility> newRoleResp = getRoleResponsibilities(roleId);
89  		
90      	updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(Collections.EMPTY_LIST, newRoleResp));
91  	}
92  	
93  
94  	/**
95  	 * This overridden method ...
96  	 *
97  	 * @see ResponsibilityInternalService#updateActionRequestsForResponsibilityChange(java.util.Set)
98  	 */
99      @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 }