001/* 002 * Copyright 2009 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 */ 016package org.kuali.ole.module.purap.document.validation.impl; 017 018import org.apache.commons.lang.StringUtils; 019import org.kuali.ole.module.purap.PurapConstants; 020import org.kuali.ole.module.purap.PurapKeyConstants; 021import org.kuali.ole.module.purap.PurapPropertyConstants; 022import org.kuali.ole.module.purap.businessobject.PurApItem; 023import org.kuali.ole.module.purap.businessobject.PurchasingItemBase; 024import org.kuali.ole.sys.OLEKeyConstants; 025import org.kuali.ole.sys.OLEPropertyConstants; 026import org.kuali.ole.sys.businessobject.UnitOfMeasure; 027import org.kuali.ole.sys.document.validation.GenericValidation; 028import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent; 029import org.kuali.rice.kns.service.DataDictionaryService; 030import org.kuali.rice.krad.service.BusinessObjectService; 031import org.kuali.rice.krad.util.GlobalVariables; 032 033import java.util.HashMap; 034import java.util.Map; 035 036public class PurchasingUnitOfMeasureValidation extends GenericValidation { 037 038 private PurApItem itemForValidation; 039 private DataDictionaryService dataDictionaryService; 040 private BusinessObjectService businessObjectService; 041 042 /** 043 * Validates that if the item type is quantity based, the unit of measure is required. 044 */ 045 @Override 046 public boolean validate(AttributedDocumentEvent event) { 047 boolean valid = true; 048 PurchasingItemBase purItem = (PurchasingItemBase) itemForValidation; 049 GlobalVariables.getMessageMap().addToErrorPath(PurapConstants.ITEM_TAB_ERRORS); 050 051 // Validations for quantity based item type 052 if (purItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) { 053 String uomCode = purItem.getItemUnitOfMeasureCode(); 054 if (StringUtils.isEmpty(uomCode)) { 055 valid = false; 056 String attributeLabel = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(purItem.getClass().getName()). 057 getAttributeDefinition(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE). 058 getLabel(); 059 GlobalVariables.getMessageMap().putError(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + purItem.getItemIdentifierString()); 060 } else { 061 //Find out whether the unit of measure code has existed in the database 062 Map<String, String> fieldValues = new HashMap<String, String>(); 063 fieldValues.put(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, purItem.getItemUnitOfMeasureCode()); 064 if (businessObjectService.countMatching(UnitOfMeasure.class, fieldValues) != 1) { 065 //This is the case where the unit of measure code on the item does not exist in the database. 066 valid = false; 067 GlobalVariables.getMessageMap().putError(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, PurapKeyConstants.PUR_ITEM_UNIT_OF_MEASURE_CODE_INVALID, " in " + purItem.getItemIdentifierString()); 068 } 069 } 070 } 071 072 GlobalVariables.getMessageMap().clearErrorPath(); 073 074 // MSU Contribution OLEMI-5041 DTT-3371 OLECNTRB-955 075 if (purItem.getItemType().isAmountBasedGeneralLedgerIndicator() 076 && StringUtils.isNotBlank(purItem.getItemUnitOfMeasureCode())) { 077 valid = false; 078 if(itemForValidation.getItemLineNumber() != null){ 079 String errorPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "[" 080 + (itemForValidation.getItemLineNumber() - 1) + "]." 081 + PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE; 082 String attributeLabel = dataDictionaryService.getDataDictionary() 083 .getBusinessObjectEntry(purItem.getClass().getName()) 084 .getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE).getLabel(); 085 GlobalVariables.getMessageMap().putError(errorPrefix, PurapKeyConstants.ERROR_ITEM_UOM_NOT_ALLOWED, 086 attributeLabel + " in " + purItem.getItemIdentifierString()); 087 } 088 } 089 090 return valid; 091 } 092 093 public PurApItem getItemForValidation() { 094 return itemForValidation; 095 } 096 097 public void setItemForValidation(PurApItem itemForValidation) { 098 this.itemForValidation = itemForValidation; 099 } 100 101 public DataDictionaryService getDataDictionaryService() { 102 return dataDictionaryService; 103 } 104 105 public void setDataDictionaryService(DataDictionaryService dataDictionaryService) { 106 this.dataDictionaryService = dataDictionaryService; 107 } 108 109 public BusinessObjectService getBusinessObjectService() { 110 return businessObjectService; 111 } 112 113 public void setBusinessObjectService(BusinessObjectService businessObjectService) { 114 this.businessObjectService = businessObjectService; 115 } 116 117}