View Javadoc

1   /*
2    * Copyright 2009 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.document.validation.impl;
17  
18  import org.kuali.ole.module.purap.PurapConstants;
19  import org.kuali.ole.module.purap.PurapKeyConstants;
20  import org.kuali.ole.module.purap.PurapPropertyConstants;
21  import org.kuali.ole.sys.document.validation.GenericValidation;
22  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
23  import org.kuali.ole.vnd.businessobject.CommodityCode;
24  import org.kuali.rice.krad.service.BusinessObjectService;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  
27  import java.util.Collection;
28  import java.util.HashMap;
29  import java.util.Map;
30  
31  public class PurchasingCommodityCodesForDistributionValidation extends GenericValidation {
32  
33      private String purchasingCommodityCode;
34      private BusinessObjectService businessObjectService;
35  
36      public boolean validate(AttributedDocumentEvent event) {
37          boolean valid = true;
38          //Find out whether the commodity code has existed in the database
39          Map<String, String> fieldValues = new HashMap<String, String>();
40          fieldValues.put(PurapPropertyConstants.ITEM_COMMODITY_CODE, purchasingCommodityCode);
41  
42          Collection<CommodityCode> result = (Collection<CommodityCode>) businessObjectService.findMatching(CommodityCode.class, fieldValues);
43          if (result != null && result.size() > 0) {
44              CommodityCode commodityCode = (CommodityCode) (result.iterator().next());
45              if (!commodityCode.isActive()) {
46                  //This is the case where the commodity code on the item is not active.
47                  valid = false;
48                  GlobalVariables.getMessageMap().putError(PurapConstants.ACCOUNT_DISTRIBUTION_ERROR_KEY, PurapKeyConstants.PUR_COMMODITY_CODE_INACTIVE, " in distribute commodity code");
49              }
50          } else {
51              //This is the case where the commodity code on the item does not exist in the database.
52              valid = false;
53              GlobalVariables.getMessageMap().putError(PurapConstants.ACCOUNT_DISTRIBUTION_ERROR_KEY, PurapKeyConstants.PUR_COMMODITY_CODE_INVALID, " in distribute commodity code");
54          }
55          return valid;
56      }
57  
58      public String getPurchasingCommodityCode() {
59          return purchasingCommodityCode;
60      }
61  
62      public void setPurchasingCommodityCode(String purchasingCommodityCode) {
63          this.purchasingCommodityCode = purchasingCommodityCode;
64      }
65  
66      public BusinessObjectService getBusinessObjectService() {
67          return businessObjectService;
68      }
69  
70      public void setBusinessObjectService(BusinessObjectService businessObjectService) {
71          this.businessObjectService = businessObjectService;
72      }
73  
74  }