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.fp.document.validation.impl;
20  
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.commons.lang.StringUtils;
25  import org.kuali.kfs.fp.businessobject.ProcurementCardTargetAccountingLine;
26  import org.kuali.kfs.fp.businessobject.ProcurementCardTransactionDetail;
27  import org.kuali.kfs.fp.document.ProcurementCardDocument;
28  import org.kuali.kfs.sys.KFSKeyConstants;
29  import org.kuali.kfs.sys.KFSPropertyConstants;
30  import org.kuali.kfs.sys.businessobject.AccountingLine;
31  import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
32  import org.kuali.kfs.sys.context.SpringContext;
33  import org.kuali.kfs.sys.document.validation.GenericValidation;
34  import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
35  import org.kuali.rice.core.api.parameter.ParameterEvaluator;
36  import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
37  import org.kuali.rice.krad.util.GlobalVariables;
38  import org.kuali.rice.krad.util.MessageMap;
39  import org.kuali.rice.krad.util.ObjectUtils;
40  
41  /**
42   * Validates that an accounting line does not have a capital object object code 
43   */
44  public class ProcurementCardObjectCodeValidation extends GenericValidation {
45      private AccountingLine accountingLineForValidation;
46  
47      /**
48       * Validates that an accounting line does not have a capital object object code
49       * <strong>Expects an accounting line as the first a parameter</strong>
50       * @see org.kuali.kfs.sys.document.validation.Validation#validate(java.lang.Object[])
51       */
52      public boolean validate(AttributedDocumentEvent event) {
53          ProcurementCardDocument pcDocument = (ProcurementCardDocument) event.getDocument();
54          AccountingLine accountingLine = getAccountingLineForValidation();
55          if (!accountingLine.isTargetAccountingLine()) return true;
56          
57          MessageMap errors = GlobalVariables.getMessageMap();
58          String errorKey = KFSPropertyConstants.FINANCIAL_OBJECT_CODE;
59          boolean objectCodeAllowed = true;
60  
61          /* object code exist done in super, check we have a valid object */
62          if (ObjectUtils.isNull(accountingLine.getObjectCode())) {
63              return false;
64          }
65  
66          /* make sure object code is active */
67          if (!accountingLine.getObjectCode().isFinancialObjectActiveCode()) {
68              errors.putError(errorKey, KFSKeyConstants.ERROR_INACTIVE, "object code");
69              objectCodeAllowed = false;
70          }
71  
72          /* get merchant category code (mcc) restriction from transaction */
73          String mccRestriction = "";
74          ProcurementCardTargetAccountingLine line = (ProcurementCardTargetAccountingLine) accountingLine;
75          List pcTransactions = pcDocument.getTransactionEntries();
76          for (Iterator iter = pcTransactions.iterator(); iter.hasNext();) {
77              ProcurementCardTransactionDetail transactionEntry = (ProcurementCardTransactionDetail) iter.next();
78              if (transactionEntry.getFinancialDocumentTransactionLineNumber().equals(line.getFinancialDocumentTransactionLineNumber())) {
79                  mccRestriction = transactionEntry.getProcurementCardVendor().getTransactionMerchantCategoryCode();
80              }
81          }
82  
83          if (StringUtils.isBlank(mccRestriction)) {
84              return objectCodeAllowed;
85          }
86  
87          /* check object code is in permitted list for merchant category code (mcc) */
88          if (objectCodeAllowed) {
89              ParameterEvaluator evaluator = /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(ProcurementCardDocument.class, ProcurementCardDocumentRuleConstants.VALID_OBJECTS_BY_MCC_CODE_PARM_NM, ProcurementCardDocumentRuleConstants.INVALID_OBJECTS_BY_MCC_CODE_PARM_NM, mccRestriction, accountingLine.getFinancialObjectCode());
90              objectCodeAllowed = evaluator.evaluateAndAddError(SourceAccountingLine.class, KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
91          }
92  
93          /* check object sub type is in permitted list for merchant category code (mcc) */
94          if (objectCodeAllowed) {
95              ParameterEvaluator evaluator = /*REFACTORME*/SpringContext.getBean(ParameterEvaluatorService.class).getParameterEvaluator(ProcurementCardDocument.class, ProcurementCardDocumentRuleConstants.VALID_OBJ_SUB_TYPE_BY_MCC_CODE_PARM_NM, ProcurementCardDocumentRuleConstants.INVALID_OBJ_SUB_TYPE_BY_MCC_CODE_PARM_NM, mccRestriction, accountingLine.getObjectCode().getFinancialObjectSubTypeCode());
96              objectCodeAllowed = evaluator.evaluateAndAddError(SourceAccountingLine.class, "objectCode.financialObjectSubTypeCode", KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
97          }
98          return objectCodeAllowed;
99      }
100 
101 
102     /**
103      * Gets the accountingLineForValidation attribute. 
104      * @return Returns the accountingLineForValidation.
105      */
106     public AccountingLine getAccountingLineForValidation() {
107         return accountingLineForValidation;
108     }
109 
110     /**
111      * Sets the accountingLineForValidation attribute value.
112      * @param accountingLineForValidation The accountingLineForValidation to set.
113      */
114     public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
115         this.accountingLineForValidation = accountingLineForValidation;
116     }
117 }