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