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.purap.document.validation.impl; 017 018 import org.apache.commons.lang.StringUtils; 019 import org.kuali.kfs.module.purap.PurapConstants; 020 import org.kuali.kfs.module.purap.PurapKeyConstants; 021 import org.kuali.kfs.module.purap.PurapPropertyConstants; 022 import org.kuali.kfs.module.purap.businessobject.PurApItem; 023 import org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocument; 024 import org.kuali.kfs.sys.document.validation.GenericValidation; 025 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent; 026 import org.kuali.rice.kns.service.DataDictionaryService; 027 import org.kuali.rice.kns.service.ParameterService; 028 import org.kuali.rice.kns.util.GlobalVariables; 029 import org.kuali.rice.kns.util.KualiDecimal; 030 import org.kuali.rice.kns.util.ObjectUtils; 031 032 public class PurchasingAccountsPayableBelowTheLineValuesValidation extends GenericValidation { 033 034 private DataDictionaryService dataDictionaryService; 035 private ParameterService parameterService; 036 private PurApItem itemForValidation; 037 038 /** 039 * Performs validations for below the line items. If the unit price is zero, and the system parameter indicates that the item 040 * should not allow zero, then the validation fails. If the unit price is positive and the system parameter indicates that the 041 * item should not allow positive values, then the validation fails. If the unit price is negative and the system parameter 042 * indicates that the item should not allow negative values, then the validation fails. If the unit price is entered and is not 043 * zero and the item description is empty and the system parameter indicates that the item requires user to enter description, 044 * then the validation fails. 045 */ 046 public boolean validate(AttributedDocumentEvent event) { 047 boolean valid = true; 048 PurchasingAccountsPayableDocument purapDocument = (PurchasingAccountsPayableDocument) event.getDocument(); 049 String documentType = dataDictionaryService.getDocumentTypeNameByClass(purapDocument.getClass()); 050 051 try { 052 if (ObjectUtils.isNotNull(itemForValidation.getItemUnitPrice()) && (new KualiDecimal(itemForValidation.getItemUnitPrice())).isZero()) { 053 if (parameterService.parameterExists(Class.forName(PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(documentType)), PurapConstants.ITEM_ALLOWS_ZERO) && !parameterService.getParameterEvaluator(Class.forName(PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(documentType)), PurapConstants.ITEM_ALLOWS_ZERO, itemForValidation.getItemTypeCode()).evaluationSucceeds()) { 054 valid = false; 055 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, PurapKeyConstants.ERROR_ITEM_BELOW_THE_LINE, itemForValidation.getItemType().getItemTypeDescription(), "zero"); 056 } 057 } 058 else if (ObjectUtils.isNotNull(itemForValidation.getItemUnitPrice()) && (new KualiDecimal(itemForValidation.getItemUnitPrice())).isPositive()) { 059 if (parameterService.parameterExists(Class.forName(PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(documentType)), PurapConstants.ITEM_ALLOWS_POSITIVE) && !parameterService.getParameterEvaluator(Class.forName(PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(documentType)), PurapConstants.ITEM_ALLOWS_POSITIVE, itemForValidation.getItemTypeCode()).evaluationSucceeds()) { 060 valid = false; 061 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, PurapKeyConstants.ERROR_ITEM_BELOW_THE_LINE, itemForValidation.getItemType().getItemTypeDescription(), "positive"); 062 } 063 } 064 else if (ObjectUtils.isNotNull(itemForValidation.getItemUnitPrice()) && (new KualiDecimal(itemForValidation.getItemUnitPrice())).isNegative()) { 065 if (parameterService.parameterExists(Class.forName(PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(documentType)), PurapConstants.ITEM_ALLOWS_NEGATIVE) && !parameterService.getParameterEvaluator(Class.forName(PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(documentType)), PurapConstants.ITEM_ALLOWS_NEGATIVE, itemForValidation.getItemTypeCode()).evaluationSucceeds()) { 066 valid = false; 067 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, PurapKeyConstants.ERROR_ITEM_BELOW_THE_LINE, itemForValidation.getItemType().getItemTypeDescription(), "negative"); 068 } 069 } 070 if (ObjectUtils.isNotNull(itemForValidation.getItemUnitPrice()) && (new KualiDecimal(itemForValidation.getItemUnitPrice())).isNonZero() && StringUtils.isEmpty(itemForValidation.getItemDescription())) { 071 if (parameterService.parameterExists(Class.forName(PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(documentType)), PurapConstants.ITEM_REQUIRES_USER_ENTERED_DESCRIPTION) && parameterService.getParameterEvaluator(Class.forName(PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(documentType)), PurapConstants.ITEM_REQUIRES_USER_ENTERED_DESCRIPTION, itemForValidation.getItemTypeCode()).evaluationSucceeds()) { 072 valid = false; 073 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_DESCRIPTION, PurapKeyConstants.ERROR_ITEM_BELOW_THE_LINE, "The item description of " + itemForValidation.getItemType().getItemTypeDescription(), "empty"); 074 } 075 } 076 077 //now check total amount, if positive check if they should really be negative 078 if (ObjectUtils.isNotNull(itemForValidation.getTotalAmount()) && itemForValidation.getTotalAmount().isPositive()){ 079 if (parameterService.parameterExists(Class.forName(PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(documentType)), PurapConstants.ITEM_ALLOWS_POSITIVE) && !parameterService.getParameterEvaluator(Class.forName(PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(documentType)), PurapConstants.ITEM_ALLOWS_POSITIVE, itemForValidation.getItemTypeCode()).evaluationSucceeds()) { 080 valid = false; 081 GlobalVariables.getMessageMap().putError(PurapPropertyConstants.TOTAL_AMOUNT, PurapKeyConstants.ERROR_ITEM_BELOW_THE_LINE, itemForValidation.getItemType().getItemTypeDescription() + " Total Amount", "positive"); 082 } 083 } 084 } 085 catch (ClassNotFoundException e) { 086 throw new RuntimeException("The valideBelowTheLineValues of PurchasingAccountsPayableDocumentRuleBase was unable to resolve a document type class: " + PurapConstants.PURAP_DETAIL_TYPE_CODE_MAP.get(documentType), e); 087 } 088 089 return valid; 090 } 091 092 public DataDictionaryService getDataDictionaryService() { 093 return dataDictionaryService; 094 } 095 096 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) { 097 this.dataDictionaryService = dataDictionaryService; 098 } 099 100 public ParameterService getParameterService() { 101 return parameterService; 102 } 103 104 public void setParameterService(ParameterService parameterService) { 105 this.parameterService = parameterService; 106 } 107 108 public PurApItem getItemForValidation() { 109 return itemForValidation; 110 } 111 112 public void setItemForValidation(PurApItem itemForValidation) { 113 this.itemForValidation = itemForValidation; 114 } 115 116 }