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.principal.validation;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.kpme.core.principal.PrincipalHRAttributes;
20  import org.kuali.kpme.core.util.ValidationUtils;
21  import org.kuali.rice.kns.document.MaintenanceDocument;
22  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
23  import org.kuali.rice.krad.bo.PersistableBusinessObject;
24  
25  public class PrincipalHRAttributesRule extends MaintenanceDocumentRuleBase {
26  
27  	private boolean validatePrincipalId(PrincipalHRAttributes principalHRAttr) {
28  		if (principalHRAttr.getPrincipalId() != null
29  				&& !ValidationUtils.validatePrincipalId(principalHRAttr
30  						.getPrincipalId())) {
31  			this.putFieldError("principalId", "error.existence",
32  					"principalId '" + principalHRAttr.getPrincipalId() + "'");
33  			return false;
34  		} else {
35  			return true;
36  		}
37  	}
38  	
39  	private boolean validatePayCalendar(PrincipalHRAttributes principalHRAttr) {
40  		if (principalHRAttr.getPayCalendar() != null
41  				&& !ValidationUtils.validateCalendarByType(principalHRAttr.getPayCalendar(), "Pay")) {
42  			this.putFieldError("payCalendar", "error.existence",
43  					"Pay Calendar '" + principalHRAttr.getPayCalendar() + "'");
44  			return false;
45  		} else {
46  			return true;
47  		}
48  	}
49  	
50  	private boolean validateLeaveCalendar(PrincipalHRAttributes principalHRAttr) {
51  		if (principalHRAttr.getLeaveCalendar() != null
52  				&& !ValidationUtils.validateCalendarByType(principalHRAttr.getLeaveCalendar(), "Leave")) {
53  			this.putFieldError("leaveCalendar", "error.existence",
54  					"Leave Calendar '" + principalHRAttr.getLeaveCalendar() + "'");
55  			return false;
56          } else {
57  		    return true;
58          }
59  	}
60  
61  	private boolean validateLeavePlan(PrincipalHRAttributes principalHRAttr) {
62          if (principalHRAttr.getLeavePlan() != null
63  				&& !ValidationUtils.validateLeavePlan(principalHRAttr.getLeavePlan(), null)) {
64  			this.putFieldError("leavePlan", "error.existence",
65  					"leavePlan '" + principalHRAttr.getLeavePlan() + "'");
66  			return false;
67  		} else {
68  			return true;
69  		}
70  	}
71  
72      private boolean validateServiceDate(PrincipalHRAttributes principalHRAttr) {
73          if (StringUtils.isNotEmpty(principalHRAttr.getLeavePlan())
74                  && principalHRAttr.getServiceDate() == null) {
75              this.putFieldError("leavePlan", "validation.prerequisite", "'Service Date'");
76              return false;
77          }
78          return true;
79      }
80  
81      private boolean validateLeavefields(PrincipalHRAttributes principalHRAttr){
82          if (StringUtils.isNotEmpty(principalHRAttr.getLeavePlan()) ||
83                  StringUtils.isNotEmpty(principalHRAttr.getLeaveCalendar()) ||
84                  principalHRAttr.getServiceDate() != null) {
85              if (StringUtils.isNotEmpty(principalHRAttr.getLeavePlan()) &&
86                      StringUtils.isNotEmpty(principalHRAttr.getLeaveCalendar()) &&
87                      principalHRAttr.getServiceDate() != null) {
88                  return true;
89              } else {
90                  if (!StringUtils.isNotEmpty(principalHRAttr.getLeavePlan())){
91                      this.putFieldError("leavePlan","error.principalHrAttibutes.leavePlan.notPresent");
92                  }
93                  if (!StringUtils.isNotEmpty(principalHRAttr.getLeaveCalendar())){
94                      this.putFieldError("leaveCalendar","error.principalHrAttibutes.leaveCalendar.notPresent");
95                  }
96                  if (principalHRAttr.getServiceDate() == null){
97                      this.putFieldError("serviceDate","error.principalHrAttibutes.serviceDate.notPresent");
98                  }
99                      return false;
100             }
101         } else {
102             return true;
103         }
104     }
105 
106 	boolean validateEffectiveDate(PrincipalHRAttributes principalHRAttr) {
107 		boolean valid = true;
108 		if (principalHRAttr.getEffectiveDate() != null && !ValidationUtils.validateOneYearFutureDate(principalHRAttr.getEffectiveLocalDate())) {
109 			this.putFieldError("effectiveDate", "error.date.exceed.year", "Effective Date");
110 			valid = false;
111 		}
112 		return valid;
113 	}
114 	
115 	@Override
116 	protected boolean processCustomRouteDocumentBusinessRules(
117 			MaintenanceDocument document) {
118 		boolean valid = false;
119 
120 
121 		LOG.debug("entering custom validation for Job");
122 		PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
123 		if (pbo instanceof PrincipalHRAttributes) {
124 			PrincipalHRAttributes principalHRAttr = (PrincipalHRAttributes) pbo;
125 			if (principalHRAttr != null) {
126 				valid = true;
127 				valid &= this.validatePrincipalId(principalHRAttr);
128 				// KPME-1442 Kagata
129 				//valid &= this.validateEffectiveDate(principalHRAttr);
130 				valid &= this.validatePayCalendar(principalHRAttr);
131 				valid &= this.validateLeaveCalendar(principalHRAttr);
132 				valid &= this.validateLeavePlan(principalHRAttr);
133 //                valid &= this.validateServiceDate(principalHRAttr);
134                 valid &= this.validateLeavefields(principalHRAttr);
135 			}
136 		}
137 		return valid;
138 	}
139 
140 }