View Javadoc
1   /**
2    * Copyright 2005-2015 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 java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.HashSet;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.kuali.rice.core.api.criteria.PredicateFactory;
27  import org.kuali.rice.core.api.criteria.QueryByCriteria;
28  import org.kuali.rice.core.api.datetime.DateTimeService;
29  import org.kuali.rice.core.api.util.Truth;
30  import org.kuali.rice.kew.api.KewApiServiceLocator;
31  import org.kuali.rice.kim.api.KimConstants;
32  import org.kuali.rice.kim.api.responsibility.Responsibility;
33  import org.kuali.rice.kim.api.responsibility.ResponsibilityService;
34  import org.kuali.rice.kim.api.role.RoleResponsibility;
35  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
36  import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
37  import org.kuali.rice.kim.impl.role.RoleMemberBo;
38  import org.kuali.rice.kim.impl.role.RoleResponsibilityBo;
39  import org.kuali.rice.krad.data.DataObjectService;
40  import org.kuali.rice.krad.data.PersistenceOption;
41  import org.kuali.rice.krad.util.KRADPropertyConstants;
42  
43  public class ResponsibilityInternalServiceImpl implements ResponsibilityInternalService {
44  
45      protected DataObjectService dataObjectService;
46      protected ResponsibilityService responsibilityService;
47      protected DateTimeService dateTimeService;
48  
49      @Override
50  	public RoleMemberBo saveRoleMember(RoleMemberBo roleMember){
51  
52  		//need to find what responsibilities changed so we can notify interested clients.  Like workflow.
53      	List<RoleResponsibility> oldRoleResp = getRoleResponsibilities(roleMember.getRoleId());
54  
55      	// add row to member table
56      	RoleMemberBo member = dataObjectService.save( roleMember, PersistenceOption.FLUSH );
57  
58      	//need to find what responsibilities changed so we can notify interested clients.  Like workflow.
59      	// the new member has been added
60      	List<RoleResponsibility> newRoleResp = getRoleResponsibilities(roleMember.getRoleId());
61  
62      	updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(oldRoleResp, newRoleResp));
63  
64          return member;
65  	}
66  
67      @Override
68      public DelegateMemberBo saveDelegateMember(DelegateMemberBo delegateMember) {
69  
70          // add row to member table
71          DelegateMemberBo member = dataObjectService.save(delegateMember, PersistenceOption.FLUSH);
72  
73          return member;
74      }
75  
76      @Override
77  	public void removeRoleMember(RoleMemberBo roleMember){
78  		//need to find what responsibilities changed so we can notify interested clients.  Like workflow.
79      	List<RoleResponsibility> oldRoleResp = getRoleResponsibilities(roleMember.getRoleId());
80  
81      	// need to set end date to inactivate, not delete
82          roleMember.setActiveToDateValue(dateTimeService.getCurrentTimestamp());
83      	roleMember = dataObjectService.save( roleMember, PersistenceOption.FLUSH );
84  
85          // Notify the RoleTypeService for this role that the member was removed
86          KimApiServiceLocator.getRoleService().notifyOnMemberRemoval(RoleMemberBo.to(roleMember));
87  
88      	//need to find what responsibilities changed so we can notify interested clients.  Like workflow.
89      	// the new member has been added
90      	List<RoleResponsibility> newRoleResp = getRoleResponsibilities(roleMember.getRoleId());
91  
92      	updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(oldRoleResp, newRoleResp));
93  	}
94  
95      @Override
96  	@SuppressWarnings("unchecked")
97  	public void updateActionRequestsForRoleChange(String roleId) {
98      	List<RoleResponsibility> newRoleResp = getRoleResponsibilities(roleId);
99  
100     	updateActionRequestsForResponsibilityChange(getChangedRoleResponsibilityIds(Collections.EMPTY_LIST, newRoleResp));
101 	}
102 
103 
104     @Override
105 	public void updateActionRequestsForResponsibilityChange(Set<String> responsibilityIds) {
106         KewApiServiceLocator.getResponsibilityChangeQueue().responsibilitiesChanged(responsibilityIds);
107 	}
108 
109 	@Override
110     public List<RoleResponsibility> getRoleResponsibilities(String roleId){
111 		List<RoleResponsibilityBo> rrBoList = dataObjectService.findMatching( RoleResponsibilityBo.class, QueryByCriteria.Builder.fromPredicates(
112 		            PredicateFactory.equal(KimConstants.PrimaryKeyConstants.SUB_ROLE_ID, roleId),
113 		            PredicateFactory.equal(KRADPropertyConstants.ACTIVE,Boolean.TRUE) ) ).getResults();
114 		List<RoleResponsibility> result = new ArrayList<RoleResponsibility>( rrBoList.size() );
115 		for ( RoleResponsibilityBo bo : rrBoList ) {
116 			result.add( RoleResponsibilityBo.to(bo) );
117 		}
118 		return result;
119     }
120 
121     /**
122      *
123      * This method compares the two lists of responsibility IDs and does a union. returns a unique
124      * list of responsibility ids.
125      *
126      * @param oldRespList
127      * @param newRespList
128      * @return
129      */
130     protected Set<String> getChangedRoleResponsibilityIds(
131 			List<RoleResponsibility> oldRespList,
132 			List<RoleResponsibility> newRespList) {
133 		Set<String> lRet = new HashSet<String>();
134 
135 		for (RoleResponsibility resp : oldRespList) {
136 			lRet.add(resp.getResponsibilityId());
137 		}
138 		for (RoleResponsibility resp : newRespList) {
139 			lRet.add(resp.getResponsibilityId());
140 		}
141 
142 		return lRet;
143 	}
144 
145     @Override
146     public boolean areActionsAtAssignmentLevel(Responsibility responsibility ) {
147     	Map<String, String> details = responsibility.getAttributes();
148     	if ( details == null ) {
149     		return false;
150     	}
151     	String actionDetailsAtRoleMemberLevel = StringUtils.trimToEmpty( details.get( KimConstants.AttributeConstants.ACTION_DETAILS_AT_ROLE_MEMBER_LEVEL ) );
152     	return Truth.strToBooleanIgnoreCase(actionDetailsAtRoleMemberLevel, Boolean.FALSE);
153     }
154 
155     @Override
156     public boolean areActionsAtAssignmentLevelById( String responsibilityId ) {
157     	Responsibility responsibility = responsibilityService.getResponsibility(responsibilityId);
158     	if ( responsibility == null ) {
159     		return false;
160     	}
161     	return areActionsAtAssignmentLevel(responsibility);
162     }
163 
164     public void setResponsibilityService(ResponsibilityService responsibilityService) {
165         this.responsibilityService = responsibilityService;
166     }
167 
168     public void setDataObjectService(DataObjectService dataObjectService) {
169         this.dataObjectService = dataObjectService;
170     }
171 
172     public void setDateTimeService(DateTimeService dateTimeService) {
173         this.dateTimeService = dateTimeService;
174     }
175 
176 }