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.time.earncode.validation;
017    
018    import org.apache.commons.lang.StringUtils;
019    import org.kuali.hr.time.earncode.EarnCode;
020    import org.kuali.hr.time.service.base.TkServiceLocator;
021    import org.kuali.hr.time.timeblock.TimeBlock;
022    import org.kuali.hr.time.util.TkConstants;
023    import org.kuali.hr.time.util.ValidationUtils;
024    import org.kuali.rice.kns.document.MaintenanceDocument;
025    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
026    
027    import java.math.BigDecimal;
028    import java.sql.Date;
029    import java.util.List;
030    
031    public class EarnCodeValidation extends MaintenanceDocumentRuleBase{
032            
033            @Override
034            protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
035                    EarnCode earnCode = (EarnCode)this.getNewBo();
036                    EarnCode oldEarnCode = (EarnCode)this.getOldBo();
037                    if ((StringUtils.equals(oldEarnCode.getEarnCode(), TkConstants.LUNCH_EARN_CODE) 
038                                    || StringUtils.equals(oldEarnCode.getEarnCode(), TkConstants.HOLIDAY_EARN_CODE))
039                                            && !earnCode.isActive()) {
040                            this.putFieldError("active", "earncode.inactivate.locked", earnCode
041                                            .getEarnCode());
042                    }
043                    //if earn code is not designated how to record then throw error
044                    if (earnCode.getHrEarnCodeId() == null) {
045                            if (ValidationUtils.validateEarnCode(earnCode.getEarnCode(), null)) {
046                                    // If there IS an earn code, ie, it is valid, we need to report
047                                    // an error as earn codes must be unique.                       
048                                    this.putFieldError("earnCode", "earncode.earncode.unique");
049                                    return false;
050                            }
051                    }
052                    
053    //              if(earnCode.getRecordAmount() == false && 
054    //                      earnCode.getRecordHours() == false && 
055    //                      earnCode.getRecordTime() == false){
056    //                      this.putFieldError("recordTime", "earncode.record.not.specified");
057    //                      return false;
058    //              }
059    //              //confirm that only one of the check boxes is checked
060    //              //if more than one are true then throw an error
061    //              if(earnCode.getRecordAmount() && earnCode.getRecordHours() && earnCode.getRecordTime()){
062    //                      this.putFieldError("recordTime", "earncode.record.unique");
063    //                      return false;
064    //              }
065    //              boolean result = earnCode.getRecordAmount() ^ earnCode.getRecordHours() ^ earnCode.getRecordTime();
066    //              if(!result){
067    //                      this.putFieldError("recordTime", "earncode.record.unique");
068    //                      return false;
069    //              }
070    
071                    //check if the effective date of the accrual category is prior to effective date of the earn code 
072                    //accrual category is an optional field
073                    if(StringUtils.isNotEmpty(earnCode.getAccrualCategory())){
074                            if (!ValidationUtils.validateAccrualCategory(earnCode.getAccrualCategory(), earnCode.getEffectiveDate())) {
075                                    this.putFieldError("accrualCategory", "earncode.accrualCategory.invalid", earnCode.getAccrualCategory());
076                                    return false;
077                            }
078                    }
079                    
080                    // check if there's a newer version of the Earn Code
081                    int count = TkServiceLocator.getEarnCodeService().getNewerEarnCodeCount(earnCode.getEarnCode(), earnCode.getEffectiveDate());
082                    if(count > 0) {
083                            this.putFieldError("effectiveDate", "earncode.effectiveDate.newer.exists");
084                            return false;
085                    }
086                    
087    //              //check if the ovtEarnCode and InflateMinHours is equal to 0
088    //              if((earnCode.getRecordAmount() || earnCode.getOvtEarnCode()) && earnCode.getInflateMinHours().compareTo(new BigDecimal(0))!=0){
089    //                      this.putFieldError("inflateMinHours", "earncode.inflateminhours.should.be.zero");
090    //                      return false;
091    //              }
092    //              
093    //              //check if the RecordAmount and AccrualCategory has no value
094    //              if(earnCode.getRecordAmount() && StringUtils.isNotBlank(earnCode.getAccrualCategory())){
095    //                      this.putFieldError("accrualCategory", "earncode.accrualcategory.should.be.blank");
096    //                      return false;
097    //              }
098    //              
099    //              //check if the RecordAmount and InflateFactor is equal to 1
100    //              if(earnCode.getRecordAmount() && (earnCode.getInflateFactor().compareTo(new BigDecimal(1))!=0)){
101    //                      this.putFieldError("inflateFactor", "earncode.inflatefactor.should.be.one");
102    //                      return false;
103    //              }
104                    
105                    // kpme-937 can not inactivation of a earn code if it used in active timeblocks
106                    List<TimeBlock> latestEndTimestampTimeBlocks =  TkServiceLocator.getTimeBlockService().getLatestEndTimestamp();
107                    
108                    if ( !earnCode.isActive() && earnCode.getEffectiveDate().before(latestEndTimestampTimeBlocks.get(0).getEndDate()) ){
109                            List<TimeBlock> activeTimeBlocks = TkServiceLocator.getTimeBlockService().getTimeBlocksWithEarnCode(earnCode.getEarnCode(), earnCode.getEffectiveDate());
110                            if(activeTimeBlocks != null && !activeTimeBlocks.isEmpty()) {
111                                    this.putFieldError("earnCode", "earncode.earncode.inactivate", earnCode.getEarnCode());
112                                    return false;
113                            }
114                    }
115                    
116                    return true;
117            }
118    
119    }