/*
* The Kuali Financial System, a comprehensive financial management system for higher education.
*
* Copyright 2005-2014 The Kuali Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.kuali.kfs.module.purap.document.validation.impl;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.kuali.kfs.module.purap.PurapConstants.ItemFields;
import org.kuali.kfs.module.purap.PurapConstants.ItemTypeCodes;
import org.kuali.kfs.module.purap.PurapKeyConstants;
import org.kuali.kfs.module.purap.PurapPropertyConstants;
import org.kuali.kfs.module.purap.businessobject.PurApItem;
import org.kuali.kfs.module.purap.businessobject.PurchasingItemBase;
import org.kuali.kfs.sys.KFSKeyConstants;
import org.kuali.kfs.sys.KFSPropertyConstants;
import org.kuali.kfs.sys.businessobject.UnitOfMeasure;
import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
import org.kuali.kfs.vnd.businessobject.CommodityCode;
import org.kuali.rice.kns.service.DataDictionaryService;
import org.kuali.rice.krad.service.BusinessObjectService;
import org.kuali.rice.krad.util.GlobalVariables;
import org.kuali.rice.krad.util.ObjectUtils;
public class PurchasingAddItemValidation extends PurchasingAccountsPayableAddItemValidation {
private BusinessObjectService businessObjectService;
private DataDictionaryService dataDictionaryService;
public boolean validate(AttributedDocumentEvent event) {
boolean valid=true;
GlobalVariables.getMessageMap().addToErrorPath(PurapPropertyConstants.NEW_PURCHASING_ITEM_LINE);
//refresh itemType
PurApItem refreshedItem = getItemForValidation();
refreshedItem.refreshReferenceObject("itemType");
super.setItemForValidation(refreshedItem);
valid &= super.validate(event);
valid &= validateItemUnitPrice(getItemForValidation());
valid &= validateUnitOfMeasure(getItemForValidation());
if (getItemForValidation().getItemType().isLineItemIndicator()) {
valid &= validateItemDescription(getItemForValidation());
valid &= validateItemQuantity(getItemForValidation());
valid &= validateCommodityCodes(getItemForValidation(), commodityCodeIsRequired());
}
GlobalVariables.getMessageMap().removeFromErrorPath(PurapPropertyConstants.NEW_PURCHASING_ITEM_LINE);
return valid;
}
/**
* Validates whether the commodity code existed on the item, and if existed, whether the
* commodity code on the item existed in the database, and if so, whether the commodity
* code is active. Display error if any of these 3 conditions are not met.
*
* @param item The PurApItem containing the commodity code to be validated.
* @return boolean false if the validation fails and true otherwise.
*/
protected boolean validateCommodityCodes(PurApItem item, boolean commodityCodeRequired) {
boolean valid = true;
String identifierString = item.getItemIdentifierString();
PurchasingItemBase purItem = (PurchasingItemBase) item;
//This validation is only needed if the commodityCodeRequired system parameter is true
if (commodityCodeRequired && StringUtils.isBlank(purItem.getPurchasingCommodityCode()) ) {
//This is the case where the commodity code is required but the item does not currently contain the commodity code.
valid = false;
String attributeLabel = dataDictionaryService.
getDataDictionary().getBusinessObjectEntry(CommodityCode.class.getName()).
getAttributeDefinition(PurapPropertyConstants.ITEM_COMMODITY_CODE).getLabel();
GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, KFSKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + identifierString);
}
else if (StringUtils.isNotBlank(purItem.getPurchasingCommodityCode())) {
//Find out whether the commodity code has existed in the database
Map<String,String> fieldValues = new HashMap<String, String>();
fieldValues.put(PurapPropertyConstants.ITEM_COMMODITY_CODE, purItem.getPurchasingCommodityCode());
if (businessObjectService.countMatching(CommodityCode.class, fieldValues) != 1) {
//This is the case where the commodity code on the item does not exist in the database.
valid = false;
GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INVALID, " in " + identifierString);
}
else {
valid &= validateThatCommodityCodeIsActive(item);
}
}
return valid;
}
/**
* Validates the unit price for all applicable item types. It validates that the unit price field was
* entered on the item, and that the price is in the right range for the item type.
*
* @param purDocument the purchasing document to be validated
* @return boolean false if there is any validation that fails.
*/
public boolean validateItemUnitPrice(PurApItem item) {
boolean valid = true;
if (item.getItemType().isLineItemIndicator()) {
if (ObjectUtils.isNull(item.getItemUnitPrice())) {
valid = false;
String attributeLabel = dataDictionaryService.
getDataDictionary().getBusinessObjectEntry(item.getClass().getName()).
getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_PRICE).getLabel();
GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, KFSKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + item.getItemIdentifierString());
}
}
if (ObjectUtils.isNotNull(item.getItemUnitPrice())) {
if ((BigDecimal.ZERO.compareTo(item.getItemUnitPrice()) > 0) && ((!item.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_ORDER_DISCOUNT_CODE)) && (!item.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_TRADE_IN_CODE)))) {
// If the item type is not full order discount or trade in items, don't allow negative unit price.
GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, PurapKeyConstants.ERROR_ITEM_AMOUNT_BELOW_ZERO, ItemFields.UNIT_COST, item.getItemIdentifierString());
valid = false;
}
else if ((BigDecimal.ZERO.compareTo(item.getItemUnitPrice()) < 0) && ((item.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_ORDER_DISCOUNT_CODE)) || (item.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_TRADE_IN_CODE)))) {
// If the item type is full order discount or trade in items, its unit price must be negative.
GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, PurapKeyConstants.ERROR_ITEM_AMOUNT_NOT_BELOW_ZERO, ItemFields.UNIT_COST, item.getItemIdentifierString());
valid = false;
}
}
return valid;
}
/**
* Validates that if the item type is quantity based, the unit of measure is required.
*
* @param item the item to be validated
* @return boolean false if the item type is quantity based and the unit of measure is empty.
*/
public boolean validateUnitOfMeasure(PurApItem item) {
boolean valid = true;
PurchasingItemBase purItem = (PurchasingItemBase) item;
// Validations for quantity based item type
if (purItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
String uomCode = purItem.getItemUnitOfMeasureCode();
if (StringUtils.isEmpty(uomCode)) {
valid = false;
String attributeLabel = dataDictionaryService.
getDataDictionary().getBusinessObjectEntry(item.getClass().getName()).
getAttributeDefinition(KFSPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE).
getLabel();
GlobalVariables.getMessageMap().putError(KFSPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, KFSKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + item.getItemIdentifierString());
}
else {
//Find out whether the unit of measure code has existed in the database
Map<String,String> fieldValues = new HashMap<String, String>();
fieldValues.put(KFSPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, purItem.getItemUnitOfMeasureCode());
if (businessObjectService.countMatching(UnitOfMeasure.class, fieldValues) != 1) {
//This is the case where the unit of measure code on the item does not exist in the database.
valid = false;
GlobalVariables.getMessageMap().putError(KFSPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, PurapKeyConstants.PUR_ITEM_UNIT_OF_MEASURE_CODE_INVALID, " in " + item.getItemIdentifierString());
}
//Validate UOM for active check.
if(ObjectUtils.isNotNull(purItem.getItemUnitOfMeasure())){
if(!purItem.getItemUnitOfMeasure().isActive()){
valid = false;
String attributeLabel = dataDictionaryService.
getDataDictionary().getBusinessObjectEntry(item.getClass().getName()).
getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE).getLabel();
GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, PurapKeyConstants.ERROR_ITEM_UOM_INACTIVE, attributeLabel + " in " + item.getItemIdentifierString());
}
}
}
}
// Validations for non-quantity based item type
if (purItem.getItemType().isAmountBasedGeneralLedgerIndicator() && StringUtils.isNotBlank(purItem.getItemUnitOfMeasureCode())) {
valid = false;
String attributeLabel = dataDictionaryService.
getDataDictionary().getBusinessObjectEntry(item.getClass().getName()).
getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE).getLabel();
GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, PurapKeyConstants.ERROR_ITEM_UOM_NOT_ALLOWED, attributeLabel + " in " + item.getItemIdentifierString());
}
return valid;
}
/**
* Checks that a description was entered for the item.
*
* @param item
* @return
*/
public boolean validateItemDescription(PurApItem item) {
boolean valid = true;
if (StringUtils.isEmpty(item.getItemDescription())) {
valid = false;
String attributeLabel = dataDictionaryService.
getDataDictionary().getBusinessObjectEntry(item.getClass().getName()).
getAttributeDefinition(PurapPropertyConstants.ITEM_DESCRIPTION).getLabel();
GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_DESCRIPTION, KFSKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + item.getItemIdentifierString());
}
return valid;
}
/**
* Validates that if the item type is quantity based, the item quantity is required and if the item type is amount based, the
* quantity is not allowed.
*
* @param item the item to be validated
* @return boolean false if there's any validation that fails.
*/
public boolean validateItemQuantity(PurApItem item) {
boolean valid = true;
PurchasingItemBase purItem = (PurchasingItemBase) item;
if (purItem.getItemType().isQuantityBasedGeneralLedgerIndicator() && (ObjectUtils.isNull(purItem.getItemQuantity()))) {
valid = false;
String attributeLabel = dataDictionaryService.
getDataDictionary().getBusinessObjectEntry(item.getClass().getName()).
getAttributeDefinition(PurapPropertyConstants.ITEM_QUANTITY).getLabel();
GlobalVariables.getMessageMap().putError(PurapPropertyConstants.QUANTITY, KFSKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + item.getItemIdentifierString());
}
else if (purItem.getItemType().isAmountBasedGeneralLedgerIndicator() && ObjectUtils.isNotNull(purItem.getItemQuantity())) {
valid = false;
String attributeLabel = dataDictionaryService.
getDataDictionary().getBusinessObjectEntry(item.getClass().getName()).
getAttributeDefinition(PurapPropertyConstants.ITEM_QUANTITY).getLabel();
GlobalVariables.getMessageMap().putError(PurapPropertyConstants.QUANTITY, PurapKeyConstants.ERROR_ITEM_QUANTITY_NOT_ALLOWED, attributeLabel + " in " + item.getItemIdentifierString());
}
return valid;
}
/**
* Predicate to do a parameter lookup and tell us whether a commodity code is required.
* Override in child classes.
*
* @return True if a commodity code is required.
*/
protected boolean commodityCodeIsRequired() {
return false;
}
protected boolean validateThatCommodityCodeIsActive(PurApItem item) {
if (!((PurchasingItemBase)item).getCommodityCode().isActive()) {
//This is the case where the commodity code on the item is not active.
GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INACTIVE, " in " + item.getItemIdentifierString());
return false;
}
return true;
}
public BusinessObjectService getBusinessObjectService() {
return businessObjectService;
}
public void setBusinessObjectService(BusinessObjectService businessObjectService) {
this.businessObjectService = businessObjectService;
}
public DataDictionaryService getDataDictionaryService() {
return dataDictionaryService;
}
public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
this.dataDictionaryService = dataDictionaryService;
}
}