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