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.fp.document.validation.impl;
017    
018    import static org.kuali.kfs.sys.KFSConstants.DOCUMENT_ERRORS;
019    import static org.kuali.kfs.sys.KFSKeyConstants.ERROR_DOCUMENT_ACCOUNTING_PERIOD_CLOSED;
020    
021    import org.kuali.kfs.coa.businessobject.AccountingPeriod;
022    import org.kuali.kfs.coa.service.AccountingPeriodService;
023    import org.kuali.kfs.fp.document.AuxiliaryVoucherDocument;
024    import org.kuali.kfs.sys.document.validation.GenericValidation;
025    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
026    import org.kuali.rice.kns.util.GlobalVariables;
027    
028    /**
029     * Validates that the accounting period given by the document is currently open
030     */
031    public class AuxiliaryVoucherAccountingPeriodOpenValidation extends GenericValidation {
032        private AuxiliaryVoucherDocument auxliaryVoucherDocumentForValidation;
033        private AccountingPeriodService accountingPeriodService;
034    
035        /**
036         * Uses the accounting period service to get the accounting period for the document and checks that it's open
037         * @see org.kuali.kfs.sys.document.validation.Validation#validate(org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent)
038         */
039        public boolean validate(AttributedDocumentEvent event) {
040            AccountingPeriod acctPeriod = getAccountingPeriodService().getByPeriod(auxliaryVoucherDocumentForValidation.getPostingPeriodCode(), auxliaryVoucherDocumentForValidation.getPostingYear());
041            
042            //  can't post into a closed period
043            if (acctPeriod == null || acctPeriod.isActive()) {
044                GlobalVariables.getMessageMap().putError(DOCUMENT_ERRORS, ERROR_DOCUMENT_ACCOUNTING_PERIOD_CLOSED);
045                return false;
046            }
047            
048            return true;
049        }
050    
051        /**
052         * Gets the accountingPeriodService attribute. 
053         * @return Returns the accountingPeriodService.
054         */
055        public AccountingPeriodService getAccountingPeriodService() {
056            return accountingPeriodService;
057        }
058    
059        /**
060         * Sets the accountingPeriodService attribute value.
061         * @param accountingPeriodService The accountingPeriodService to set.
062         */
063        public void setAccountingPeriodService(AccountingPeriodService accountingPeriodService) {
064            this.accountingPeriodService = accountingPeriodService;
065        }
066    
067        /**
068         * Gets the auxliaryVoucherDocumentForValidation attribute. 
069         * @return Returns the auxliaryVoucherDocumentForValidation.
070         */
071        public AuxiliaryVoucherDocument getAuxliaryVoucherDocumentForValidation() {
072            return auxliaryVoucherDocumentForValidation;
073        }
074    
075        /**
076         * Sets the auxliaryVoucherDocumentForValidation attribute value.
077         * @param auxliaryVoucherDocumentForValidation The auxliaryVoucherDocumentForValidation to set.
078         */
079        public void setAuxliaryVoucherDocumentForValidation(AuxiliaryVoucherDocument auxliaryVoucherDocumentForValidation) {
080            this.auxliaryVoucherDocumentForValidation = auxliaryVoucherDocumentForValidation;
081        }
082    }