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.ld.document.validation.impl;
017    
018    import java.util.ArrayList;
019    import java.util.Iterator;
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.apache.commons.lang.StringUtils;
024    import org.kuali.kfs.coa.businessobject.Account;
025    import org.kuali.kfs.module.ld.LaborConstants ;
026    import org.kuali.kfs.module.ld.LaborKeyConstants; 
027    import org.kuali.kfs.module.ld.LaborPropertyConstants;
028    import org.kuali.kfs.module.ld.businessobject.ExpenseTransferAccountingLine;
029    import org.kuali.kfs.module.ld.businessobject.ExpenseTransferSourceAccountingLine;
030    import org.kuali.kfs.module.ld.businessobject.LaborObject;
031    import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
032    import org.kuali.kfs.module.ld.document.SalaryExpenseTransferDocument;
033    import org.kuali.kfs.sys.KFSKeyConstants;
034    import org.kuali.kfs.sys.KFSPropertyConstants;
035    import org.kuali.kfs.sys.ObjectUtil;
036    import org.kuali.kfs.sys.businessobject.AccountingLine;
037    import org.kuali.kfs.sys.document.validation.GenericValidation;
038    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
039    import org.kuali.rice.kns.document.Document;
040    import org.kuali.rice.kns.util.GlobalVariables;
041    import org.kuali.rice.kns.util.KualiDecimal;
042    import org.kuali.rice.kns.util.ObjectUtils;
043    
044    /**
045     * determine whether the given accounting line has already been in the given document
046     * 
047     * @param accountingDocument the given document
048     * @param accountingLine the given accounting line
049     * @return true if the given accounting line has already been in the given document; otherwise, false
050     */
051    public class LaborExpenseTransferValidAccountValidation extends GenericValidation {
052        private Document documentForValidation;
053        
054        /**
055         * Validates before the document routes 
056         * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
057         */
058        public boolean validate(AttributedDocumentEvent event) {
059            boolean result = true;
060                   
061            Document documentForValidation = getDocumentForValidation();
062            
063            LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) documentForValidation;
064            
065            // check to ensure the accounts in source/target accounting lines are valid
066            if (!isValidAccount(expenseTransferDocument)) {
067                return false;
068            }
069    
070            return result;       
071        }
072    
073        /**
074         * Determine whether the accounts in source/target accounting lines are valid
075         * 
076         * @param accountingDocument the given accounting document
077         * @return true if the accounts in source/target accounting lines are valid; otherwise, false
078         */
079        public boolean isValidAccount(LaborExpenseTransferDocumentBase expenseTransferDocument) {
080    
081            for (Object sourceAccountingLine : expenseTransferDocument.getSourceAccountingLines()) {
082                AccountingLine line = (AccountingLine) sourceAccountingLine;
083                if (ObjectUtils.isNull(line.getAccount())) {
084                    GlobalVariables.getMessageMap().putError(KFSPropertyConstants.SOURCE_ACCOUNTING_LINES, KFSKeyConstants.ERROR_DOCUMENT_GLOBAL_ACCOUNT_INVALID_ACCOUNT, new String[] { line.getChartOfAccountsCode(), line.getAccountNumber() });
085                    return false;
086                }
087            }
088    
089            for (Object targetAccountingLine : expenseTransferDocument.getTargetAccountingLines()) {
090                AccountingLine line = (AccountingLine) targetAccountingLine;
091                if (ObjectUtils.isNull(line.getAccount())) {
092                    GlobalVariables.getMessageMap().putError(KFSPropertyConstants.TARGET_ACCOUNTING_LINES, KFSKeyConstants.ERROR_DOCUMENT_GLOBAL_ACCOUNT_INVALID_ACCOUNT, new String[] { line.getChartOfAccountsCode(), line.getAccountNumber() });
093                    return false;
094                }
095            }
096            return true;
097        }
098    
099        /**
100         * Gets the documentForValidation attribute. 
101         * @return Returns the documentForValidation.
102         */
103        public Document getDocumentForValidation() {
104            return documentForValidation;
105        }
106    
107        /**
108         * Sets the accountingDocumentForValidation attribute value.
109         * @param documentForValidation The documentForValidation to set.
110         */
111        public void setDocumentForValidation(Document documentForValidation) {
112            this.documentForValidation = documentForValidation;
113        }    
114    }