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.overtime.daily.rule.validation;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.time.overtime.daily.rule.DailyOvertimeRule;
20  import org.kuali.hr.time.service.base.TkServiceLocator;
21  import org.kuali.hr.time.util.TkConstants;
22  import org.kuali.hr.time.util.ValidationUtils;
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  				.getEffectiveDate())) {
34  			this.putFieldError("workArea", "error.existence", "workArea '"
35  					+ ruleObj.getWorkArea() + "'");
36  			valid = false;
37  		} else if (!ruleObj.getWorkArea().equals(TkConstants.WILDCARD_LONG)) {
38  			int count= TkServiceLocator.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(TkConstants.WILDCARD_CHARACTER)
51  				&& !ValidationUtils.validateDepartment(ruleObj.getDept(),
52  						ruleObj.getEffectiveDate())) {
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.getEffectiveDate())) {
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  							.getEffectiveDate())) {
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  						.getEffectiveDate())) {
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.getEffectiveDate())){
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.getEffectiveDate())) {
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 						TkConstants.WILDCARD_CHARACTER)
115 				&& !ValidationUtils.validatePayType(dailyOvertimeRule
116 						.getPaytype(), dailyOvertimeRule.getEffectiveDate())) {
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 }