001 /** 002 * Copyright 2004-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.hr.time.department; 017 018 import java.sql.Timestamp; 019 import java.util.ArrayList; 020 import java.util.List; 021 import java.util.Map; 022 023 import org.apache.commons.lang.StringUtils; 024 import org.kuali.hr.time.HrBusinessObject; 025 import org.kuali.hr.time.roles.TkRole; 026 import org.kuali.hr.time.service.base.TkServiceLocator; 027 import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl; 028 import org.kuali.hr.time.util.TKContext; 029 import org.kuali.rice.kim.api.identity.Person; 030 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 031 import org.kuali.rice.kns.document.MaintenanceDocument; 032 import org.kuali.rice.kns.maintenance.Maintainable; 033 import org.kuali.rice.kns.web.ui.Section; 034 import org.kuali.rice.krad.bo.PersistableBusinessObject; 035 import org.kuali.rice.krad.util.GlobalVariables; 036 import org.kuali.rice.krad.util.KRADConstants; 037 038 public class DepartmentMaintainableImpl extends HrBusinessObjectMaintainableImpl { 039 040 private static final long serialVersionUID = -330523155799598560L; 041 042 @Override 043 protected void setNewCollectionLineDefaultValues(String arg0, 044 PersistableBusinessObject arg1) { 045 if(arg1 instanceof TkRole){ 046 TkRole role = (TkRole)arg1; 047 Department dept = (Department) this.getBusinessObject(); 048 role.setEffectiveDate(dept.getEffectiveDate()); 049 } 050 super.setNewCollectionLineDefaultValues(arg0, arg1); 051 } 052 053 @Override 054 public void processAfterEdit( MaintenanceDocument document, Map<String,String[]> parameters ) { 055 Department dOld = (Department)document.getOldMaintainableObject().getBusinessObject(); 056 Department dNew = (Department)document.getNewMaintainableObject().getBusinessObject(); 057 058 TkServiceLocator.getDepartmentService().populateDepartmentRoles(dOld); 059 TkServiceLocator.getDepartmentService().populateDepartmentRoles(dNew); 060 super.processAfterEdit(document, parameters); 061 } 062 063 @Override 064 public void addNewLineToCollection(String collectionName) { 065 if (collectionName.equals("roles")) { 066 TkRole aRole = (TkRole)newCollectionLines.get(collectionName ); 067 if ( aRole != null ) { 068 if(!aRole.getPrincipalId().isEmpty()) { 069 Person aPerson = KimApiServiceLocator.getPersonService().getPerson(aRole.getPrincipalId()); 070 if(aPerson == null) { 071 GlobalVariables.getMessageMap().putErrorWithoutFullErrorPath(KRADConstants.MAINTENANCE_NEW_MAINTAINABLE +"roles", 072 "dept.role.person.notExist",aRole.getPrincipalId()); 073 return; 074 } 075 } 076 } 077 } 078 super.addNewLineToCollection(collectionName); 079 } 080 081 @SuppressWarnings("rawtypes") 082 @Override 083 public List getSections(MaintenanceDocument document, 084 Maintainable oldMaintainable) { 085 List sections = super.getSections(document, oldMaintainable); 086 for (Object obj : sections) { 087 Section sec = (Section) obj; 088 if (document.isOldBusinessObjectInDocument() 089 && sec.getSectionId().equals("inactiveRoles")) { 090 sec.setHidden(false); 091 } else if (!document.isOldBusinessObjectInDocument() 092 && sec.getSectionId().equals("inactiveRoles")) { 093 sec.setHidden(true); 094 } 095 } 096 return sections; 097 } 098 099 @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 }