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 org.kuali.kfs.fp.businessobject.AdvanceDepositDetail;
019    import org.kuali.kfs.sys.KFSKeyConstants;
020    import org.kuali.kfs.sys.KFSPropertyConstants;
021    import org.kuali.kfs.sys.context.SpringContext;
022    import org.kuali.kfs.sys.document.validation.impl.BankCodeValidation;
023    import org.kuali.rice.kns.service.DataDictionaryService;
024    import org.kuali.rice.kns.service.DictionaryValidationService;
025    import org.kuali.rice.kns.util.GlobalVariables;
026    import org.kuali.rice.kns.util.MessageMap;
027    
028    /**
029     * Common Advance Deposit Document rule utilities.
030     */
031    public class AdvanceDepositDocumentRuleUtil {
032        /**
033         * This method method will invoke the data dictionary validation for a AdvanceDepositDetail bo instance, in addition to checking
034         * existence of the Bank and BankAccount attributes that hang off of it. This method assumes that the document hierarchy for the
035         * error map path is managed outside of this call.
036         * 
037         * @param advanceDeposit advanceDeposit object being validated
038         * @return boolean returns true if dollar amount is not 0 and bank-related references (i.e. bank and bank account) are valid
039         */
040        public static boolean validateAdvanceDeposit(AdvanceDepositDetail advanceDeposit) {
041            MessageMap errorMap = GlobalVariables.getMessageMap();
042            int originalErrorCount = errorMap.getErrorCount();
043    
044            // call the DD validation which checks basic data integrity
045            SpringContext.getBean(DictionaryValidationService.class).validateBusinessObject(advanceDeposit);
046            boolean isValid = (errorMap.getErrorCount() == originalErrorCount);
047    
048            // check that dollar amount is not zero before continuing
049            if (isValid) {
050                isValid = !advanceDeposit.getFinancialDocumentAdvanceDepositAmount().isZero();
051                if (!isValid) {
052                    String label = SpringContext.getBean(DataDictionaryService.class).getAttributeLabel(AdvanceDepositDetail.class, KFSPropertyConstants.ADVANCE_DEPOSIT_AMOUNT);
053                    errorMap.putError(KFSPropertyConstants.ADVANCE_DEPOSIT_AMOUNT, KFSKeyConstants.AdvanceDeposit.ERROR_DOCUMENT_ADVANCE_DEPOSIT_ZERO_AMOUNT, label);
054                }
055            }
056    
057            if (isValid) {
058                isValid = BankCodeValidation.validate(advanceDeposit.getFinancialDocumentBankCode(), KFSPropertyConstants.FINANCIAL_DOCUMENT_BANK_CODE, true, false);
059            }
060    
061            return isValid;
062        }
063    }