View Javadoc

1   /**
2    * Copyright 2004-2014 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.kpme.tklm.time.rules.overtime.daily.validation;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.kpme.core.service.HrServiceLocator;
20  import org.kuali.kpme.core.util.HrConstants;
21  import org.kuali.kpme.core.util.ValidationUtils;
22  import org.kuali.kpme.tklm.time.rules.overtime.daily.DailyOvertimeRule;
23  import org.kuali.rice.kns.document.MaintenanceDocument;
24  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
25  import org.kuali.rice.krad.bo.PersistableBusinessObject;
26  import org.kuali.rice.krad.util.GlobalVariables;
27  
28  public class DailyOvertimeRuleRule extends MaintenanceDocumentRuleBase {
29  
30  	boolean validateWorkArea(DailyOvertimeRule ruleObj) {
31  		boolean valid = true;
32  		if (!ValidationUtils.validateWorkArea(ruleObj.getWorkArea(), ruleObj
33  				.getEffectiveLocalDate())) {
34  			this.putFieldError("workArea", "error.existence", "workArea '"
35  					+ ruleObj.getWorkArea() + "'");
36  			valid = false;
37  		} else if (!ruleObj.getWorkArea().equals(HrConstants.WILDCARD_LONG)) {
38  			int count= HrServiceLocator.getWorkAreaService().getWorkAreaCount(ruleObj.getDept(), ruleObj.getWorkArea());
39  			valid = (count > 0);
40  			if (!valid) {
41  				this.putFieldError("workArea", "dept.workarea.invalid.sync",
42  						ruleObj.getWorkArea() + "");
43  			}
44  		}
45  		return valid;
46  	}
47  
48  	boolean validateDepartment(DailyOvertimeRule ruleObj) {
49  		if (ruleObj.getDept() != null
50  				&& !ruleObj.getDept().equals(HrConstants.WILDCARD_CHARACTER)
51  				&& !ValidationUtils.validateDepartment(ruleObj.getDept(),
52  						ruleObj.getEffectiveLocalDate())) {
53  			this.putFieldError("dept", "error.existence", "department '"
54  					+ ruleObj.getDept() + "'");
55  			return false;
56  		} else {
57  			return true;
58  		}
59  	}
60  
61  	boolean validateEarnCode(DailyOvertimeRule dailyOvertimeRule) {
62  		if (dailyOvertimeRule.getEarnCode() != null
63  				&& !ValidationUtils.validateEarnCode(dailyOvertimeRule
64  						.getEarnCode(), dailyOvertimeRule.getEffectiveLocalDate())) {
65  			this.putFieldError("earnCode", "error.existence", "earnCode '"
66  					+ dailyOvertimeRule.getEarnCode() + "'");
67  			return false;
68  		} else {
69  			if (dailyOvertimeRule.getEarnCode() != null
70  					&& !ValidationUtils.validateEarnCode(dailyOvertimeRule
71  							.getEarnCode(), true, dailyOvertimeRule
72  							.getEffectiveLocalDate())) {
73  				this.putFieldError("earnCode", "earncode.ovt.required",
74  						dailyOvertimeRule.getEarnCode());
75  				return false;
76  			}
77  			return true;
78  		}
79  	}
80  
81  	boolean validateEarnGroup(DailyOvertimeRule dailyOvertimeRule) {
82  		if (dailyOvertimeRule.getFromEarnGroup() != null
83  				&& !ValidationUtils.validateEarnGroup(dailyOvertimeRule
84  						.getFromEarnGroup(), dailyOvertimeRule
85  						.getEffectiveLocalDate())) {
86  			this.putFieldError("fromEarnGroup", "error.existence",
87  					"from EarnCodeGroup '" + dailyOvertimeRule.getFromEarnGroup()
88  							+ "'");
89  			return false;
90  		}
91  		if (!StringUtils.isEmpty(dailyOvertimeRule.getFromEarnGroup())
92  				&& ValidationUtils.earnGroupHasOvertimeEarnCodes(dailyOvertimeRule.getFromEarnGroup(), dailyOvertimeRule.getEffectiveLocalDate())){
93  			this.putFieldError("fromEarnGroup", "earngroup.earncode.overtime", dailyOvertimeRule.getFromEarnGroup());
94  			return false;
95  		}
96  		return true;
97  	}
98  	
99  	boolean validateLocation(DailyOvertimeRule dailyOvertimeRule) {
100 		if (dailyOvertimeRule.getLocation() != null
101 				&& !ValidationUtils.validateLocation(dailyOvertimeRule
102 						.getLocation(), dailyOvertimeRule.getEffectiveLocalDate())) {
103 			this.putFieldError("location", "error.existence", "location '"
104 					+ dailyOvertimeRule.getLocation() + "'");
105 			return false;
106 		} else {
107 			return true;
108 		}
109 	}
110 
111 	boolean validatePayType(DailyOvertimeRule dailyOvertimeRule) {
112 		if (dailyOvertimeRule.getPaytype() != null
113 				&& !dailyOvertimeRule.getPaytype().equals(
114 						HrConstants.WILDCARD_CHARACTER)
115 				&& !ValidationUtils.validatePayType(dailyOvertimeRule
116 						.getPaytype(), dailyOvertimeRule.getEffectiveLocalDate())) {
117 			this.putFieldError("paytype", "error.existence", "paytype '"
118 					+ dailyOvertimeRule.getPaytype() + "'");
119 			return false;
120 		} else {
121 			return true;
122 		}
123 	}
124 
125 	/**
126 	 * It looks like the method that calls this class doesn't actually care
127 	 * about the return type.
128 	 */
129 	@Override
130 	protected boolean processCustomRouteDocumentBusinessRules(
131 			MaintenanceDocument document) {
132 		boolean valid = false;
133 
134 		LOG.debug("entering custom validation for DailyOvertimeRule");
135 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
136 		if (pbo instanceof DailyOvertimeRule) {
137 			DailyOvertimeRule dailyOvertimeRule = (DailyOvertimeRule) pbo;
138 			dailyOvertimeRule.setUserPrincipalId(GlobalVariables
139 					.getUserSession().getPrincipalId());
140 			if (dailyOvertimeRule != null) {
141 				valid = true;
142 				valid &= this.validateLocation(dailyOvertimeRule);
143 				valid &= this.validatePayType(dailyOvertimeRule);
144 				valid &= this.validateDepartment(dailyOvertimeRule);
145 				valid &= this.validateWorkArea(dailyOvertimeRule);
146 				valid &= this.validateEarnCode(dailyOvertimeRule);
147 				valid &= this.validateEarnGroup(dailyOvertimeRule);
148 			}
149 		}
150 
151 		return valid;
152 	}
153 
154 }