001    /*
002     * Copyright 2011 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.kfs.module.cam.document.validation.impl;
017    
018    import java.sql.Date;
019    import java.util.HashMap;
020    import java.util.Map;
021    
022    import org.kuali.kfs.module.cam.CamsKeyConstants;
023    import org.kuali.kfs.module.cam.CamsPropertyConstants;
024    import org.kuali.kfs.module.cam.businessobject.AssetPaymentDetail;
025    import org.kuali.kfs.module.cam.document.AssetPaymentDocument;
026    import org.kuali.kfs.module.cam.document.service.AssetPaymentService;
027    import org.kuali.kfs.sys.KFSKeyConstants;
028    import org.kuali.kfs.sys.KFSPropertyConstants;
029    import org.kuali.kfs.sys.businessobject.AccountingLine;
030    import org.kuali.kfs.sys.businessobject.UniversityDate;
031    import org.kuali.kfs.sys.document.validation.GenericValidation;
032    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
033    import org.kuali.kfs.sys.service.UniversityDateService;
034    import org.kuali.rice.kns.service.BusinessObjectService;
035    import org.kuali.rice.kns.service.DataDictionaryService;
036    import org.kuali.rice.kns.service.DateTimeService;
037    import org.kuali.rice.kns.util.GlobalVariables;
038    
039    /**
040     * This class validates asset payment posting date
041     */
042    public class AssetPaymentPostingDateValidation extends GenericValidation {
043    
044        private AccountingLine accountingLineForValidation;
045        private DateTimeService dateTimeService;
046        private BusinessObjectService businessObjectService;
047        private DataDictionaryService dataDictionaryService;
048        private UniversityDateService universityDateService;
049        private AssetPaymentService assetPaymentService;
050    
051    
052        /**
053         * Validates asset payment posting date
054         * 
055         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
056         */
057        public boolean validate(AttributedDocumentEvent event) {
058            // skip check if accounting line is from CAB
059            AssetPaymentDocument assetPaymentDocument = (AssetPaymentDocument) event.getDocument();
060            if (assetPaymentDocument.isCapitalAssetBuilderOriginIndicator()) {
061                return true;
062            }
063            
064            boolean valid = true;
065            AssetPaymentDetail assetPaymentDetail = (AssetPaymentDetail) getAccountingLineForValidation();
066            Date expenditureFinancialDocumentPostedDate = assetPaymentDetail.getExpenditureFinancialDocumentPostedDate();
067            if (expenditureFinancialDocumentPostedDate != null) {
068                // check if date is after today
069                java.util.Date today = dateTimeService.getCurrentDate();
070                if (expenditureFinancialDocumentPostedDate.after(today)) {
071                    GlobalVariables.getMessageMap().putError(CamsPropertyConstants.AssetPaymentDetail.DOCUMENT_POSTING_DATE, CamsKeyConstants.Payment.ERROR_POSTING_DATE_FUTURE_NOT_ALLOWED);
072                    return false;
073                }
074                Map<String, Object> keyToFind = new HashMap<String, Object>();
075                keyToFind.put(KFSPropertyConstants.UNIVERSITY_DATE, expenditureFinancialDocumentPostedDate);
076                UniversityDate universityDate = (UniversityDate) businessObjectService.findByPrimaryKey(UniversityDate.class, keyToFind);
077    
078                if (universityDate == null) {
079                    String label = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(AssetPaymentDetail.class.getName()).getAttributeDefinition(CamsPropertyConstants.AssetPaymentDetail.DOCUMENT_POSTING_DATE).getLabel();
080                    GlobalVariables.getMessageMap().putError(CamsPropertyConstants.AssetPaymentDetail.DOCUMENT_POSTING_DATE, KFSKeyConstants.ERROR_EXISTENCE, label);
081                    return false;
082                }
083                
084                // Validating the fiscal year extracted from posted document date is not greater than the current fiscal year.
085                Integer currentFiscalYear = universityDateService.getCurrentFiscalYear();
086                if (universityDate.getUniversityFiscalYear().compareTo(currentFiscalYear) > 0) {
087                    GlobalVariables.getMessageMap().putError(CamsPropertyConstants.AssetPaymentDetail.DOCUMENT_POSTING_DATE, CamsKeyConstants.Payment.ERROR_INVALID_DOC_POST_DATE);
088                    valid = false;
089                }
090                if (valid) {
091                    assetPaymentService.extractPostedDatePeriod(assetPaymentDetail);
092                }
093            }
094            else {
095                String label = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(AssetPaymentDetail.class.getName()).getAttributeDefinition(CamsPropertyConstants.AssetPaymentDetail.DOCUMENT_POSTING_DATE).getLabel();
096                GlobalVariables.getMessageMap().putError(CamsPropertyConstants.AssetPaymentDetail.DOCUMENT_POSTING_DATE, KFSKeyConstants.ERROR_REQUIRED, label);
097                valid = false;
098            }
099            return valid;
100        }
101    
102        public AccountingLine getAccountingLineForValidation() {
103            return accountingLineForValidation;
104        }
105    
106        public void setAccountingLineForValidation(AccountingLine accountingLine) {
107            this.accountingLineForValidation = accountingLine;
108        }
109    
110        public DateTimeService getDateTimeService() {
111            return dateTimeService;
112        }
113    
114        public void setDateTimeService(DateTimeService dateTimeService) {
115            this.dateTimeService = dateTimeService;
116        }
117    
118        public BusinessObjectService getBusinessObjectService() {
119            return businessObjectService;
120        }
121    
122        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
123            this.businessObjectService = businessObjectService;
124        }
125    
126        public DataDictionaryService getDataDictionaryService() {
127            return dataDictionaryService;
128        }
129    
130        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
131            this.dataDictionaryService = dataDictionaryService;
132        }
133    
134        public UniversityDateService getUniversityDateService() {
135            return universityDateService;
136        }
137    
138        public void setUniversityDateService(UniversityDateService universityDateService) {
139            this.universityDateService = universityDateService;
140        }
141    
142        public AssetPaymentService getAssetPaymentService() {
143            return assetPaymentService;
144        }
145    
146        public void setAssetPaymentService(AssetPaymentService assetPaymentService) {
147            this.assetPaymentService = assetPaymentService;
148        }
149    }