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 java.util.HashMap;
019    import java.util.Map;
020    
021    import org.apache.commons.lang.StringUtils;
022    import org.kuali.kfs.module.purap.PurapConstants;
023    import org.kuali.kfs.module.purap.PurapKeyConstants;
024    import org.kuali.kfs.module.purap.businessobject.PurApItem;
025    import org.kuali.kfs.module.purap.businessobject.PurchasingItemBase;
026    import org.kuali.kfs.sys.KFSKeyConstants;
027    import org.kuali.kfs.sys.KFSPropertyConstants;
028    import org.kuali.kfs.sys.businessobject.UnitOfMeasure;
029    import org.kuali.kfs.sys.document.validation.GenericValidation;
030    import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
031    import org.kuali.rice.kns.service.BusinessObjectService;
032    import org.kuali.rice.kns.service.DataDictionaryService;
033    import org.kuali.rice.kns.util.GlobalVariables;
034    
035    public class PurchasingUnitOfMeasureValidation extends GenericValidation {
036    
037        private PurApItem itemForValidation;
038        private DataDictionaryService dataDictionaryService;
039        private BusinessObjectService businessObjectService;
040        
041        /**
042         * Validates that if the item type is quantity based, the unit of measure is required.
043         */
044        public boolean validate(AttributedDocumentEvent event) {
045            boolean valid = true;
046            PurchasingItemBase purItem = (PurchasingItemBase) itemForValidation;
047            GlobalVariables.getMessageMap().addToErrorPath(PurapConstants.ITEM_TAB_ERRORS);
048            
049            // Validations for quantity based item type
050            if (purItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
051                String uomCode = purItem.getItemUnitOfMeasureCode();
052                if (StringUtils.isEmpty(uomCode)) {
053                    valid = false;
054                    String attributeLabel = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(purItem.getClass().getName()).
055                                            getAttributeDefinition(KFSPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE).
056                                            getLabel();
057                    GlobalVariables.getMessageMap().putError(KFSPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, KFSKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + purItem.getItemIdentifierString());
058                }
059                else {
060                    //Find out whether the unit of measure code has existed in the database
061                    Map<String,String> fieldValues = new HashMap<String, String>();
062                    fieldValues.put(KFSPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, purItem.getItemUnitOfMeasureCode());
063                    if (businessObjectService.countMatching(UnitOfMeasure.class, fieldValues) != 1) {
064                        //This is the case where the unit of measure code on the item does not exist in the database.
065                        valid = false;
066                        GlobalVariables.getMessageMap().putError(KFSPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, PurapKeyConstants.PUR_ITEM_UNIT_OF_MEASURE_CODE_INVALID,  " in " + purItem.getItemIdentifierString());
067                    }
068                }            
069            }
070    
071            GlobalVariables.getMessageMap().clearErrorPath();
072            
073            return valid;
074        }
075    
076        public PurApItem getItemForValidation() {
077            return itemForValidation;
078        }
079    
080        public void setItemForValidation(PurApItem itemForValidation) {
081            this.itemForValidation = itemForValidation;
082        }
083    
084        public DataDictionaryService getDataDictionaryService() {
085            return dataDictionaryService;
086        }
087    
088        public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
089            this.dataDictionaryService = dataDictionaryService;
090        }
091    
092        public BusinessObjectService getBusinessObjectService() {
093            return businessObjectService;
094        }
095    
096        public void setBusinessObjectService(BusinessObjectService businessObjectService) {
097            this.businessObjectService = businessObjectService;
098        }
099    
100    }