View Javadoc
1   /**
2    * Copyright 2004-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.kpme.core.department.web;
17  
18  import java.sql.Timestamp;
19  import java.util.*;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.joda.time.DateTime;
23  import org.kuali.kpme.core.bo.HrBusinessObject;
24  import org.kuali.kpme.core.bo.HrBusinessObjectMaintainableImpl;
25  import org.kuali.kpme.core.department.DepartmentBo;
26  import org.kuali.kpme.core.role.KPMERoleMemberAttribute;
27  import org.kuali.kpme.core.role.department.DepartmentPrincipalRoleMemberBo;
28  import org.kuali.kpme.core.service.HrServiceLocator;
29  import org.kuali.kpme.core.service.HrServiceLocatorInternal;
30  import org.kuali.rice.kim.api.identity.name.EntityName;
31  import org.kuali.rice.kim.api.identity.principal.Principal;
32  import org.kuali.rice.kim.api.role.RoleMember;
33  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
34  import org.kuali.rice.kim.impl.role.RoleMemberBo;
35  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
36  import org.kuali.rice.krad.bo.PersistableBusinessObject;
37  import org.kuali.rice.krad.util.GlobalVariables;
38  import org.kuali.rice.krad.util.KRADConstants;
39  
40  @SuppressWarnings("deprecation")
41  public class DepartmentMaintainableImpl extends HrBusinessObjectMaintainableImpl {
42  
43  	private static final long serialVersionUID = -330523155799598560L;
44  	
45  	@Override
46  	public HrBusinessObject getObjectById(String id) {
47  		return (HrBusinessObject) HrServiceLocatorInternal.getDepartmentInternalService().getDepartmentWithRoleData(id);
48  	}
49  
50      @Override
51      public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> requestParameters) {
52          DepartmentBo oldMaintainableObject = (DepartmentBo) document.getOldMaintainableObject().getDataObject();
53          DepartmentBo newMaintainableObject = (DepartmentBo) document.getNewMaintainableObject().getDataObject();
54          
55          DepartmentBo oldDepartment = oldMaintainableObject;
56          if(StringUtils.isNotBlank(oldMaintainableObject.getHrDeptId())) {
57          	oldDepartment = HrServiceLocatorInternal.getDepartmentInternalService().getDepartmentWithRoleData(oldMaintainableObject.getHrDeptId());
58          } else {
59          	oldDepartment = HrServiceLocatorInternal.getDepartmentInternalService().getDepartmentWithRoleData(oldMaintainableObject.getDept(), oldMaintainableObject.getGroupKeyCode(), oldMaintainableObject.getEffectiveLocalDate());
60          }
61  
62          //KPME-3312: reinitiate all collection lists so old and new collections are unique
63          List<DepartmentPrincipalRoleMemberBo> oldRoleMembers = new ArrayList<DepartmentPrincipalRoleMemberBo>();
64          oldRoleMembers.addAll(oldDepartment.getRoleMembers());
65          oldMaintainableObject.setRoleMembers(oldRoleMembers);
66          
67          List<DepartmentPrincipalRoleMemberBo> oldInactiveRoleMembers = new ArrayList<DepartmentPrincipalRoleMemberBo>();
68          oldInactiveRoleMembers.addAll(oldDepartment.getInactiveRoleMembers());
69          oldMaintainableObject.setInactiveRoleMembers(oldInactiveRoleMembers);
70          
71          DepartmentBo newDepartment = newMaintainableObject;
72          if(StringUtils.isNotBlank(newMaintainableObject.getHrDeptId())) {
73          	newDepartment = HrServiceLocatorInternal.getDepartmentInternalService().getDepartmentWithRoleData(newMaintainableObject.getHrDeptId());
74          } else {
75          	newDepartment = HrServiceLocatorInternal.getDepartmentInternalService().getDepartmentWithRoleData(newMaintainableObject.getDept(), newMaintainableObject.getGroupKeyCode(), newMaintainableObject.getEffectiveLocalDate());
76          }
77          
78          List<DepartmentPrincipalRoleMemberBo> newRoleMembers = new ArrayList<DepartmentPrincipalRoleMemberBo>();
79          newRoleMembers.addAll(newDepartment.getRoleMembers());
80          newMaintainableObject.setRoleMembers(newRoleMembers);
81  
82          List<DepartmentPrincipalRoleMemberBo> newInactiveRoleMembers = new ArrayList<DepartmentPrincipalRoleMemberBo>();
83          newInactiveRoleMembers.addAll(newDepartment.getInactiveRoleMembers());
84          newMaintainableObject.setInactiveRoleMembers(newInactiveRoleMembers);
85  
86          super.processAfterEdit(document, requestParameters);
87      }
88  
89      @Override
90  	protected void setNewCollectionLineDefaultValues(String collectionName, PersistableBusinessObject addLine) {
91      	DepartmentBo department = (DepartmentBo) this.getBusinessObject();
92      	
93      	if (department.getEffectiveDate() != null) {
94  	    	if (addLine instanceof RoleMemberBo) {
95  	        	RoleMemberBo roleMember = (RoleMemberBo) addLine;
96  	        	roleMember.setActiveFromDateValue(new Timestamp(department.getEffectiveDate().getTime()));
97  	    	}
98      	}
99      	
100     	super.setNewCollectionLineDefaultValues(collectionName, addLine);
101 	}
102 	
103 	@Override
104     public void addNewLineToCollection(String collectionName) {
105 		if (collectionName.equals("roleMembers")) {
106 			RoleMemberBo roleMember = (RoleMemberBo) newCollectionLines.get(collectionName);
107             if (roleMember != null) {
108             	if(!StringUtils.isEmpty(roleMember.getMemberId())) {
109             		Principal person = KimApiServiceLocator.getIdentityService().getPrincipal(roleMember.getMemberId());
110             		if (person == null) {
111             			GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roleMembers", 
112                 				"dept.role.person.notExist",roleMember.getMemberId());
113                 		return;
114             		}
115             	}
116             }
117         }
118 		
119         super.addNewLineToCollection(collectionName);
120 	}
121 
122     @Override
123 	public void customSaveLogic(HrBusinessObject hrObj) {
124 		DepartmentBo department = (DepartmentBo) hrObj;
125 		
126 		List<DepartmentPrincipalRoleMemberBo> newInactiveRoleMembers = createInactiveRoleMembers(department.getRoleMembers());
127 		List<DepartmentPrincipalRoleMemberBo> roleList = new ArrayList<DepartmentPrincipalRoleMemberBo> ();
128 		roleList.addAll(department.getRoleMembers());
129 		
130     	for (DepartmentPrincipalRoleMemberBo newInactiveRoleMember : newInactiveRoleMembers) {
131     		department.addInactiveRoleMember(newInactiveRoleMember);
132     		List<DepartmentPrincipalRoleMemberBo> tempRoleList = department.getRoleMembers();
133     		for(DepartmentPrincipalRoleMemberBo role : tempRoleList) {
134     			if(StringUtils.isNotEmpty(role.getId())
135     					&& StringUtils.isNotEmpty(newInactiveRoleMember.getId())
136     					&& StringUtils.equals(role.getId(), newInactiveRoleMember.getId())) {
137     				roleList.remove(role);
138     			}
139     		}
140     	}
141     	
142     	for (DepartmentPrincipalRoleMemberBo roleMember : roleList) {
143     		RoleMember.Builder builder = RoleMember.Builder.create(roleMember);
144             Map<String, String> attributes = new HashMap<String, String>();
145             attributes.put(KPMERoleMemberAttribute.DEPARTMENT.getRoleMemberAttributeName(), department.getDept());
146             attributes.put(KPMERoleMemberAttribute.GROUP_KEY_CODE.getRoleMemberAttributeName(), department.getGroupKeyCode());
147     		builder.setAttributes(attributes);
148     		
149     		if (StringUtils.isBlank(roleMember.getId())) {
150                 KimApiServiceLocator.getRoleService().createRoleMember(builder.build());
151     		} else {
152     			KimApiServiceLocator.getRoleService().updateRoleMember(builder.build());
153     		}
154     	}
155     	for (DepartmentPrincipalRoleMemberBo inactiveRoleMember : department.getInactiveRoleMembers()) {
156     		RoleMember.Builder builder = RoleMember.Builder.create(inactiveRoleMember);
157             Map<String, String> attributes = new HashMap<String, String>();
158             attributes.put(KPMERoleMemberAttribute.DEPARTMENT.getRoleMemberAttributeName(), department.getDept());
159             attributes.put(KPMERoleMemberAttribute.GROUP_KEY_CODE.getRoleMemberAttributeName(), department.getGroupKeyCode());
160     		builder.setAttributes(attributes);
161     		
162     		if (StringUtils.isBlank(inactiveRoleMember.getId())) {
163     			KimApiServiceLocator.getRoleService().createRoleMember(builder.build());
164 
165     		} else {
166     			KimApiServiceLocator.getRoleService().updateRoleMember(builder.build());
167     		}
168     	}
169 	}
170     
171     private List<DepartmentPrincipalRoleMemberBo> createInactiveRoleMembers(List<DepartmentPrincipalRoleMemberBo> roleMembers) {
172     	List<DepartmentPrincipalRoleMemberBo> inactiveRoleMembers = new ArrayList<DepartmentPrincipalRoleMemberBo>();
173     	
174         List<RoleMemberBo> oldRoleMembers = new ArrayList<RoleMemberBo>();
175         List<RoleMemberBo> newRoleMembers = new ArrayList<RoleMemberBo>();
176         for (RoleMemberBo roleMember : roleMembers) {
177 		  	if (!StringUtils.isEmpty(roleMember.getId())) {
178 		  		oldRoleMembers.add(roleMember);
179 		  	} else {
180 		  		newRoleMembers.add(roleMember);
181 		  	}
182         }
183         
184         for (RoleMemberBo newRoleMember : newRoleMembers) {
185         	for (RoleMemberBo oldRoleMember : oldRoleMembers) {
186         		if (StringUtils.equals(newRoleMember.getRoleId(), oldRoleMember.getRoleId()) 
187                            && StringUtils.equals(newRoleMember.getMemberId(), oldRoleMember.getMemberId()) ) {
188     				DepartmentPrincipalRoleMemberBo.Builder builder = DepartmentPrincipalRoleMemberBo.Builder.create(oldRoleMember);
189     				builder.setActiveToDate(DateTime.now());
190     				
191 			  		inactiveRoleMembers.add(builder.build());
192 			  	}
193         	}
194         }
195         
196         return inactiveRoleMembers;
197     }
198 
199 
200 }