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 static org.kuali.kfs.sys.businessobject.AccountingLineOverride.CODE.EXPIRED_ACCOUNT;
019 import static org.kuali.kfs.sys.businessobject.AccountingLineOverride.CODE.EXPIRED_ACCOUNT_AND_NON_FRINGE_ACCOUNT_USED;
020
021 import java.util.ArrayList;
022 import java.util.Iterator;
023 import java.util.List;
024 import java.util.Map;
025
026 import org.apache.commons.lang.StringUtils;
027 import org.kuali.kfs.coa.businessobject.Account;
028 import org.kuali.kfs.module.ld.LaborConstants ;
029 import org.kuali.kfs.module.ld.LaborKeyConstants;
030 import org.kuali.kfs.module.ld.LaborPropertyConstants;
031 import org.kuali.kfs.module.ld.businessobject.ExpenseTransferAccountingLine;
032 import org.kuali.kfs.module.ld.businessobject.ExpenseTransferSourceAccountingLine;
033 import org.kuali.kfs.module.ld.businessobject.LaborObject;
034 import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
035 import org.kuali.kfs.sys.KFSKeyConstants;
036 import org.kuali.kfs.sys.KFSPropertyConstants;
037 import org.kuali.kfs.sys.ObjectUtil;
038 import org.kuali.kfs.sys.businessobject.AccountingLine;
039 import org.kuali.kfs.sys.document.validation.GenericValidation;
040 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
041 import org.kuali.rice.kns.document.Document;
042 import org.kuali.rice.kns.util.GlobalVariables;
043 import org.kuali.rice.kns.util.KualiDecimal;
044
045 /**
046 * determine whether the given accounting line has already been in the given document
047 *
048 * @param accountingDocument the given document
049 * @param accountingLine the given accounting line
050 * @return true if the given accounting line has already been in the given document; otherwise, false
051 */
052 public class LaborExpenseTransferAccountLineTotalsMatchValidation extends GenericValidation {
053 private Document documentForValidation;
054
055 /**
056 * Validates before the document routes
057 * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
058 */
059 public boolean validate(AttributedDocumentEvent event) {
060 boolean result = true;
061
062 Document documentForValidation = getDocumentForValidation();
063
064 LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) documentForValidation;
065
066 List sourceLines = expenseTransferDocument.getSourceAccountingLines();
067 List targetLines = expenseTransferDocument.getTargetAccountingLines();
068
069 // check to ensure totals of accounting lines in source and target sections match
070 if (!isAccountingLineTotalsMatch(sourceLines, targetLines)) {
071 GlobalVariables.getMessageMap().putError(KFSPropertyConstants.SOURCE_ACCOUNTING_LINES, LaborKeyConstants.ACCOUNTING_LINE_TOTALS_MISMATCH_ERROR);
072 return false;
073 }
074
075 return result;
076 }
077
078 /**
079 * This method checks if the total sum amount of the source accounting line matches the total sum amount of the target
080 * accounting line, return true if the totals match, false otherwise.
081 *
082 * @param sourceLines
083 * @param targetLines
084 * @return
085 */
086 public boolean isAccountingLineTotalsMatch(List sourceLines, List targetLines) {
087 boolean isValid = true;
088
089 AccountingLine line = null;
090
091 // totals for the from and to lines.
092 KualiDecimal sourceLinesAmount = KualiDecimal.ZERO;
093 KualiDecimal targetLinesAmount = KualiDecimal.ZERO;
094
095 // sum source lines
096 for (Iterator i = sourceLines.iterator(); i.hasNext();) {
097 line = (ExpenseTransferAccountingLine) i.next();
098 sourceLinesAmount = sourceLinesAmount.add(line.getAmount());
099 }
100
101 // sum target lines
102 for (Iterator i = targetLines.iterator(); i.hasNext();) {
103 line = (ExpenseTransferAccountingLine) i.next();
104 targetLinesAmount = targetLinesAmount.add(line.getAmount());
105 }
106
107 // if totals don't match, then add error message
108 if (sourceLinesAmount.compareTo(targetLinesAmount) != 0) {
109 isValid = false;
110 }
111
112 return isValid;
113 }
114
115 /**
116 * Gets the documentForValidation attribute.
117 * @return Returns the documentForValidation.
118 */
119 public Document getDocumentForValidation() {
120 return documentForValidation;
121 }
122
123 /**
124 * Sets the accountingDocumentForValidation attribute value.
125 * @param documentForValidation The documentForValidation to set.
126 */
127 public void setDocumentForValidation(Document documentForValidation) {
128 this.documentForValidation = documentForValidation;
129 }
130 }