use of org.kuali.kfs.module.purap.businessobject.PurchasingItemBase in project cu-kfs by CU-CommunityApps.
the class PurchasingCommodityCodeValidation method validateCommodityCodes.
/**
* 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;
// check to see if item is unordered and commodity code is required, if so, assign a default commodity code and return true
if (commodityCodeRequired && purItem.getItemTypeCode().equals("UNOR")) {
ParameterService parameterService = SpringContext.getBean(ParameterService.class);
String unorderedItemDefaultCommodityCode = parameterService.getParameterValueAsString(PurapConstants.PURAP_NAMESPACE, "LineItemReceiving", UNORDERED_ITEM_DEFAULT_COMMODITY_CODE);
purItem.setPurchasingCommodityCode(unorderedItemDefaultCommodityCode);
valid = true;
}
// 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 = businessObjectDictionaryService.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;
}
Aggregations