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.lm.earncodesec.validation;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.hr.lm.earncodesec.EarnCodeSecurity;
23  import org.kuali.hr.time.roles.TkUserRoles;
24  import org.kuali.hr.time.service.base.TkServiceLocator;
25  import org.kuali.hr.time.timeblock.TimeBlock;
26  import org.kuali.hr.time.util.TkConstants;
27  import org.kuali.hr.time.util.ValidationUtils;
28  import org.kuali.rice.kns.document.MaintenanceDocument;
29  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
30  import org.kuali.rice.krad.bo.PersistableBusinessObject;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  
33  
34  public class EarnCodeSecurityRule extends MaintenanceDocumentRuleBase {
35  
36  	private 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  	private 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  	private 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  	private 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  	private 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  	private 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  	private boolean isEarnCodeUsedByActiveTimeBlocks(EarnCodeSecurity departmentEarnCode){
94  		// KPME-1106 can not deactivate a department earn code if it used in active time blocks
95  		boolean valid = true;
96  		List<TimeBlock> latestEndTimestampTimeBlocks =  TkServiceLocator.getTimeBlockService().getLatestEndTimestampForEarnCode(departmentEarnCode.getEarnCode());
97  		
98  		if ( !departmentEarnCode.isActive() && !latestEndTimestampTimeBlocks.isEmpty() && departmentEarnCode.getEffectiveDate().before(latestEndTimestampTimeBlocks.get(0).getEndDate()) ){
99  			this.putFieldError("active", "deptEarncode.deptEarncode.inactivate", departmentEarnCode.getEarnCode());
100 			return false;
101 		} 
102 		
103 		return valid;
104 		
105 	}
106 
107 	/**
108 	 * It looks like the method that calls this class doesn't actually care
109 	 * about the return type.
110 	 */
111 	@Override
112 	protected boolean processCustomRouteDocumentBusinessRules(
113 			MaintenanceDocument document) {
114 		boolean valid = false;
115 
116 		LOG.debug("entering custom validation for EarnCodeSecurity");
117 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
118 		if (pbo instanceof EarnCodeSecurity) {
119 			EarnCodeSecurity departmentEarnCode = (EarnCodeSecurity) pbo;
120 
121 			if (departmentEarnCode != null) {
122 				valid = true;
123 				valid &= this.validateSalGroup(departmentEarnCode);
124 				valid &= this.validateDept(departmentEarnCode);
125 				valid &= this.validateEarnCode(departmentEarnCode);
126 				valid &= this.validateDuplication(departmentEarnCode);
127 				valid &= this.validateLocation(departmentEarnCode);
128 				valid &= this.validateDepartmentCurrentUser(departmentEarnCode);
129 				valid &= this.isEarnCodeUsedByActiveTimeBlocks(departmentEarnCode);
130 			}
131 
132 		}
133 
134 		return valid;
135 	}
136 
137 }