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.fp.document.validation.impl;
017
018 import java.util.Iterator;
019 import java.util.List;
020
021 import org.apache.commons.lang.StringUtils;
022 import org.kuali.kfs.fp.businessobject.ProcurementCardTargetAccountingLine;
023 import org.kuali.kfs.fp.businessobject.ProcurementCardTransactionDetail;
024 import org.kuali.kfs.fp.document.ProcurementCardDocument;
025 import org.kuali.kfs.sys.KFSKeyConstants;
026 import org.kuali.kfs.sys.KFSPropertyConstants;
027 import org.kuali.kfs.sys.businessobject.AccountingLine;
028 import org.kuali.kfs.sys.businessobject.SourceAccountingLine;
029 import org.kuali.kfs.sys.context.SpringContext;
030 import org.kuali.kfs.sys.document.validation.GenericValidation;
031 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
032 import org.kuali.rice.kns.service.ParameterEvaluator;
033 import org.kuali.rice.kns.service.ParameterService;
034 import org.kuali.rice.kns.util.GlobalVariables;
035 import org.kuali.rice.kns.util.MessageMap;
036 import org.kuali.rice.kns.util.ObjectUtils;
037
038 /**
039 * Validates that an accounting line does not have a capital object object code
040 */
041 public class ProcurementCardObjectCodeValidation extends GenericValidation {
042 private AccountingLine accountingLineForValidation;
043
044 /**
045 * Validates that an accounting line does not have a capital object object code
046 * <strong>Expects an accounting line as the first a parameter</strong>
047 * @see org.kuali.kfs.sys.document.validation.Validation#validate(java.lang.Object[])
048 */
049 public boolean validate(AttributedDocumentEvent event) {
050 ProcurementCardDocument pcDocument = (ProcurementCardDocument) event.getDocument();
051 AccountingLine accountingLine = getAccountingLineForValidation();
052 if (!accountingLine.isTargetAccountingLine()) return true;
053
054 MessageMap errors = GlobalVariables.getMessageMap();
055 String errorKey = KFSPropertyConstants.FINANCIAL_OBJECT_CODE;
056 boolean objectCodeAllowed = true;
057
058 /* object code exist done in super, check we have a valid object */
059 if (ObjectUtils.isNull(accountingLine.getObjectCode())) {
060 return false;
061 }
062
063 /* make sure object code is active */
064 if (!accountingLine.getObjectCode().isFinancialObjectActiveCode()) {
065 errors.putError(errorKey, KFSKeyConstants.ERROR_INACTIVE, "object code");
066 objectCodeAllowed = false;
067 }
068
069 /* get merchant category code (mcc) restriction from transaction */
070 String mccRestriction = "";
071 ProcurementCardTargetAccountingLine line = (ProcurementCardTargetAccountingLine) accountingLine;
072 List pcTransactions = pcDocument.getTransactionEntries();
073 for (Iterator iter = pcTransactions.iterator(); iter.hasNext();) {
074 ProcurementCardTransactionDetail transactionEntry = (ProcurementCardTransactionDetail) iter.next();
075 if (transactionEntry.getFinancialDocumentTransactionLineNumber().equals(line.getFinancialDocumentTransactionLineNumber())) {
076 mccRestriction = transactionEntry.getProcurementCardVendor().getTransactionMerchantCategoryCode();
077 }
078 }
079
080 if (StringUtils.isBlank(mccRestriction)) {
081 return objectCodeAllowed;
082 }
083
084 /* check object code is in permitted list for merchant category code (mcc) */
085 if (objectCodeAllowed) {
086 ParameterEvaluator evaluator = SpringContext.getBean(ParameterService.class).getParameterEvaluator(ProcurementCardDocument.class, ProcurementCardDocumentRuleConstants.VALID_OBJECTS_BY_MCC_CODE_PARM_NM, ProcurementCardDocumentRuleConstants.INVALID_OBJECTS_BY_MCC_CODE_PARM_NM, mccRestriction, accountingLine.getFinancialObjectCode());
087 objectCodeAllowed = evaluator.evaluateAndAddError(SourceAccountingLine.class, KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
088 }
089
090 /* check object sub type is in permitted list for merchant category code (mcc) */
091 if (objectCodeAllowed) {
092 ParameterEvaluator evaluator = SpringContext.getBean(ParameterService.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());
093 objectCodeAllowed = evaluator.evaluateAndAddError(SourceAccountingLine.class, "objectCode.financialObjectSubTypeCode", KFSPropertyConstants.FINANCIAL_OBJECT_CODE);
094 }
095 return objectCodeAllowed;
096 }
097
098
099 /**
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 }