View Javadoc
1   /*
2    * The Kuali Financial System, a comprehensive financial management system for higher education.
3    * 
4    * Copyright 2005-2014 The Kuali Foundation
5    * 
6    * This program is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU Affero General Public License as
8    * published by the Free Software Foundation, either version 3 of the
9    * License, or (at your option) any later version.
10   * 
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU Affero General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Affero General Public License
17   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.kuali.kfs.module.purap.document.validation.impl;
20  
21  import java.util.Collection;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.kuali.kfs.module.purap.PurapConstants;
26  import org.kuali.kfs.module.purap.PurapKeyConstants;
27  import org.kuali.kfs.module.purap.PurapPropertyConstants;
28  import org.kuali.kfs.sys.document.validation.GenericValidation;
29  import org.kuali.kfs.sys.document.validation.event.AttributedDocumentEvent;
30  import org.kuali.kfs.vnd.businessobject.CommodityCode;
31  import org.kuali.rice.krad.service.BusinessObjectService;
32  import org.kuali.rice.krad.util.GlobalVariables;
33  
34  public class PurchasingCommodityCodesForDistributionValidation extends GenericValidation {
35  
36      private String purchasingCommodityCode;
37      private BusinessObjectService businessObjectService;
38      
39      public boolean validate(AttributedDocumentEvent event) {
40          boolean valid = true;
41          //Find out whether the commodity code has existed in the database
42          Map<String,String> fieldValues = new HashMap<String, String>();
43          fieldValues.put(PurapPropertyConstants.ITEM_COMMODITY_CODE, purchasingCommodityCode);
44          
45          Collection<CommodityCode> result = (Collection<CommodityCode>)businessObjectService.findMatching(CommodityCode.class, fieldValues);
46          if (result != null && result.size() > 0) {
47              CommodityCode commodityCode = (CommodityCode)(result.iterator().next());
48              if (!commodityCode.isActive()) {
49                  //This is the case where the commodity code on the item is not active.
50                  valid = false;
51                  GlobalVariables.getMessageMap().putError(PurapConstants.ACCOUNT_DISTRIBUTION_ERROR_KEY, PurapKeyConstants.PUR_COMMODITY_CODE_INACTIVE, " in distribute commodity code" );
52              }
53          }
54          else {
55              //This is the case where the commodity code on the item does not exist in the database.
56              valid = false;
57              GlobalVariables.getMessageMap().clearErrorPath();
58              GlobalVariables.getMessageMap().addToErrorPath(PurapConstants.ITEM_TAB_ERRORS);
59              GlobalVariables.getMessageMap().putError(PurapConstants.ACCOUNT_DISTRIBUTION_ERROR_KEY, PurapKeyConstants.PUR_COMMODITY_CODE_INVALID,  " in distribute commodity code" );
60          }
61          return valid;
62      }
63  
64      public String getPurchasingCommodityCode() {
65          return purchasingCommodityCode;
66      }
67  
68      public void setPurchasingCommodityCode(String purchasingCommodityCode) {
69          this.purchasingCommodityCode = purchasingCommodityCode;
70      }
71  
72      public BusinessObjectService getBusinessObjectService() {
73          return businessObjectService;
74      }
75  
76      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
77          this.businessObjectService = businessObjectService;
78      }
79  
80  }