View Javadoc

1   /**
2    * Copyright 2004-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.kpme.core.location.web;
17  
18  import java.sql.Timestamp;
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.joda.time.DateTime;
26  import org.kuali.kpme.core.bo.HrBusinessObject;
27  import org.kuali.kpme.core.bo.HrBusinessObjectMaintainableImpl;
28  import org.kuali.kpme.core.location.Location;
29  import org.kuali.kpme.core.role.KPMERoleMemberAttribute;
30  import org.kuali.kpme.core.role.location.LocationPrincipalRoleMemberBo;
31  import org.kuali.kpme.core.service.HrServiceLocator;
32  import org.kuali.rice.kim.api.identity.Person;
33  import org.kuali.rice.kim.api.role.Role;
34  import org.kuali.rice.kim.api.role.RoleMember;
35  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
36  import org.kuali.rice.kim.impl.role.RoleMemberBo;
37  import org.kuali.rice.kns.document.MaintenanceDocument;
38  import org.kuali.rice.kns.maintenance.Maintainable;
39  import org.kuali.rice.kns.web.ui.Section;
40  import org.kuali.rice.krad.bo.PersistableBusinessObject;
41  import org.kuali.rice.krad.util.GlobalVariables;
42  import org.kuali.rice.krad.util.KRADConstants;
43  
44  @SuppressWarnings("deprecation")
45  public class LocationMaintainableImpl extends HrBusinessObjectMaintainableImpl {
46  
47  	private static final long serialVersionUID = 130620652923528916L;
48  
49  	@Override
50  	public HrBusinessObject getObjectById(String id) {
51  		return HrServiceLocator.getLocationService().getLocation(id);
52  	}
53  	
54  	@Override
55  	@SuppressWarnings("rawtypes")
56  	public List getSections(MaintenanceDocument document, Maintainable oldMaintainable) {
57  		List sections = super.getSections(document, oldMaintainable);
58  		
59  		for (Object obj : sections) {
60  			Section sec = (Section) obj;
61  			if (sec.getSectionId().equals("inactiveRoleMembers")) {
62              	sec.setHidden(!document.isOldBusinessObjectInDocument());
63              }
64  		}
65  		
66  		return sections;
67  	}
68  	
69      @Override
70      public void processAfterEdit(MaintenanceDocument document, Map<String, String[]> parameters) {
71          Location oldMaintainableObject = (Location) document.getOldMaintainableObject().getBusinessObject();
72          Location newMaintainableObject = (Location) document.getNewMaintainableObject().getBusinessObject();
73          
74          Location oldLocation = oldMaintainableObject;
75          if(StringUtils.isNotBlank(oldMaintainableObject.getHrLocationId())) {
76          	oldLocation = HrServiceLocator.getLocationService().getLocation(oldMaintainableObject.getHrLocationId());
77          } else {
78          	oldLocation = HrServiceLocator.getLocationService().getLocation(oldMaintainableObject.getLocation(), oldMaintainableObject.getEffectiveLocalDate());
79          }
80          
81          oldMaintainableObject.setRoleMembers(oldLocation.getRoleMembers());
82          oldMaintainableObject.setInactiveRoleMembers(oldLocation.getInactiveRoleMembers());        
83          
84          Location newLocation = newMaintainableObject;
85          if(StringUtils.isNotBlank(newMaintainableObject.getHrLocationId())) {
86          	newLocation = HrServiceLocator.getLocationService().getLocation(newMaintainableObject.getHrLocationId());
87          } else {
88          	newLocation = HrServiceLocator.getLocationService().getLocation(newMaintainableObject.getLocation(), newMaintainableObject.getEffectiveLocalDate());
89          }
90          
91          newMaintainableObject.setRoleMembers(newLocation.getRoleMembers());
92          newMaintainableObject.setInactiveRoleMembers(newLocation.getInactiveRoleMembers());
93  
94          List<Location> locationList = HrServiceLocator.getLocationService().getNewerVersionLocation(newLocation.getLocation(), newLocation.getEffectiveLocalDate());
95          if (locationList.size() > 0) {
96              GlobalVariables.getMessageMap().putWarningForSectionId(
97                      "Location Maintenance",
98                      "location.newer.exists", null);
99          }
100         super.processAfterEdit(document, parameters);
101     }
102 
103     @Override
104 	protected void setNewCollectionLineDefaultValues(String collectionName, PersistableBusinessObject addLine) {
105     	Location location = (Location) this.getBusinessObject();
106     	
107     	if (location.getEffectiveDate() != null) {
108 	    	if (addLine instanceof RoleMemberBo) {
109 	        	RoleMemberBo roleMember = (RoleMemberBo) addLine;
110 	        	roleMember.setActiveFromDateValue(new Timestamp(location.getEffectiveDate().getTime()));
111 	    	}
112     	}
113     	
114     	super.setNewCollectionLineDefaultValues(collectionName, addLine);
115 	}
116 	
117 	@Override
118     public void addNewLineToCollection(String collectionName) {
119 		if (collectionName.equals("roleMembers")) {
120 			RoleMemberBo roleMember = (RoleMemberBo) newCollectionLines.get(collectionName);
121             if (roleMember != null) {
122             	if(!StringUtils.isEmpty(roleMember.getMemberId())) {
123             		Person person = KimApiServiceLocator.getPersonService().getPerson(roleMember.getMemberId());
124             		if (person == null) {
125             			GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roleMembers", 
126                 				"location.role.person.notExist",roleMember.getMemberId());
127                 		return;
128             		}
129             	}
130             }
131         }
132 		
133         super.addNewLineToCollection(collectionName);
134 	}
135 
136     @Override
137 	public void customSaveLogic(HrBusinessObject hrObj) {
138 		Location location = (Location) hrObj;
139 		
140 		List<LocationPrincipalRoleMemberBo> newInactiveRoleMembers = createInactiveRoleMembers(location.getRoleMembers());
141 		List<LocationPrincipalRoleMemberBo> roleList = new ArrayList<LocationPrincipalRoleMemberBo> ();
142 		roleList.addAll(location.getRoleMembers());
143 		
144     	for (LocationPrincipalRoleMemberBo newInactiveRoleMember : newInactiveRoleMembers) {
145     		location.addInactiveRoleMember(newInactiveRoleMember);
146     		List<LocationPrincipalRoleMemberBo> tempRoleList = location.getRoleMembers();
147     		for(LocationPrincipalRoleMemberBo role : tempRoleList) {
148     			if(StringUtils.isNotEmpty(role.getId())
149     					&& StringUtils.isNotEmpty(newInactiveRoleMember.getId())
150     					&& StringUtils.equals(role.getId(), newInactiveRoleMember.getId())) {
151     				roleList.remove(role);
152     			}
153     		}
154     	}
155 		    	
156     	for (LocationPrincipalRoleMemberBo roleMember : roleList) {
157     		RoleMember.Builder builder = RoleMember.Builder.create(roleMember);
158     		builder.setAttributes(Collections.singletonMap(KPMERoleMemberAttribute.LOCATION.getRoleMemberAttributeName(), location.getLocation()));
159     		
160     		if (StringUtils.isBlank(roleMember.getId())) {
161     			KimApiServiceLocator.getRoleService().createRoleMember(builder.build());
162     		} else {
163     			KimApiServiceLocator.getRoleService().updateRoleMember(builder.build());
164     		}
165     	}
166     	for (LocationPrincipalRoleMemberBo inactiveRoleMember : location.getInactiveRoleMembers()) {
167     		RoleMember.Builder builder = RoleMember.Builder.create(inactiveRoleMember);
168     		builder.setAttributes(Collections.singletonMap(KPMERoleMemberAttribute.LOCATION.getRoleMemberAttributeName(),  location.getLocation()));
169     		
170     		if (StringUtils.isBlank(inactiveRoleMember.getId())) {
171     			KimApiServiceLocator.getRoleService().createRoleMember(builder.build());
172     		} else {
173     			KimApiServiceLocator.getRoleService().updateRoleMember(builder.build());
174     		}
175     	}
176 	}
177     
178     private List<LocationPrincipalRoleMemberBo> createInactiveRoleMembers(List<LocationPrincipalRoleMemberBo> roleMembers) {
179     	List<LocationPrincipalRoleMemberBo> inactiveRoleMembers = new ArrayList<LocationPrincipalRoleMemberBo>();
180     	
181         List<RoleMemberBo> oldRoleMembers = new ArrayList<RoleMemberBo>();
182         List<RoleMemberBo> newRoleMembers = new ArrayList<RoleMemberBo>();
183         for (RoleMemberBo roleMember : roleMembers) {
184 		  	if (!StringUtils.isEmpty(roleMember.getId())) {
185 		  		oldRoleMembers.add(roleMember);
186 		  	} else {
187 		  		newRoleMembers.add(roleMember);
188 		  	}
189         }
190         
191         for (RoleMemberBo newRoleMember : newRoleMembers) {
192     		Role newRole = KimApiServiceLocator.getRoleService().getRole(newRoleMember.getRoleId());
193         	for (RoleMemberBo oldRoleMember : oldRoleMembers) {
194         		Role oldRole = KimApiServiceLocator.getRoleService().getRole(oldRoleMember.getRoleId());
195 			  	
196         		if (StringUtils.equals(newRole.getName(), oldRole.getName()) && StringUtils.equals(newRoleMember.getMemberId(), oldRoleMember.getMemberId())) {
197     				LocationPrincipalRoleMemberBo.Builder builder = LocationPrincipalRoleMemberBo.Builder.create(oldRoleMember);
198     				builder.setActiveToDate(new DateTime());
199     				
200 			  		inactiveRoleMembers.add(builder.build());
201 			  	}
202         	}
203         }
204         
205         return inactiveRoleMembers;
206     }
207         
208     @Override
209     public Map<String, String> populateNewCollectionLines(Map<String, String> fieldValues,
210 			MaintenanceDocument maintenanceDocument, String methodToCall) {
211     	if(fieldValues.containsKey("roleMembers.roleName") && StringUtils.isEmpty(fieldValues.get("roleMembers.roleName"))) {
212     		fieldValues.put("roleMembers.roleName", null);
213     	}
214     	return super.populateNewCollectionLines(fieldValues, maintenanceDocument, methodToCall);
215     }   
216 }