View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.ld.document.validation.impl;
20  
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  import org.kuali.kfs.module.ld.LaborKeyConstants;
25  import org.kuali.kfs.module.ld.document.LaborExpenseTransferDocumentBase;
26  import org.kuali.kfs.sys.KFSPropertyConstants;
27  import org.kuali.kfs.sys.businessobject.AccountingLine;
28  import org.kuali.kfs.sys.document.AccountingDocument;
29  import org.kuali.kfs.sys.document.validation.GenericValidation;
30  import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
31  import org.kuali.rice.krad.document.Document;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  
34  /**
35   * benefit transfers cannot be made between two different fringe benefit labor object codes 
36   */
37  public class BenefitExpenseTransferSameFringeBenefitObjectCodeValidation extends GenericValidation {
38      private Document documentForValidation;
39      
40      /**
41       * Validates that the accounting lines in the accounting document have the same employee id 
42       * <strong>Expects an accounting document as the first a parameter</strong>
43       * @see org.kuali.kfs.validation.Validation#validate(java.lang.Object[])
44       */
45      public boolean validate(AttributedDocumentEvent event) {
46          boolean result = true;
47          
48          Document documentForValidation = getDocumentForValidation();
49          
50          AccountingDocument accountingDocument = (AccountingDocument) documentForValidation;
51          
52          // benefit transfers cannot be made between two different fringe benefit labor object codes.
53          boolean sameFringeBenefitObjectCodes = hasSameFringeBenefitObjectCodes(accountingDocument);
54          if (!sameFringeBenefitObjectCodes) {
55              GlobalVariables.getMessageMap().putError(KFSPropertyConstants.TARGET_ACCOUNTING_LINES, LaborKeyConstants.DISTINCT_OBJECT_CODE_ERROR);
56              result = false;
57          }
58  
59          return result;
60      }
61  
62      /**
63       * Determines whether target accounting lines have the same fringe benefit object codes as source accounting lines
64       * 
65       * @param accountingDocument the given accounting document
66       * @return true if target accounting lines have the same fringe benefit object codes as source accounting lines; otherwise, false
67       */
68      protected boolean hasSameFringeBenefitObjectCodes(AccountingDocument accountingDocument) {
69          LaborExpenseTransferDocumentBase expenseTransferDocument = (LaborExpenseTransferDocumentBase) accountingDocument;
70  
71          Set<String> objectCodesFromSourceLine = new HashSet<String>();
72          for (Object sourceAccountingLine : expenseTransferDocument.getSourceAccountingLines()) {
73              AccountingLine line = (AccountingLine) sourceAccountingLine;
74              objectCodesFromSourceLine.add(line.getFinancialObjectCode());
75          }
76  
77          Set<String> objectCodesFromTargetLine = new HashSet<String>();
78          for (Object targetAccountingLine : expenseTransferDocument.getTargetAccountingLines()) {
79              AccountingLine line = (AccountingLine) targetAccountingLine;
80              objectCodesFromTargetLine.add(line.getFinancialObjectCode());
81          }
82  
83          if (objectCodesFromSourceLine.size() != objectCodesFromTargetLine.size()) {
84              return false;
85          }
86  
87          return objectCodesFromSourceLine.containsAll(objectCodesFromTargetLine);
88      }
89  
90      /**
91       * Gets the accountingDocumentForValidation attribute. 
92       * @return Returns the accountingDocumentForValidation.
93       */
94      public Document getDocumentForValidation() {
95          return documentForValidation;
96      }
97  
98      /**
99       * Sets the accountingDocumentForValidation attribute value.
100      * @param accountingDocumentForValidation The accountingDocumentForValidation to set.
101      */
102     public void setDocumentForValidation(Document documentForValidation) {
103         this.documentForValidation = documentForValidation;
104     } 
105 }