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.timeoff.validation;
017    
018    import java.math.BigDecimal;
019    import java.sql.Date;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.hr.lm.timeoff.SystemScheduledTimeOff;
023    import org.kuali.hr.time.earncode.EarnCode;
024    import org.kuali.hr.time.service.base.TkServiceLocator;
025    import org.kuali.hr.time.util.TkConstants;
026    import org.kuali.hr.time.util.ValidationUtils;
027    import org.kuali.rice.kns.document.MaintenanceDocument;
028    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
029    import org.kuali.rice.krad.bo.PersistableBusinessObject;
030    
031    public class SystemScheduledTimeOffValidation extends MaintenanceDocumentRuleBase {      
032            boolean validateEffectiveDate(Date effectiveDate) {
033                    boolean valid = true;
034                    if (effectiveDate != null && !ValidationUtils.validateOneYearFutureDate(effectiveDate)) {
035                            this.putFieldError("effectiveDate", "error.date.exceed.year", "Effective Date");
036                            valid = false;
037                    }
038                    return valid;
039            }
040    
041            boolean validateAccruedDate(Date accruedDate) {
042                    boolean valid = true;
043                    if (accruedDate != null && !ValidationUtils.validateFutureDate(accruedDate)) {
044                            this.putFieldError("accruedDate", "error.future.date", "Accrued Date");
045                            valid = false;
046                    }
047                    return valid;
048            }
049            
050            boolean validateLocation(String location) {
051                    boolean valid = true;
052                    if(!StringUtils.isEmpty(location) && location.equals(TkConstants.WILDCARD_CHARACTER)) {
053                            return true;
054                    }
055                    if (!ValidationUtils.validateLocation(location, null)) {
056                            this.putFieldError("location", "error.existence", "location '"
057                                            + location + "'");
058                            valid = false;
059                    }
060                    return valid;
061            }
062            
063            boolean validateScheduledTimeOffDate(Date scheduledTimeOffDate) {
064                    boolean valid = true;
065                    if (scheduledTimeOffDate!= null && !ValidationUtils.validateFutureDate(scheduledTimeOffDate)) {
066                            this.putFieldError("scheduledTimeOffDate", "error.future.date", "Scheduled Time Off Date");
067                            valid = false;
068                    }
069                    return valid;
070            }
071            
072            boolean validateUnusedTimeForScheduledTimeOffDate(Date scheduledTimeOffDate, String unusedTime) {
073                    boolean valid = true;
074                    if (scheduledTimeOffDate == null && (StringUtils.isEmpty(unusedTime) || StringUtils.equals("T", unusedTime) || StringUtils.equals("NUTA", unusedTime))) {
075                            this.putFieldError("unusedTime", "error.unusedtime.bank.required", "Unused Time");                      
076                            valid = false;
077                    }               
078                    return valid;
079            }
080                    
081            boolean validateTransfertoEarnCode(String transfertoEarnCode) {
082                    boolean valid = true;
083                    if (StringUtils.isEmpty(transfertoEarnCode)) {
084                            this.putFieldError("transfertoEarnCode", "error.required", "Transfer to Earn Code");
085                            valid = false;
086                    }
087                    return valid;
088            }
089            
090            boolean validateTransferConversionFactor(BigDecimal transferConversionFactor) {
091                    boolean valid = true;
092                    if (transferConversionFactor == null) {
093                            this.putFieldError("transferConversionFactor", "error.required", "Transfer Conversion Factor");
094                            valid = false;
095                    }
096                    return valid;
097            }
098            
099            boolean validateEarnCode(String earnCode, Date asOfDate) {
100                    boolean valid = true;
101                    if (!ValidationUtils.validateEarnCode(earnCode, asOfDate)) {
102                            this.putFieldError("earnCode", "error.existence", "earnCode '"
103                                            + earnCode + "'");
104                            valid = false;
105                    }
106                    return valid;
107            }
108    
109        private boolean validateFraction(String earnCode, BigDecimal amount, Date asOfDate, String fieldName) {
110            boolean valid = true;
111            if (!ValidationUtils.validateEarnCodeFraction(earnCode, amount, asOfDate)) {
112                EarnCode ec = TkServiceLocator.getEarnCodeService().getEarnCode(earnCode, asOfDate);
113                if(ec != null && ec.getFractionalTimeAllowed() != null) {
114                    BigDecimal fracAllowed = new BigDecimal(ec.getFractionalTimeAllowed());
115                    String[] parameters = new String[2];
116                    parameters[0] = earnCode;
117                    parameters[1] = Integer.toString(fracAllowed.scale());
118                    this.putFieldError(fieldName, "error.amount.fraction", parameters);
119                    valid = false;
120                }
121            }
122            return valid;
123        }
124    
125            @Override
126            protected boolean processCustomRouteDocumentBusinessRules(
127                            MaintenanceDocument document) {
128                    boolean valid = false;
129                    LOG.debug("entering custom validation for SystemScheduledTimeOff");
130                    PersistableBusinessObject pbo = (PersistableBusinessObject) this.getNewBo();
131                    if (pbo instanceof SystemScheduledTimeOff) {
132                            SystemScheduledTimeOff sysSchTimeOff = (SystemScheduledTimeOff) pbo;
133                            if (sysSchTimeOff != null) {
134                                    valid = true;
135                                    valid &= this.validateEffectiveDate(sysSchTimeOff.getEffectiveDate());
136                                    valid &= this.validateAccruedDate(sysSchTimeOff.getAccruedDate());
137                                    valid &= this.validateScheduledTimeOffDate(sysSchTimeOff.getScheduledTimeOffDate());
138                    valid &= this.validateFraction(sysSchTimeOff.getEarnCode(),sysSchTimeOff.getAmountofTime(),sysSchTimeOff.getEffectiveDate(),"amountofTime");
139                                    valid &= this.validateUnusedTimeForScheduledTimeOffDate(sysSchTimeOff.getScheduledTimeOffDate(), sysSchTimeOff.getUnusedTime());
140                                    valid &= this.validateLocation(sysSchTimeOff.getLocation());
141                                    valid &= this.validateEarnCode(sysSchTimeOff.getEarnCode(), sysSchTimeOff.getEffectiveDate());
142                            }
143                    }
144                    
145                    return valid;
146            }
147    }