001 /** 002 * Copyright 2004-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.hr.lm.leaveplan.validation; 017 018 import java.sql.Date; 019 import java.text.ParseException; 020 import java.text.SimpleDateFormat; 021 import java.util.List; 022 023 import org.apache.commons.lang.StringUtils; 024 import org.kuali.hr.lm.leaveplan.LeavePlan; 025 import org.kuali.hr.time.principal.PrincipalHRAttributes; 026 import org.kuali.hr.time.service.base.TkServiceLocator; 027 import org.kuali.hr.time.util.ValidationUtils; 028 import org.kuali.rice.kns.document.MaintenanceDocument; 029 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase; 030 import org.kuali.rice.krad.bo.PersistableBusinessObject; 031 032 public class LeavePlanValidation extends MaintenanceDocumentRuleBase { 033 034 // KPME-1250 Kagata 035 // This method determines if the leave plan can be inactivated 036 boolean validateInactivation(LeavePlan leavePlan) { 037 boolean valid = true; 038 // Get a list of active employees based on leave plan and its effective 039 // date. 040 // If the list is not null, there are active employees and the leave 041 // plan can't be inactivated, so return false otherwise true 042 if (!leavePlan.isActive()) { 043 // this has to use the effective date of the job passed in 044 List<PrincipalHRAttributes> pList = TkServiceLocator 045 .getPrincipalHRAttributeService() 046 .getActiveEmployeesForLeavePlan(leavePlan.getLeavePlan(), 047 leavePlan.getEffectiveDate()); 048 049 if (pList != null && pList.size() > 0) { 050 // error.leaveplan.inactivate=Can not inactivate leave plan {0}. 051 // There are active employees in the plan. 052 this.putFieldError("active", "error.leaveplan.inactivate", 053 leavePlan.getLeavePlan()); 054 valid = false; 055 } 056 } 057 058 return valid; 059 } 060 061 // KPME-1407 Kagata 062 boolean validatePlanningMonths(String planningMonths) { 063 boolean valid = true; 064 if (planningMonths != null) { 065 int iPlanningMonths = Integer.parseInt(planningMonths); 066 // error.leaveplan.planningMonths='{0}' should be between 1 and 24. 067 if (iPlanningMonths > 24 || iPlanningMonths <= 0) { 068 this.putFieldError("planningMonths", 069 "error.leaveplan.planningMonths", "Planning Months"); 070 valid = false; 071 } 072 } 073 return valid; 074 } 075 076 boolean validateEffectiveDate(Date effectiveDate) { 077 boolean valid = true; 078 valid = ValidationUtils.validateOneYearFutureEffectiveDate(effectiveDate); 079 if(!valid) { 080 this.putFieldError("effectiveDate", "error.date.exceed.year", "Effective Date"); 081 } 082 return valid; 083 } 084 085 boolean validateCalendarYearStart(String dateString) { 086 if (StringUtils.isBlank(dateString)) { 087 return false; 088 } 089 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd"); 090 sdf.setLenient(false); 091 try { 092 sdf.parse(dateString); 093 } catch (ParseException e) { 094 this.putFieldError("calendarYearStart", 095 "error.calendar.year.start", "Calendar Year Start"); 096 return false; 097 } 098 return true; 099 } 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.getEffectiveDate()); 120 } 121 valid &= this.validateCalendarYearStart(leavePlan 122 .getCalendarYearStart()); 123 } 124 } 125 return valid; 126 } 127 }