View Javadoc

1   /**
2    * Copyright 2004-2012 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.earncodesec.validation;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.earncodesec.EarnCodeSecurity;
20  import org.kuali.hr.time.roles.TkUserRoles;
21  import org.kuali.hr.time.service.base.TkServiceLocator;
22  import org.kuali.hr.time.timeblock.TimeBlock;
23  import org.kuali.hr.time.util.TkConstants;
24  import org.kuali.hr.time.util.ValidationUtils;
25  import org.kuali.rice.kns.document.MaintenanceDocument;
26  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
27  import org.kuali.rice.krad.bo.PersistableBusinessObject;
28  import org.kuali.rice.krad.util.GlobalVariables;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  
34  public class EarnCodeSecurityRule extends MaintenanceDocumentRuleBase {
35  
36  	boolean validateSalGroup(EarnCodeSecurity departmentEarnCode ) {
37  		if (!ValidationUtils.validateSalGroup(departmentEarnCode.getHrSalGroup(), departmentEarnCode.getEffectiveDate())) {
38  			this.putFieldError("hrSalGroup", "error.existence", "Salgroup '" + departmentEarnCode.getHrSalGroup()+ "'");
39  			return false;
40  		} else {
41  			return true;
42  		}
43  	}
44  
45  	boolean validateDept(EarnCodeSecurity clr) {
46  		if (!ValidationUtils.validateDepartment(clr.getDept(), clr.getEffectiveDate()) && !StringUtils.equals(clr.getDept(), TkConstants.WILDCARD_CHARACTER)) {
47  			this.putFieldError("dept", "error.existence", "department '" + clr.getDept() + "'");
48  			return false;
49  		} else {
50  			return true;
51  		}
52  	}
53  
54  	boolean validateEarnCode(EarnCodeSecurity departmentEarnCode ) {
55  		if (!ValidationUtils.validateEarnCode(departmentEarnCode.getEarnCode(), departmentEarnCode.getEffectiveDate())) {
56  			this.putFieldError("earnCode", "error.existence", "Earncode '" + departmentEarnCode.getEarnCode()+ "'");
57  			return false;
58  		} else {
59  			return true;
60  		}
61  	}
62  
63  	boolean validateDuplication(EarnCodeSecurity departmentEarnCode) {
64  		if(ValidationUtils.duplicateDeptEarnCodeExists(departmentEarnCode)) {
65  			this.putFieldError("effectiveDate", "deptEarncode.duplicate.exists");
66  			return false;
67  		} else {
68  			return true;
69  		}
70  	}
71  
72  	boolean validateLocation(EarnCodeSecurity departmentEarnCode) {
73  		if (departmentEarnCode.getLocation() != null
74  				&& !ValidationUtils.validateLocation(departmentEarnCode.getLocation(), null) && 
75  				!StringUtils.equals(departmentEarnCode.getLocation(), TkConstants.WILDCARD_CHARACTER)) {
76  			this.putFieldError("location", "error.existence", "location '"
77  					+ departmentEarnCode.getLocation() + "'");
78  			return false;
79  		} else {
80  			return true;
81  		}
82  	}
83  	
84  	boolean validateDepartmentCurrentUser(EarnCodeSecurity departmentEarnCode) {
85  		if (!TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).isSystemAdmin() && !TkUserRoles.getUserRoles(GlobalVariables.getUserSession().getPrincipalId()).getOrgAdminDepartments().contains(departmentEarnCode.getDept())) {
86  			this.putFieldError("dept", "error.department.permissions", departmentEarnCode.getDept());
87  			return false;
88  		} else {
89  			return true;
90  		}
91  	}
92  	
93  	boolean isEarnCodeUsedByActiveTimeBlocks(EarnCodeSecurity departmentEarnCode){
94  		// KPME-1106 can not inactivation of a department earn code if it used in active time blocks
95  		boolean valid = true;
96  		List<TimeBlock> latestEndTimestampTimeBlocks =  TkServiceLocator.getTimeBlockService().getLatestEndTimestamp();
97  		
98  		if ( !departmentEarnCode.isActive() && departmentEarnCode.getEffectiveDate().before(latestEndTimestampTimeBlocks.get(0).getEndDate()) ){
99  			List<TimeBlock> activeTimeBlocks = new ArrayList<TimeBlock>();
100 			activeTimeBlocks = TkServiceLocator.getTimeBlockService().getTimeBlocks();
101 			for(TimeBlock activeTimeBlock : activeTimeBlocks){
102 				if ( departmentEarnCode.getEarnCode().equals(activeTimeBlock.getEarnCode())){
103 					this.putFieldError("active", "deptEarncode.deptEarncode.inactivate", departmentEarnCode.getEarnCode());
104 					return  false;
105 				}
106 			}
107 		} 
108 		
109 		return valid;
110 		
111 	}
112 
113 	/**
114 	 * It looks like the method that calls this class doesn't actually care
115 	 * about the return type.
116 	 */
117 	@Override
118 	protected boolean processCustomRouteDocumentBusinessRules(
119 			MaintenanceDocument document) {
120 		boolean valid = false;
121 
122 		LOG.debug("entering custom validation for EarnCodeSecurity");
123 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
124 		if (pbo instanceof EarnCodeSecurity) {
125 			EarnCodeSecurity departmentEarnCode = (EarnCodeSecurity) pbo;
126 
127 			if (departmentEarnCode != null) {
128 				valid = true;
129 				valid &= this.validateSalGroup(departmentEarnCode);
130 				valid &= this.validateDept(departmentEarnCode);
131 				valid &= this.validateEarnCode(departmentEarnCode);
132 				valid &= this.validateDuplication(departmentEarnCode);
133 				valid &= this.validateLocation(departmentEarnCode);
134 				valid &= this.validateDepartmentCurrentUser(departmentEarnCode);
135 				valid &= this.isEarnCodeUsedByActiveTimeBlocks(departmentEarnCode);
136 			}
137 
138 		}
139 
140 		return valid;
141 	}
142 
143 }