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.roles;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.core.cache.CacheUtils;
20  import org.kuali.hr.job.Job;
21  import org.kuali.hr.time.HrBusinessObject;
22  import org.kuali.hr.time.service.base.TkServiceLocator;
23  import org.kuali.hr.time.util.HrBusinessObjectMaintainableImpl;
24  import org.kuali.hr.time.util.TKUtils;
25  import org.kuali.hr.time.util.TkConstants;
26  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
27  import org.kuali.rice.kns.document.MaintenanceDocument;
28  import org.kuali.rice.kns.maintenance.Maintainable;
29  import org.kuali.rice.kns.web.ui.Section;
30  import org.kuali.rice.krad.bo.BusinessObject;
31  import org.kuali.rice.krad.service.KRADServiceLocator;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  
34  import java.sql.Timestamp;
35  import java.util.*;
36  
37  public class TkRoleGroupMaintainableImpl extends HrBusinessObjectMaintainableImpl {
38  
39      /**
40  	 * 
41  	 */
42  	private static final long serialVersionUID = 1L;
43  
44  	@Override
45      public void saveBusinessObject() {
46          BusinessObject bo = this.getBusinessObject();
47          if (bo instanceof TkRoleGroup) {
48              TkRoleGroup trg = (TkRoleGroup)bo;
49              List<TkRole> roles = trg.getRoles();
50      		List<TkRole> rolesCopy = new ArrayList<TkRole>();
51      		rolesCopy.addAll(roles);
52      		if (trg.getInactiveRoles() != null
53      				&& trg.getInactiveRoles().size() > 0) {
54      			for (TkRole role : trg.getInactiveRoles()) {
55      				roles.add(role);
56      			}
57      		}
58              for (TkRole role : roles) {
59                  if (StringUtils.equals(role.getRoleName(), TkConstants.ROLE_TK_SYS_ADMIN)) {
60                      String principalId = role.getPrincipalId();
61                      if(StringUtils.isBlank(principalId)){
62                      	principalId = trg.getPrincipalId();
63                      }
64                      if(StringUtils.isBlank(principalId)){
65                      	KimApiServiceLocator.getRoleService().assignPrincipalToRole(principalId, TkConstants.ROLE_NAMESAPCE, role.getRoleName(), new HashMap<String,String>());
66                      }
67                  }
68                  role.setPrincipalId(trg.getPrincipalId());
69                  role.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
70                  
71                  HrBusinessObject oldHrObj = this.getObjectById(role.getId());
72                  
73      			if(oldHrObj!= null){
74      				//if the effective dates are the same do not create a new row just inactivate the old one
75      				if(role.getEffectiveDate().equals(oldHrObj.getEffectiveDate())){
76      					oldHrObj.setActive(false);
77      					oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(TKUtils.getCurrentDate().getTime()))); 
78      				} else{
79      					//if effective dates not the same add a new row that inactivates the old entry based on the new effective date
80      					oldHrObj.setTimestamp(TKUtils.subtractOneSecondFromTimestamp(new Timestamp(TKUtils.getCurrentDate().getTime())));
81      					oldHrObj.setEffectiveDate(role.getEffectiveDate());
82      					oldHrObj.setActive(false);
83      					oldHrObj.setId(null);
84      				}
85      				KRADServiceLocator.getBusinessObjectService().save(oldHrObj);
86                      CacheUtils.flushCache(TkRole.CACHE_NAME);
87                      CacheUtils.flushCache(TkRoleGroup.CACHE_NAME);
88  
89      				role.setTimestamp(new Timestamp(System.currentTimeMillis()));
90      				role.setId(null);
91      			}
92              }
93       
94              TkServiceLocator.getTkRoleService().saveOrUpdate(roles);
95              trg.setRoles(rolesCopy);
96          }
97      }
98  
99  	@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 }