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.List;
019
020 import org.kuali.kfs.coa.businessobject.Account;
021 import org.kuali.kfs.module.ld.LaborKeyConstants;
022 import org.kuali.kfs.module.ld.businessobject.ExpenseTransferSourceAccountingLine;
023 import org.kuali.kfs.module.ld.document.BenefitExpenseTransferDocument;
024 import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
025 import org.kuali.kfs.sys.KFSPropertyConstants;
026 import org.kuali.kfs.sys.businessobject.AccountingLine;
027 import org.kuali.kfs.sys.document.AccountingDocument;
028 import org.kuali.kfs.sys.document.validation.GenericValidation;
029 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
030 import org.kuali.rice.kns.document.Document;
031 import org.kuali.rice.kns.util.GlobalVariables;
032
033 /**
034 * Validates that the given accounting line and source lines are the same
035 */
036 public class BenefitExpenseTransferSameAccountValidation extends GenericValidation {
037 private Document documentForValidation;
038 private AccountingLine accountingLineForValidation;
039
040 /**
041 * Validates that the given accounting lines in the accounting document have
042 * the same account as the source accounting lines.
043 * <strong>Expects an accounting document as the first a parameter</strong>
044 * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
045 */
046 public boolean validate(AttributedDocumentEvent event) {
047 boolean result = true;
048
049 Document documentForValidation = getDocumentForValidation() ;
050 AccountingLine accountingLine = getAccountingLineForValidation();
051
052 boolean isTargetLine = accountingLine.isTargetAccountingLine();
053 if (!isTargetLine) {
054 if (!hasSameAccount(documentForValidation, accountingLine)) {
055 GlobalVariables.getMessageMap().putError(KFSPropertyConstants.TARGET_ACCOUNTING_LINES, LaborKeyConstants.ERROR_ACCOUNT_NOT_SAME);
056 result = false;
057 }
058 }
059 return result ;
060 }
061
062 /**
063 * Determines whether the given accounting line has the same account as the source accounting lines
064 *
065 * @param document the given document
066 * @param accountingLine the given accounting line
067 * @return true if the given accounting line has the same account as the source accounting lines; otherwise, false
068 */
069 public boolean hasSameAccount(Document document, AccountingLine accountingLine) {
070 LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) document;
071 List<ExpenseTransferSourceAccountingLine> sourceAccountingLines = expenseTransferDocument.getSourceAccountingLines();
072
073 accountingLine.refreshReferenceObject(KFSPropertyConstants.ACCOUNT);
074
075 Account cachedAccount = accountingLine.getAccount();
076 for (AccountingLine sourceAccountingLine : sourceAccountingLines) {
077 Account account = sourceAccountingLine.getAccount();
078
079 // account number was not retrieved correctly, so the two statements are used to populate the fields manually
080 account.setChartOfAccountsCode(sourceAccountingLine.getChartOfAccountsCode());
081 account.setAccountNumber(sourceAccountingLine.getAccountNumber());
082
083 if (!account.equals(cachedAccount)) {
084 return false;
085 }
086 }
087
088 return true;
089 }
090
091 /**
092 * Sets the accountingDocumentForValidation attribute value.
093 * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
094 */
095 public void setDocumentForValidation(Document documentForValidation) {
096 this.documentForValidation = documentForValidation;
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 * Gets the accountingLineForValidation attribute.
109 * @return Returns the accountingLineForValidation.
110 */
111 public AccountingLine getAccountingLineForValidation() {
112 return accountingLineForValidation;
113 }
114
115 /**
116 * Sets the accountingLineForValidation attribute value.
117 * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
118 */
119 public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
120 this.accountingLineForValidation = accountingLineForValidation;
121 }
122 }