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.leaveplan.validation;
17  
18  import java.text.ParseException;
19  import java.text.SimpleDateFormat;
20  import java.util.List;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.joda.time.LocalDate;
24  import org.kuali.kpme.core.leaveplan.LeavePlan;
25  import org.kuali.kpme.core.principal.PrincipalHRAttributes;
26  import org.kuali.kpme.core.service.HrServiceLocator;
27  import org.kuali.kpme.core.util.ValidationUtils;
28  import org.kuali.rice.kns.document.MaintenanceDocument;
29  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
30  import org.kuali.rice.krad.bo.PersistableBusinessObject;
31  
32  public class LeavePlanValidation extends MaintenanceDocumentRuleBase {
33  
34  	// KPME-1250 Kagata
35  	// This method determines if the leave plan can be inactivated
36  	boolean validateInactivation(LeavePlan leavePlan) {
37  		boolean valid = true;
38  		// Get a list of active employees based on leave plan and its effective
39  		// date.
40  		// If the list is not null, there are active employees and the leave
41  		// plan can't be inactivated, so return false otherwise true
42  		if (!leavePlan.isActive()) {
43  			// this has to use the effective date of the job passed in
44  			List<PrincipalHRAttributes> pList = HrServiceLocator
45  					.getPrincipalHRAttributeService()
46  					.getActiveEmployeesForLeavePlan(leavePlan.getLeavePlan(),
47  							leavePlan.getEffectiveLocalDate());
48  
49  			if (pList != null && pList.size() > 0) {
50  				// error.leaveplan.inactivate=Can not inactivate leave plan {0}.
51  				// There are active employees in the plan.
52  				this.putFieldError("active", "error.leaveplan.inactivate",
53  						leavePlan.getLeavePlan());
54  				valid = false;
55  			}
56  		}
57  
58  		return valid;
59  	}
60  
61  	// KPME-1407 Kagata
62  	boolean validatePlanningMonths(String planningMonths) {
63  		boolean valid = true;
64  		if (planningMonths != null) {
65  			int iPlanningMonths = Integer.parseInt(planningMonths);
66  			// error.leaveplan.planningMonths='{0}' should be between 1 and 24.
67  			if (iPlanningMonths > 24 || iPlanningMonths <= 0) {
68  				this.putFieldError("planningMonths",
69  						"error.leaveplan.planningMonths", "Planning Months");
70  				valid = false;
71  			}
72  		}
73  		return valid;
74  	}
75  
76  	boolean validateEffectiveDate(LocalDate effectiveDate) {
77  		boolean valid = true;
78  		valid = ValidationUtils.validateOneYearFutureEffectiveDate(effectiveDate);
79  		if(!valid) {
80  			this.putFieldError("effectiveDate", "error.date.exceed.year", "Effective Date");
81  		} 
82  		return valid;
83  	}
84  
85  	boolean validateCalendarYearStart(String dateString) {
86  		if (StringUtils.isBlank(dateString)) {
87  			return false;
88  		}
89  		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd");
90  		sdf.setLenient(false);
91  		try {
92  			sdf.parse(dateString);
93  		} catch (ParseException e) {
94  			this.putFieldError("calendarYearStart",
95  					"error.calendar.year.start", "Calendar Year Start");
96  			return false;
97  		}
98  		return true;
99  	}
100 
101 	@Override
102 	protected boolean processCustomRouteDocumentBusinessRules(
103 			MaintenanceDocument document) {
104 		boolean valid = true;
105 		LOG.debug("entering custom validation for Leave Plan");
106 		PersistableBusinessObject pbo = (PersistableBusinessObject) this
107 				.getNewBo();
108 		if (pbo instanceof LeavePlan) {
109 			LeavePlan leavePlan = (LeavePlan) pbo;
110 			if (leavePlan != null) {
111 				valid = true;
112 				valid &= this.validateInactivation(leavePlan);
113 				if (StringUtils.isNotEmpty(leavePlan.getPlanningMonths())) {
114 					valid &= this.validatePlanningMonths(leavePlan
115 							.getPlanningMonths());
116 				}
117 				if (leavePlan.getEffectiveDate() != null) {
118 					valid &= this.validateEffectiveDate(
119 							leavePlan.getEffectiveLocalDate());
120 				}
121 				valid &= this.validateCalendarYearStart(leavePlan
122 						.getCalendarYearStart());
123 			}
124 		}
125 		return valid;
126 	}
127 }