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.collection.rule.validation;
17  
18  import org.apache.cxf.common.util.StringUtils;
19  import org.kuali.hr.time.collection.rule.TimeCollectionRule;
20  import org.kuali.hr.time.util.TkConstants;
21  import org.kuali.hr.time.util.ValidationUtils;
22  import org.kuali.rice.kns.document.MaintenanceDocument;
23  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
24  import org.kuali.rice.krad.bo.PersistableBusinessObject;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  
27  public class TimeCollectionRuleRule extends MaintenanceDocumentRuleBase {
28  
29  	boolean validateWorkArea(TimeCollectionRule ruleObj) {
30  		if (!ValidationUtils.validateWorkArea(ruleObj.getWorkArea(), ruleObj.getDept(), ruleObj.getEffectiveDate())) {
31  			this.putFieldError("workArea", "error.existence", "workarea '" + ruleObj.getWorkArea() + "'");
32  			return false;
33  		} else {
34  			return true;
35  		}
36  	}
37  
38  	boolean validateDepartment(TimeCollectionRule ruleObj) {
39  		if (!ValidationUtils.validateDepartment(ruleObj.getDept(), ruleObj.getEffectiveDate())) {
40  			this.putFieldError("dept", "error.existence", "department '" + ruleObj.getDept() + "'");
41  			return false;
42  		} else {
43  			return true;
44  		}
45  	}
46  	
47  	// JIRA1152
48  	boolean validatePayType(TimeCollectionRule ruleObj) {
49  		if(!StringUtils.isEmpty(ruleObj.getPayType()) && ruleObj.getPayType().equals(TkConstants.WILDCARD_CHARACTER)) {
50  			return true;
51  		}		
52  		if (!ValidationUtils.validatePayType(ruleObj.getPayType(), ruleObj.getEffectiveDate())) {
53  			this.putFieldError("payType", "error.existence", "payType '" + ruleObj.getPayType() + "'");
54  			return false;
55  		} else {
56  			return true;
57  		}
58  	}
59  	
60  	boolean validateClockUserAndHrsDistFlags(TimeCollectionRule ruleObj) {
61  		if (!ruleObj.isClockUserFl() && ruleObj.isHrsDistributionF()) {
62  			this.putFieldError("hrsDistributionF", "timecollRule.hrDistribution.invalid");
63  			return false;
64  		}
65  		return true;
66  	}
67  
68  	/**
69  	 * It looks like the method that calls this class doesn't actually care
70  	 * about the return type.
71  	 */
72  	@Override
73  	protected boolean processCustomRouteDocumentBusinessRules(
74  			MaintenanceDocument document) {
75  		boolean valid = false;
76  		LOG.debug("entering custom validation for TimeCollectionRule");
77  		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
78  
79  
80  
81  		if (pbo instanceof TimeCollectionRule) {
82  			TimeCollectionRule timeCollectionRule = (TimeCollectionRule) pbo;
83  			timeCollectionRule.setUserPrincipalId(GlobalVariables.getUserSession().getLoggedInUserPrincipalName());
84  			if (timeCollectionRule != null) {
85  				valid = true;
86  				valid &= this.validateDepartment(timeCollectionRule);
87  				valid &= this.validateWorkArea(timeCollectionRule);
88  				valid &= this.validatePayType(timeCollectionRule);
89  				valid &= this.validateClockUserAndHrsDistFlags(timeCollectionRule);
90  			}
91  		}
92  
93  		return valid;
94  	}
95  
96  }