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.kpme.core.paystep.validation;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.kpme.core.institution.Institution;
20  import org.kuali.kpme.core.paygrade.PayGrade;
21  import org.kuali.kpme.core.paystep.PayStep;
22  import org.kuali.kpme.core.salarygroup.SalaryGroup;
23  import org.kuali.kpme.core.service.HrServiceLocator;
24  import org.kuali.kpme.core.util.ValidationUtils;
25  import org.kuali.rice.krad.maintenance.MaintenanceDocument;
26  import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
27  
28  @SuppressWarnings("deprecation")
29  public class PayStepValidation extends MaintenanceDocumentRuleBase {
30  
31  	@Override
32  	protected boolean processCustomRouteDocumentBusinessRules(
33  			MaintenanceDocument document) {
34  		LOG.debug("entering custom validation for pay step");
35  		boolean isValid = super.processCustomRouteDocumentBusinessRules(document);
36  
37  		PayStep payStep = (PayStep) this.getNewDataObject();
38  		
39  		isValid &= validateInstitution(payStep);
40  		isValid &= validateLocation(payStep);
41  		isValid &= validateSalaryGroup(payStep);
42  		isValid &= validatePayGrade(payStep);
43  		isValid &= validatePayGradeInSalaryGroup(payStep);
44  		
45  		return isValid;
46  	}
47  
48  	private boolean validatePayGrade(PayStep payStep) {
49  		PayGrade aPayGrade = HrServiceLocator.getPayGradeService().getPayGrade(payStep.getPayGrade(), payStep.getSalaryGroup(), payStep.getEffectiveLocalDate());
50  		String errorMes = "Pay Grade '" + payStep.getPayGrade() + "'";
51  		if(aPayGrade == null) {
52  			this.putFieldError("dataObject.payGrade", "error.existence", errorMes);
53  			return false;
54  		} else {
55  			if(!ValidationUtils.wildCardMatch(aPayGrade.getInstitution(), payStep.getInstitution())) {
56  				String[] params = new String[3];
57  				params[0] = payStep.getInstitution();
58  				params[1] = aPayGrade.getInstitution();
59  				params[2] = errorMes;
60  				this.putFieldError("dataObject.institution", "institution.inconsistent", params);
61  				return false;
62  			}
63  			if(!ValidationUtils.wildCardMatch(aPayGrade.getLocation(), payStep.getLocation())) {
64  				String[] params = new String[3];
65  				params[0] = payStep.getLocation();
66  				params[1] = aPayGrade.getLocation();
67  				params[2] = errorMes;
68  				this.putFieldError("dataObject.location", "location.inconsistent", params);
69  				return false;
70  			}
71  		}
72  		return true;
73  	}
74  
75  	private boolean validateSalaryGroup(PayStep payStep) {
76  		SalaryGroup aSalGroup = HrServiceLocator.getSalaryGroupService().getSalaryGroup(payStep.getSalaryGroup(), payStep.getEffectiveLocalDate());
77  		String errorMes = "SalaryGroup '" + payStep.getSalaryGroup() + "'";
78  		if(aSalGroup != null) {
79  			if(!ValidationUtils.wildCardMatch(aSalGroup.getInstitution(), payStep.getInstitution())) {
80  				String[] params = new String[3];
81  				params[0] = payStep.getInstitution();
82  				params[1] = aSalGroup.getInstitution();
83  				params[2] = errorMes;
84  				this.putFieldError("dataObject.institution", "institution.inconsistent", params);
85  				return false;
86  			}
87  			if(!ValidationUtils.wildCardMatch(aSalGroup.getLocation(), payStep.getLocation())) {
88  				String[] params = new String[3];
89  				params[0] = payStep.getLocation();
90  				params[1] = aSalGroup.getLocation();
91  				params[2] = errorMes;
92  				this.putFieldError("dataObject.location", "location.inconsistent", params);
93  				return false;
94  			}
95  		} else {
96  			this.putFieldError("dataObject.salaryGroup", "error.existence", errorMes);
97  			return false;
98  		}
99  		
100 		return true;
101 	}
102 	
103 	private boolean validatePayGradeInSalaryGroup(PayStep payStep) {
104 		if(StringUtils.isNotEmpty(payStep.getSalaryGroup())
105 				&& ValidationUtils.validatePayGradeWithSalaryGroup(payStep.getSalaryGroup(),payStep.getPayGrade(),payStep.getEffectiveLocalDate())) {
106 			return true;
107 		} else {
108 			String[] params = new String[2];
109 			params[0] = payStep.getPayGrade();
110 			params[1] = payStep.getSalaryGroup();
111 			
112 			this.putFieldError("dataObject.payGrade", "salaryGroup.contains.payGrade", params);
113 			return false;
114 		}
115 	}
116 
117 	private boolean validateLocation(PayStep payStep) {
118 		if (StringUtils.isNotEmpty(payStep.getLocation())
119 				&& ValidationUtils.validateLocation(payStep.getLocation(), payStep.getEffectiveLocalDate())) {
120 			return true;
121 		} else {
122 			this.putFieldError("dataObject.location", "error.existence", "Location '"
123 					+ payStep.getLocation() + "'");
124 			return false;
125 		}
126 	}
127 
128 	private boolean validateInstitution(PayStep payStep) {
129 		if (StringUtils.isNotEmpty(payStep.getInstitution())
130 				&& ValidationUtils.validateInstitution(payStep.getInstitution(), payStep.getEffectiveLocalDate())) {
131 			return true;
132 		} else {
133 			this.putFieldError("dataObject.institution", "error.existence", "Instituion '"
134 					+ payStep.getInstitution() + "'");
135 			return false;
136 		}
137 	}
138 
139 }