001 /** 002 * Copyright 2004-2012 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.roles; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.hr.core.cache.CacheUtils; 020 import org.kuali.hr.job.Job; 021 import org.kuali.hr.time.HrBusinessObject; 022 import org.kuali.hr.time.service.base.TkServiceLocator; 023 import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl; 024 import org.kuali.hr.time.util.TKUtils; 025 import org.kuali.hr.time.util.TkConstants; 026 import org.kuali.rice.kim.api.services.KimApiServiceLocator; 027 import org.kuali.rice.kns.document.MaintenanceDocument; 028 import org.kuali.rice.kns.maintenance.Maintainable; 029 import org.kuali.rice.kns.web.ui.Section; 030 import org.kuali.rice.krad.bo.BusinessObject; 031 import org.kuali.rice.krad.service.KRADServiceLocator; 032 import org.kuali.rice.krad.util.GlobalVariables; 033 034 import java.sql.Timestamp; 035 import java.util.*; 036 037 public class TkRoleGroupMaintainableImpl extends HrBusinessObjectMaintainableImpl { 038 039 /** 040 * 041 */ 042 private static final long serialVersionUID = 1L; 043 044 @Override 045 public void saveBusinessObject() { 046 BusinessObject bo = this.getBusinessObject(); 047 if (bo instanceof TkRoleGroup) { 048 TkRoleGroup trg = (TkRoleGroup)bo; 049 List<TkRole> roles = trg.getRoles(); 050 List<TkRole> rolesCopy = new ArrayList<TkRole>(); 051 rolesCopy.addAll(roles); 052 if (trg.getInactiveRoles() != null 053 && trg.getInactiveRoles().size() > 0) { 054 for (TkRole role : trg.getInactiveRoles()) { 055 roles.add(role); 056 } 057 } 058 for (TkRole role : roles) { 059 if (StringUtils.equals(role.getRoleName(), TkConstants.ROLE_TK_SYS_ADMIN)) { 060 String principalId = role.getPrincipalId(); 061 if(StringUtils.isBlank(principalId)){ 062 principalId = trg.getPrincipalId(); 063 } 064 if(StringUtils.isBlank(principalId)){ 065 KimApiServiceLocator.getRoleService().assignPrincipalToRole(principalId, TkConstants.ROLE_NAMESAPCE, role.getRoleName(), new HashMap<String,String>()); 066 } 067 } 068 role.setPrincipalId(trg.getPrincipalId()); 069 role.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId()); 070 071 HrBusinessObject oldHrObj = this.getObjectById(role.getId()); 072 073 if(oldHrObj!= null){ 074 //if the effective dates are the same do not create a new row just inactivate the old one 075 if(role.getEffectiveDate().equals(oldHrObj.getEffectiveDate())){ 076 oldHrObj.setActive(false); 077 oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(TKUtils.getCurrentDate().getTime()))); 078 } else{ 079 //if effective dates not the same add a new row that inactivates the old entry based on the new effective date 080 oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(TKUtils.getCurrentDate().getTime()))); 081 oldHrObj.setEffectiveDate(role.getEffectiveDate()); 082 oldHrObj.setActive(false); 083 oldHrObj.setId(null); 084 } 085 KRADServiceLocator.getBusinessObjectService().save(oldHrObj); 086 CacheUtils.flushCache(TkRole.CACHE_NAME); 087 CacheUtils.flushCache(TkRoleGroup.CACHE_NAME); 088 089 role.setTimestamp(new Timestamp(System.currentTimeMillis())); 090 role.setId(null); 091 } 092 } 093 094 TkServiceLocator.getTkRoleService().saveOrUpdate(roles); 095 trg.setRoles(rolesCopy); 096 } 097 } 098 099 @Override 100 public void processAfterEdit(MaintenanceDocument document, 101 Map<String, String[]> parameters) { 102 TkRoleGroup tkRoleGroup = (TkRoleGroup)document.getNewMaintainableObject().getBusinessObject(); 103 TkRoleGroup tkRoleGroupOld = (TkRoleGroup)document.getOldMaintainableObject().getBusinessObject(); 104 List<Job> jobs = TkServiceLocator.getJobService().getJobs(tkRoleGroup.getPrincipalId(), TKUtils.getCurrentDate()); 105 List<TkRole> positionRoles = new ArrayList<TkRole>(); 106 List<TkRole> inactivePositionRoles = new ArrayList<TkRole>(); 107 Set<String> positionNumbers = new HashSet<String>(); 108 for(Job job : jobs){ 109 positionNumbers.add(job.getPositionNumber()); 110 } 111 for(String pNo : positionNumbers){ 112 TkRole positionRole = TkServiceLocator.getTkRoleService().getRolesByPosition(pNo); 113 if(positionRole != null) 114 positionRoles.add(positionRole); 115 TkRole inactivePositionRole = TkServiceLocator.getTkRoleService().getInactiveRolesByPosition(pNo); 116 if(inactivePositionRole != null) 117 inactivePositionRoles.add(inactivePositionRole); 118 } 119 tkRoleGroup.setInactivePositionRoles(inactivePositionRoles); 120 tkRoleGroupOld.setInactivePositionRoles(inactivePositionRoles); 121 tkRoleGroup.setPositionRoles(positionRoles); 122 tkRoleGroupOld.setPositionRoles(positionRoles); 123 124 TkServiceLocator.getTkRoleGroupService().populateRoles(tkRoleGroupOld); 125 TkServiceLocator.getTkRoleGroupService().populateRoles(tkRoleGroup); 126 super.processAfterEdit(document, parameters); 127 } 128 129 @Override 130 public List getSections(MaintenanceDocument document, 131 Maintainable oldMaintainable) { 132 List sections = super.getSections(document, oldMaintainable); 133 for (Object obj : sections) { 134 Section sec = (Section) obj; 135 if (document.isOldBusinessObjectInDocument() 136 && sec.getSectionId().equals("inactiveRoles")) { 137 sec.setHidden(false); 138 } else if (!document.isOldBusinessObjectInDocument() 139 && sec.getSectionId().equals("inactiveRoles")) { 140 sec.setHidden(true); 141 } 142 } 143 return sections; 144 } 145 146 @Override 147 public HrBusinessObject getObjectById(String id) { 148 return (HrBusinessObject)TkServiceLocator.getTkRoleService().getRole(id); 149 } 150 151 }