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.hr.time.department;
17  
18  import java.sql.Timestamp;
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.lang.StringUtils;
24  import org.kuali.hr.time.HrBusinessObject;
25  import org.kuali.hr.time.roles.TkRole;
26  import org.kuali.hr.time.service.base.TkServiceLocator;
27  import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
28  import org.kuali.hr.time.util.TKContext;
29  import org.kuali.rice.kim.api.identity.Person;
30  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
31  import org.kuali.rice.kns.document.MaintenanceDocument;
32  import org.kuali.rice.kns.maintenance.Maintainable;
33  import org.kuali.rice.kns.web.ui.Section;
34  import org.kuali.rice.krad.bo.PersistableBusinessObject;
35  import org.kuali.rice.krad.util.GlobalVariables;
36  import org.kuali.rice.krad.util.KRADConstants;
37  
38  public class DepartmentMaintainableImpl extends HrBusinessObjectMaintainableImpl {
39  
40  	private static final long serialVersionUID = -330523155799598560L;
41  
42      @Override
43  	protected void setNewCollectionLineDefaultValues(String arg0,
44  			PersistableBusinessObject arg1) {
45      	if(arg1 instanceof TkRole){
46      		TkRole role = (TkRole)arg1;
47      		Department dept = (Department) this.getBusinessObject();
48      		role.setEffectiveDate(dept.getEffectiveDate());
49      	}
50  		super.setNewCollectionLineDefaultValues(arg0, arg1);
51  	}
52  
53  	@Override
54      public void processAfterEdit( MaintenanceDocument document, Map<String,String[]> parameters ) {
55          Department dOld = (Department)document.getOldMaintainableObject().getBusinessObject();
56          Department dNew = (Department)document.getNewMaintainableObject().getBusinessObject();
57  
58          TkServiceLocator.getDepartmentService().populateDepartmentRoles(dOld);
59          TkServiceLocator.getDepartmentService().populateDepartmentRoles(dNew);
60          super.processAfterEdit(document, parameters);
61      }
62  	
63  	@Override
64      public void addNewLineToCollection(String collectionName) {
65          if (collectionName.equals("roles")) {
66          	TkRole aRole = (TkRole)newCollectionLines.get(collectionName );
67              if ( aRole != null ) {
68              	if(!aRole.getPrincipalId().isEmpty()) {
69              		Person aPerson = KimApiServiceLocator.getPersonService().getPerson(aRole.getPrincipalId());
70              		if(aPerson == null) {
71              			GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles", 
72                  				"dept.role.person.notExist",aRole.getPrincipalId());
73                  		return;
74              		}
75              	}
76              }
77          }
78          super.addNewLineToCollection(collectionName);
79  	}
80  	
81  	@SuppressWarnings("rawtypes")
82  	@Override
83  	public List getSections(MaintenanceDocument document,
84  			Maintainable oldMaintainable) {
85  		List sections = super.getSections(document, oldMaintainable);
86  		for (Object obj : sections) {
87  			Section sec = (Section) obj;
88  			if (document.isOldBusinessObjectInDocument()
89  					&& sec.getSectionId().equals("inactiveRoles")) {
90  				sec.setHidden(false);
91  			} else if (!document.isOldBusinessObjectInDocument()
92  					&& sec.getSectionId().equals("inactiveRoles")) {
93  				sec.setHidden(true);
94  			}
95  		}
96  		return sections;
97  	}
98  
99  	@Override
100 	public HrBusinessObject getObjectById(String id) {
101 		return TkServiceLocator.getDepartmentService().getDepartment(id);
102 	}
103 
104     @Override
105 	public void customSaveLogic(HrBusinessObject hrObj) {
106 		Department department = (Department) hrObj;
107 		
108 		List<TkRole> roles = new ArrayList<TkRole>();
109 		roles.addAll(department.getRoles());
110 		roles.addAll(department.getInactiveRoles());
111 		roles.addAll(createInactiveRoles(department.getRoles()));
112 
113 		for (TkRole role : roles) {
114 			role.setDepartmentObj(department);
115 			role.setUserPrincipalId(TKContext.getPrincipalId());
116 		}
117 		department.setRoles(roles);
118 		
119 		TkServiceLocator.getTkRoleService().saveOrUpdate(roles);
120 	}
121     
122     private List<TkRole> createInactiveRoles(List<TkRole> activeRoles) {
123     	List<TkRole> inactiveRoles = new ArrayList<TkRole>();
124     	
125         List<TkRole> oldRoles = new ArrayList<TkRole>();
126         List<TkRole> newRoles = new ArrayList<TkRole>();
127         
128         for (TkRole activeRole : activeRoles) {
129 		  	if (!StringUtils.isEmpty(activeRole.getHrRolesId())) {
130 		  		oldRoles.add(activeRole);
131 		  	} else {
132 		  		newRoles.add(activeRole);
133 		  	}
134         }
135         
136         for (TkRole newRole : newRoles) {
137         	for (TkRole oldRole : oldRoles) {
138         		if (StringUtils.equals(newRole.getRoleName(), oldRole.getRoleName()) 
139        	      	 && StringUtils.equals(newRole.getPrincipalId(), oldRole.getPrincipalId())) {
140         			TkRole newInactiveRole = new TkRole();
141         			newInactiveRole.setPrincipalId(oldRole.getPrincipalId());
142         			newInactiveRole.setRoleName(oldRole.getRoleName());
143         			newInactiveRole.setWorkArea(oldRole.getWorkArea());
144         			newInactiveRole.setDepartment(oldRole.getDepartment());
145         			newInactiveRole.setChart(oldRole.getChart());
146         			newInactiveRole.setHrDeptId(oldRole.getHrDeptId());
147         			newInactiveRole.setPositionNumber(oldRole.getPositionNumber());
148         			newInactiveRole.setExpirationDate(oldRole.getExpirationDate());
149         			newInactiveRole.setEffectiveDate(newRole.getEffectiveDate());
150         			newInactiveRole.setTimestamp(new Timestamp(System.currentTimeMillis()));
151 			  		newInactiveRole.setUserPrincipalId(TKContext.getPrincipalId());
152         			newInactiveRole.setActive(false);
153         			
154         			inactiveRoles.add(newInactiveRole);
155         		}
156         	}
157         }
158         
159         return inactiveRoles;
160     }
161 }