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.timecollection.validation;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.hr.kpme.tklm.time.rules.validation.TkKeyedBusinessObjectValidation;
20  import org.kuali.kpme.core.util.HrConstants;
21  import org.kuali.kpme.core.util.ValidationUtils;
22  import org.kuali.kpme.tklm.time.rules.timecollection.TimeCollectionRule;
23  import org.kuali.rice.kns.document.MaintenanceDocument;
24  import org.kuali.rice.krad.bo.PersistableBusinessObject;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  
27  public class TimeCollectionRuleValidation extends TkKeyedBusinessObjectValidation {
28  
29  	boolean validateWorkArea(TimeCollectionRule ruleObj) {
30  		if (!ValidationUtils.validateWorkArea(ruleObj.getWorkArea(), ruleObj.getDept(), ruleObj.getEffectiveLocalDate())) {
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.getGroupKeyCode(), ruleObj.getEffectiveLocalDate())) {
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(HrConstants.WILDCARD_CHARACTER)) {
50  			return true;
51  		}		
52  		if (!ValidationUtils.validatePayType(ruleObj.getPayType(), ruleObj.getEffectiveLocalDate())) {
53  			this.putFieldError("payType", "error.existence", "payType '" + ruleObj.getPayType() + "'");
54  			return false;
55  		} else {
56  			return true;
57  		}
58  	}
59  
60  	public static boolean validateWorkAreaDeptWildcarding(TimeCollectionRule tcr) {
61          return StringUtils.equals(tcr.getDept(), HrConstants.WILDCARD_CHARACTER)
62                  && HrConstants.WILDCARD_LONG.equals(tcr.getWorkArea());
63      }
64  	
65  	boolean validateWildcards(TimeCollectionRule tcr) {
66  
67          if(validateWorkAreaDeptWildcarding(tcr)){
68  			// add error when work area defined, department is wild carded.
69  			this.putFieldError("workArea", "error.wc.wadef");
70  			return false;
71  		}
72          
73          if(validateGroupKeyDeptWildcarding(tcr)){
74          	// add error when dept is defined, groupkey is wild carded.
75  			this.putFieldError("groupKeyCode", "error.wc.deptdef");
76  			return false;
77  		}
78          
79  		return true;
80      }
81  	
82  	private boolean validateGroupKeyDeptWildcarding(TimeCollectionRule tcr) {
83          return StringUtils.equals(tcr.getGroupKeyCode(), HrConstants.WILDCARD_CHARACTER)
84                  && StringUtils.equals(tcr.getDept(), HrConstants.WILDCARD_CHARACTER);
85      }
86  
87  	/**
88  	 * It looks like the method that calls this class doesn't actually care
89  	 * about the return type.
90  	 */
91  	@Override
92  	protected boolean processCustomRouteDocumentBusinessRules(
93  			MaintenanceDocument document) {
94  		boolean valid = false;
95  		LOG.debug("entering custom validation for TimeCollectionRule");
96  		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
97  
98  
99  
100 		if (pbo instanceof TimeCollectionRule) {
101 			TimeCollectionRule timeCollectionRule = (TimeCollectionRule) pbo;
102 			timeCollectionRule.setUserPrincipalId(GlobalVariables.getUserSession().getLoggedInUserPrincipalName());
103 			if (timeCollectionRule != null) {
104 				valid = true;
105 				valid &= this.validateWildcards(timeCollectionRule);
106 				valid &= this.validateDepartment(timeCollectionRule);
107 				valid &= this.validateWorkArea(timeCollectionRule);
108 				valid &= this.validatePayType(timeCollectionRule);
109 				valid &= this.validateGroupKeyCode(timeCollectionRule);
110 			}
111 		}
112 
113 		return valid;
114 	}
115 
116 }