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