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.Collection;
019 import java.util.HashMap;
020 import java.util.Map;
021
022 import org.kuali.kfs.module.purap.PurapConstants;
023 import org.kuali.kfs.module.purap.PurapKeyConstants;
024 import org.kuali.kfs.module.purap.PurapPropertyConstants;
025 import org.kuali.kfs.sys.context.SpringContext;
026 import org.kuali.kfs.sys.document.validation.GenericValidation;
027 import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
028 import org.kuali.kfs.vnd.businessobject.CommodityCode;
029 import org.kuali.rice.kns.service.BusinessObjectService;
030 import org.kuali.rice.kns.util.GlobalVariables;
031
032 public class PurchasingCommodityCodesForDistributionValidation extends GenericValidation {
033
034 private String purchasingCommodityCode;
035 private BusinessObjectService businessObjectService;
036
037 public boolean validate(AttributedDocumentEvent event) {
038 boolean valid = true;
039 //Find out whether the commodity code has existed in the database
040 Map<String,String> fieldValues = new HashMap<String, String>();
041 fieldValues.put(PurapPropertyConstants.ITEM_COMMODITY_CODE, purchasingCommodityCode);
042
043 Collection<CommodityCode> result = (Collection<CommodityCode>)businessObjectService.findMatching(CommodityCode.class, fieldValues);
044 if (result != null && result.size() > 0) {
045 CommodityCode commodityCode = (CommodityCode)(result.iterator().next());
046 if (!commodityCode.isActive()) {
047 //This is the case where the commodity code on the item is not active.
048 valid = false;
049 GlobalVariables.getMessageMap().putError(PurapConstants.ACCOUNT_DISTRIBUTION_ERROR_KEY, PurapKeyConstants.PUR_COMMODITY_CODE_INACTIVE, " in distribute commodity code" );
050 }
051 }
052 else {
053 //This is the case where the commodity code on the item does not exist in the database.
054 valid = false;
055 GlobalVariables.getMessageMap().putError(PurapConstants.ACCOUNT_DISTRIBUTION_ERROR_KEY, PurapKeyConstants.PUR_COMMODITY_CODE_INVALID, " in distribute commodity code" );
056 }
057 return valid;
058 }
059
060 public String getPurchasingCommodityCode() {
061 return purchasingCommodityCode;
062 }
063
064 public void setPurchasingCommodityCode(String purchasingCommodityCode) {
065 this.purchasingCommodityCode = purchasingCommodityCode;
066 }
067
068 public BusinessObjectService getBusinessObjectService() {
069 return businessObjectService;
070 }
071
072 public void setBusinessObjectService(BusinessObjectService businessObjectService) {
073 this.businessObjectService = businessObjectService;
074 }
075
076 }