View Javadoc
1   /*
2    * Copyright 2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.fp.document.validation.impl;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.kuali.ole.fp.businessobject.ProcurementCardTargetAccountingLine;
23  import org.kuali.ole.fp.businessobject.ProcurementCardTransactionDetail;
24  import org.kuali.ole.fp.document.ProcurementCardDocument;
25  import org.kuali.ole.sys.OLEKeyConstants;
26  import org.kuali.ole.sys.OLEPropertyConstants;
27  import org.kuali.ole.sys.businessobject.AccountingLine;
28  import org.kuali.ole.sys.businessobject.SourceAccountingLine;
29  import org.kuali.ole.sys.context.SpringContext;
30  import org.kuali.ole.sys.document.validation.GenericValidation;
31  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
32  import org.kuali.rice.core.api.parameter.ParameterEvaluator;
33  import org.kuali.rice.core.api.parameter.ParameterEvaluatorService;
34  import org.kuali.rice.krad.util.GlobalVariables;
35  import org.kuali.rice.krad.util.MessageMap;
36  import org.kuali.rice.krad.util.ObjectUtils;
37  
38  /**
39   * Validates that an accounting line does not have a capital object object code 
40   */
41  public class ProcurementCardObjectCodeValidation extends GenericValidation {
42      private AccountingLine accountingLineForValidation;
43  
44      /**
45       * Validates that an accounting line does not have a capital object object code
46       * <strong>Expects an accounting line as the first a parameter</strong>
47       * @see org.kuali.ole.sys.document.validation.Validation#validate(java.lang.Object[])
48       */
49      public boolean validate(AttributedDocumentEvent event) {
50          ProcurementCardDocument pcDocument = (ProcurementCardDocument) event.getDocument();
51          AccountingLine accountingLine = getAccountingLineForValidation();
52          if (!accountingLine.isTargetAccountingLine()) return true;
53          
54          MessageMap errors = GlobalVariables.getMessageMap();
55          String errorKey = OLEPropertyConstants.FINANCIAL_OBJECT_CODE;
56          boolean objectCodeAllowed = true;
57  
58          /* object code exist done in super, check we have a valid object */
59          if (ObjectUtils.isNull(accountingLine.getObjectCode())) {
60              return false;
61          }
62  
63          /* make sure object code is active */
64          if (!accountingLine.getObjectCode().isFinancialObjectActiveCode()) {
65              errors.putError(errorKey, OLEKeyConstants.ERROR_INACTIVE, "object code");
66              objectCodeAllowed = false;
67          }
68  
69          /* get merchant category code (mcc) restriction from transaction */
70          String mccRestriction = "";
71          ProcurementCardTargetAccountingLine line = (ProcurementCardTargetAccountingLine) accountingLine;
72          List pcTransactions = pcDocument.getTransactionEntries();
73          for (Iterator iter = pcTransactions.iterator(); iter.hasNext();) {
74              ProcurementCardTransactionDetail transactionEntry = (ProcurementCardTransactionDetail) iter.next();
75              if (transactionEntry.getFinancialDocumentTransactionLineNumber().equals(line.getFinancialDocumentTransactionLineNumber())) {
76                  mccRestriction = transactionEntry.getProcurementCardVendor().getTransactionMerchantCategoryCode();
77              }
78          }
79  
80          if (StringUtils.isBlank(mccRestriction)) {
81              return objectCodeAllowed;
82          }
83  
84          /* check object code is in permitted list for merchant category code (mcc) */
85          if (objectCodeAllowed) {
86              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());
87              objectCodeAllowed = evaluator.evaluateAndAddError(SourceAccountingLine.class, OLEPropertyConstants.FINANCIAL_OBJECT_CODE);
88          }
89  
90          /* check object sub type is in permitted list for merchant category code (mcc) */
91          if (objectCodeAllowed) {
92              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());
93              objectCodeAllowed = evaluator.evaluateAndAddError(SourceAccountingLine.class, "objectCode.financialObjectSubTypeCode", OLEPropertyConstants.FINANCIAL_OBJECT_CODE);
94          }
95          return objectCodeAllowed;
96      }
97  
98  
99      /**
100      * Gets the accountingLineForValidation attribute. 
101      * @return Returns the accountingLineForValidation.
102      */
103     public AccountingLine getAccountingLineForValidation() {
104         return accountingLineForValidation;
105     }
106 
107     /**
108      * Sets the accountingLineForValidation attribute value.
109      * @param accountingLineForValidation The accountingLineForValidation to set.
110      */
111     public void setAccountingLineForValidation(AccountingLine accountingLineForValidation) {
112         this.accountingLineForValidation = accountingLineForValidation;
113     }
114 }