View Javadoc

1   /*
2    * Copyright 2007-2009 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.service.impl;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Set;
23  
24  import javax.xml.namespace.QName;
25  
26  import org.kuali.rice.kew.exception.WorkflowRuntimeException;
27  import org.kuali.rice.kew.messaging.MessageServiceNames;
28  import org.kuali.rice.kew.responsibility.ResponsibilityChangeProcessor;
29  import org.kuali.rice.kim.bo.role.dto.RoleResponsibilityInfo;
30  import org.kuali.rice.kim.bo.role.impl.RoleMemberImpl;
31  import org.kuali.rice.kim.bo.role.impl.RoleResponsibilityImpl;
32  import org.kuali.rice.kim.service.ResponsibilityInternalService;
33  import org.kuali.rice.kim.util.KimConstants;
34  import org.kuali.rice.kns.service.BusinessObjectService;
35  import org.kuali.rice.kns.service.KNSServiceLocator;
36  import org.kuali.rice.ksb.messaging.service.KSBXMLService;
37  import org.kuali.rice.ksb.service.KSBServiceLocator;
38  
39  /**
40   * This is a description of what this class does - Garey don't forget to fill this in.
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   *
44   */
45  public class ResponsibilityInternalServiceImpl implements ResponsibilityInternalService {
46  
47  	private BusinessObjectService businessObjectService;
48  
49  	public void saveRoleMember(RoleMemberImpl roleMember){
50  
51  		//need to find what responsibilities changed so we can notify interested clients.  Like workflow.
52      	List<RoleResponsibilityInfo> oldRoleResp = getRoleResponsibilities(roleMember.getRoleId());
53  
54      	// add row to member table
55      	getBusinessObjectService().save( roleMember );
56  
57      	//need to find what responsibilities changed so we can notify interested clients.  Like workflow.
58      	// the new member has been added
59      	List<RoleResponsibilityInfo> newRoleResp = getRoleResponsibilities(roleMember.getRoleId());
60  
61      	updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(oldRoleResp, newRoleResp));
62  	}
63  
64  	public void removeRoleMember(RoleMemberImpl roleMember){
65  		//need to find what responsibilities changed so we can notify interested clients.  Like workflow.
66      	List<RoleResponsibilityInfo> oldRoleResp = getRoleResponsibilities(roleMember.getRoleId());
67  
68      	// add row to member table
69      	getBusinessObjectService().delete( roleMember );
70  
71      	//need to find what responsibilities changed so we can notify interested clients.  Like workflow.
72      	// the new member has been added
73      	List<RoleResponsibilityInfo> newRoleResp = getRoleResponsibilities(roleMember.getRoleId());
74  
75      	updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(oldRoleResp, newRoleResp));
76  	}
77  
78  	@SuppressWarnings("unchecked")
79  	public void updateActionRequestsForRoleChange(String roleId) {
80      	List<RoleResponsibilityInfo> newRoleResp = getRoleResponsibilities(roleId);
81  		
82      	updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(Collections.EMPTY_LIST, newRoleResp));
83  	}
84  	
85  
86  	/**
87  	 * This overridden method ...
88  	 *
89  	 * @see org.kuali.rice.kim.service.ResponsibilityInternalService#updateActionRequestsForResponsibilityChange(java.util.Set)
90  	 */
91  	public void updateActionRequestsForResponsibilityChange(Set<String> responsibilityIds) {
92  
93  		KSBXMLService responsibilityChangeProcessor = (KSBXMLService) KSBServiceLocator.getMessageHelper()
94          .getServiceAsynchronously(new QName(MessageServiceNames.RESPONSIBILITY_CHANGE_SERVICE));
95          try {
96          	responsibilityChangeProcessor.invoke(ResponsibilityChangeProcessor.getResponsibilityChangeContents(responsibilityIds));
97  
98          } catch (Exception e) {
99              throw new WorkflowRuntimeException(e);
100         }
101 
102 	}
103 
104 	@SuppressWarnings("unchecked")
105 	public List<RoleResponsibilityInfo> getRoleResponsibilities(String roleId){		
106 		List<RoleResponsibilityImpl> roleResponsibilities = 
107 				(List<RoleResponsibilityImpl>)getBusinessObjectService()
108 				.findMatching(RoleResponsibilityImpl.class, Collections.singletonMap(KimConstants.PrimaryKeyConstants.ROLE_ID, roleId));
109 		List<RoleResponsibilityInfo> result = new ArrayList<RoleResponsibilityInfo>( roleResponsibilities.size() );
110 		for ( RoleResponsibilityImpl roleResp : roleResponsibilities ) {
111 			result.add( roleResp.toSimpleInfo() );
112 		}
113 
114 		return result;
115     }
116 
117 	 /**
118     *
119     * This method compares the two lists of responsibilitiy IDs and does a union.  returns a unique list of responsibility ids.
120     *
121     * @param oldRespList
122     * @param newRespList
123     * @return
124     */
125    protected Set<String> getChangedRoleResponsibilityIds(
126 			List<RoleResponsibilityInfo> oldRespList,
127 			List<RoleResponsibilityInfo> newRespList) {
128 		Set<String> lRet = new HashSet<String>();
129 
130 		for (RoleResponsibilityInfo resp : oldRespList) {
131 			lRet.add(resp.getResponsibilityId());
132 		}
133 		for (RoleResponsibilityInfo resp : newRespList) {
134 			lRet.add(resp.getResponsibilityId());
135 		}
136 
137 		return lRet;
138 	}
139 
140 	protected BusinessObjectService getBusinessObjectService() {
141 		if ( businessObjectService == null ) {
142 			businessObjectService = KNSServiceLocator.getBusinessObjectService();
143 		}
144 		return businessObjectService;
145 	}
146 
147 }