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.List;
020
021 import org.apache.commons.lang.StringUtils;
022 import org.kuali.kfs.module.ld.LaborConstants ;
023 import org.kuali.kfs.module.ld.LaborKeyConstants;
024 import org.kuali.kfs.module.ld.LaborPropertyConstants;
025 import org.kuali.kfs.module.ld.businessobject.ExpenseTransferAccountingLine;
026 import org.kuali.kfs.module.ld.businessobject.ExpenseTransferSourceAccountingLine;
027 import org.kuali.kfs.module.ld.businessobject.LaborObject;
028 import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
029 import org.kuali.kfs.module.ld.document.SalaryExpenseTransferDocument;
030 import org.kuali.kfs.sys.KFSPropertyConstants;
031 import org.kuali.kfs.sys.ObjectUtil;
032 import org.kuali.kfs.sys.businessobject.AccountingLine;
033 import org.kuali.kfs.sys.document.AccountingDocument;
034 import org.kuali.kfs.sys.document.validation.GenericValidation;
035 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
036 import org.kuali.rice.kns.util.GlobalVariables;
037
038 /**
039 * determine whether the given accounting line has already been in the given document
040 *
041 * @param accountingDocument the given document
042 * @param accountingLine the given accounting line
043 * @return true if the given accounting line has already been in the given document; otherwise, false
044 */
045 public class LaborExpenseTransferDuplicateSourceAccountingLineValidation extends GenericValidation {
046 private AccountingLine accountingLineForValidation;
047 private AccountingDocument accountingDocumentForValidation;
048
049 /**
050 * Validates that an accounting line does not duplicate in the source lines
051 * <strong>Expects an accounting line as the first a parameter</strong>
052 * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
053 */
054 public boolean validate(AttributedDocumentEvent event) {
055 boolean result = true;
056 AccountingDocument accountingDocument = getAccountingDocumentForValidation();
057 AccountingLine accountingLine = getAccountingLineForValidation();
058
059 // not allow the duplicate source accounting line in the document
060 if (isDuplicateSourceAccountingLine(accountingDocument, accountingLine)) {
061 GlobalVariables.getMessageMap().putError(KFSPropertyConstants.SOURCE_ACCOUNTING_LINES, LaborKeyConstants.ERROR_DUPLICATE_SOURCE_ACCOUNTING_LINE );
062 return false;
063 }
064
065 return result;
066 }
067
068 /**
069 * determine whether the given accounting line has already been in the given document
070 *
071 * @param accountingDocument the given document
072 * @param accountingLine the given accounting line
073 * @return true if the given accounting line has already been in the given document; otherwise, false
074 */
075 protected boolean isDuplicateSourceAccountingLine(AccountingDocument accountingDocument, AccountingLine accountingLine) {
076 // only check source accounting lines
077 if (!(accountingLine instanceof ExpenseTransferSourceAccountingLine)) {
078 return false;
079 }
080
081 LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) accountingDocument;
082 List<ExpenseTransferSourceAccountingLine> sourceAccountingLines = expenseTransferDocument.getSourceAccountingLines();
083 List<String> key = defaultKeyOfExpenseTransferAccountingLine();
084
085 int counter = 0;
086 for (AccountingLine sourceAccountingLine : sourceAccountingLines) {
087 boolean isExisting = ObjectUtil.equals(accountingLine, sourceAccountingLine, key);
088 counter = isExisting ? counter + 1 : counter;
089
090 if (counter > 1) {
091 return true;
092 }
093 }
094 return false;
095 }
096
097 /**
098 * Gets the default key of ExpenseTransferAccountingLine
099 *
100 * @return the default key of ExpenseTransferAccountingLine
101 */
102 protected List<String> defaultKeyOfExpenseTransferAccountingLine() {
103 List<String> defaultKey = new ArrayList<String>();
104
105 defaultKey.add(KFSPropertyConstants.POSTING_YEAR);
106 defaultKey.add(KFSPropertyConstants.CHART_OF_ACCOUNTS_CODE);
107 defaultKey.add(KFSPropertyConstants.ACCOUNT_NUMBER);
108 defaultKey.add(KFSPropertyConstants.SUB_ACCOUNT_NUMBER);
109
110 defaultKey.add(KFSPropertyConstants.BALANCE_TYPE_CODE);
111 defaultKey.add(KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
112 defaultKey.add(KFSPropertyConstants.FINANCIAL_SUB_OBJECT_CODE);
113
114 defaultKey.add(KFSPropertyConstants.EMPLID);
115 defaultKey.add(KFSPropertyConstants.POSITION_NUMBER);
116
117 defaultKey.add(LaborPropertyConstants.PAYROLL_END_DATE_FISCAL_YEAR);
118 defaultKey.add(LaborPropertyConstants.PAYROLL_END_DATE_FISCAL_PERIOD_CODE);
119
120 return defaultKey;
121 }
122
123 /**
124 * Gets the accountingLineForValidation attribute.
125 * @return Returns the accountingLineForValidation.
126 */
127 public AccountingDocument getAccountingDocumentForValidation() {
128 return accountingDocumentForValidation;
129 }
130
131 /**
132 * Sets the accountingDocumentForValidation attribute value.
133 * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
134 */
135 public void setAccountingDocumentForValidation(AccountingDocument accountingDocumentForValidation) {
136 this.accountingDocumentForValidation = accountingDocumentForValidation;
137 }
138
139 /**
140 * Gets the accountingLineForValidation attribute.
141 * @return Returns the accountingLineForValidation.
142 */
143 public AccountingLine getAccountingLineForValidation() {
144 return accountingLineForValidation;
145 }
146
147 /**
148 * Sets the accountingLineForValidation attribute value.
149 * @param accountingLineForValidation The accountingLineForValidation to set.
150 */
151 public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
152 this.accountingLineForValidation = accountingLineForValidation;
153 }
154 }