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.time.dept.lunch.validation;
17  
18  import java.math.BigDecimal;
19  
20  import org.kuali.hr.time.dept.lunch.DeptLunchRule;
21  import org.kuali.hr.time.service.base.TkServiceLocator;
22  import org.kuali.hr.time.util.TkConstants;
23  import org.kuali.hr.time.util.ValidationUtils;
24  import org.kuali.rice.kns.document.MaintenanceDocument;
25  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
26  import org.kuali.rice.krad.bo.PersistableBusinessObject;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  
29  public class DeptLunchRuleRule extends MaintenanceDocumentRuleBase {
30  
31  	boolean validateWorkArea(DeptLunchRule ruleObj) {
32  		boolean valid = true;
33  		if (ruleObj.getWorkArea() != null
34  				&& !ValidationUtils.validateWorkArea(ruleObj.getWorkArea(), ruleObj
35  						.getEffectiveDate())) {
36  			this.putFieldError("workArea", "error.existence", "workArea '"
37  					+ ruleObj.getWorkArea() + "'");
38  			valid = false;
39  		} else if (ruleObj.getWorkArea() != null
40  				&& !ruleObj.getWorkArea().equals(TkConstants.WILDCARD_LONG)) {
41  			int count = TkServiceLocator.getWorkAreaService().getWorkAreaCount(ruleObj.getDept(), ruleObj.getWorkArea());
42  			valid = (count > 0);
43  			if (!valid) {
44  				this.putFieldError("workArea", "dept.workarea.invalid.sync",
45  						ruleObj.getWorkArea() + "");
46  			}
47  		}
48  		return valid;
49  	}
50  
51  	boolean validateDepartment(DeptLunchRule ruleObj) {
52  		if (!ValidationUtils.validateDepartment(ruleObj.getDept(), ruleObj.getEffectiveDate())) {
53  			this.putFieldError("dept", "error.existence", "department '" + ruleObj.getDept() + "'");
54  			return false;
55  		} else {
56  			return true;
57  		}
58  	}
59  
60  	boolean validateJobNumber(DeptLunchRule ruleObj) {
61  		boolean valid = true;
62  		if (ruleObj.getJobNumber() == null) {
63  			valid = false;
64  		} else if (!ruleObj.getJobNumber().equals(TkConstants.WILDCARD_LONG)) {
65  			int count = TkServiceLocator.getJobService().getJobCount(ruleObj.getPrincipalId(), ruleObj.getJobNumber(), null);
66  			valid = (count > 0);
67  			if (!valid) {
68  				this.putFieldError("jobNumber", "principalid.job.invalid.sync",
69  						ruleObj.getJobNumber() + "");
70  			}
71  		}
72  		return valid;
73  	}
74  
75  	boolean validatePrincipalId(DeptLunchRule ruleObj) {
76  		if (!ruleObj.getPrincipalId().equals(TkConstants.WILDCARD_CHARACTER)
77  				&&!ValidationUtils.validatePrincipalId(ruleObj.getPrincipalId())) {
78  			this.putFieldError("principalId", "error.existence", "Principal Id '" + ruleObj.getPrincipalId() + "'");
79  			return false;
80  		} else {
81  			return true;
82  		}
83  	}
84  
85  	boolean validateShiftHour(DeptLunchRule ruleObj) {
86  		BigDecimal shiftHour = ruleObj.getShiftHours();
87  		BigDecimal maxHour = new BigDecimal(24);
88  		if(shiftHour.compareTo(maxHour) == 1) {
89  			this.putFieldError("shiftHours", "dept.shifthour.exceedsMax");
90  			return false;
91  		}
92  		return true;
93  	}
94  
95  
96  
97  	/**
98  	 * It looks like the method that calls this class doesn't actually care
99  	 * about the return type.
100 	 */
101 	@Override
102 	protected boolean processCustomRouteDocumentBusinessRules(
103 			MaintenanceDocument document) {
104 		boolean valid = false;
105 
106 		LOG.debug("entering custom validation for DeptLunchRule");
107 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
108 		if (pbo instanceof DeptLunchRule) {
109 			DeptLunchRule deptLunchRule = (DeptLunchRule) pbo;
110 			deptLunchRule.setUserPrincipalId(GlobalVariables.getUserSession().getPrincipalId());
111 			if (deptLunchRule != null) {
112 				valid = true;
113 				valid &= this.validateDepartment(deptLunchRule);
114 				valid &= this.validateWorkArea(deptLunchRule);
115 				valid &= this.validatePrincipalId(deptLunchRule);
116 				valid &= this.validateJobNumber(deptLunchRule);
117 				valid &= this.validateShiftHour(deptLunchRule);
118 			}
119 		}
120 
121 		return valid;
122 	}
123 
124 	@Override
125 	protected boolean processCustomApproveDocumentBusinessRules(
126 			MaintenanceDocument document) {
127 		return super.processCustomApproveDocumentBusinessRules(document);
128 	}
129 }