View Javadoc
1   /*
2    * Copyright 2008-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.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.purap.PurapKeyConstants;
20  import org.kuali.ole.module.purap.PurapPropertyConstants;
21  import org.kuali.ole.module.purap.businessobject.PurApItem;
22  import org.kuali.ole.module.purap.businessobject.PurchasingItemBase;
23  import org.kuali.ole.sys.OLEKeyConstants;
24  import org.kuali.ole.sys.document.validation.GenericValidation;
25  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
26  import org.kuali.ole.vnd.businessobject.CommodityCode;
27  import org.kuali.rice.kns.service.DataDictionaryService;
28  import org.kuali.rice.krad.service.BusinessObjectService;
29  import org.kuali.rice.krad.util.GlobalVariables;
30  
31  import java.util.HashMap;
32  import java.util.Map;
33  
34  public class PurchasingCommodityCodeValidation extends GenericValidation {
35  
36      private BusinessObjectService businessObjectService;
37      private DataDictionaryService dataDictionaryService;
38      private PurApItem itemForValidation;
39  
40      public boolean validate(AttributedDocumentEvent event) {
41          boolean valid = true;
42          GlobalVariables.getMessageMap().clearErrorPath();
43          GlobalVariables.getMessageMap().addToErrorPath("document.item[" + (itemForValidation.getItemLineNumber() - 1) + "]");
44  
45          itemForValidation.refreshReferenceObject(PurapPropertyConstants.COMMODITY_CODE);
46          valid &= validateCommodityCodes(itemForValidation, commodityCodeIsRequired());
47  
48          GlobalVariables.getMessageMap().removeFromErrorPath("document.item[" + (itemForValidation.getItemLineNumber() - 1) + "]");
49  
50          return valid;
51  
52      }
53  
54      public PurApItem getItemForValidation() {
55          return itemForValidation;
56      }
57  
58      public void setItemForValidation(PurApItem itemForValidation) {
59          this.itemForValidation = itemForValidation;
60      }
61  
62      /**
63       * Validates whether the commodity code existed on the item, and if existed, whether the
64       * commodity code on the item existed in the database, and if so, whether the commodity
65       * code is active. Display error if any of these 3 conditions are not met.
66       *
67       * @param item The PurApItem containing the commodity code to be validated.
68       * @return boolean false if the validation fails and true otherwise.
69       */
70      protected boolean validateCommodityCodes(PurApItem item, boolean commodityCodeRequired) {
71          boolean valid = true;
72          String identifierString = item.getItemIdentifierString();
73          PurchasingItemBase purItem = (PurchasingItemBase) item;
74  
75          //This validation is only needed if the commodityCodeRequired system parameter is true
76          if (commodityCodeRequired && StringUtils.isBlank(purItem.getPurchasingCommodityCode())) {
77              //This is the case where the commodity code is required but the item does not currently contain the commodity code.
78              valid = false;
79              String attributeLabel = dataDictionaryService.
80                      getDataDictionary().getBusinessObjectEntry(CommodityCode.class.getName()).
81                      getAttributeDefinition(PurapPropertyConstants.ITEM_COMMODITY_CODE).getLabel();
82              GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + identifierString);
83          } else if (StringUtils.isNotBlank(purItem.getPurchasingCommodityCode())) {
84              //Find out whether the commodity code has existed in the database
85              Map<String, String> fieldValues = new HashMap<String, String>();
86              fieldValues.put(PurapPropertyConstants.ITEM_COMMODITY_CODE, purItem.getPurchasingCommodityCode());
87              if (businessObjectService.countMatching(CommodityCode.class, fieldValues) != 1) {
88                  //This is the case where the commodity code on the item does not exist in the database.
89                  valid = false;
90                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INVALID, " in " + identifierString);
91              } else {
92                  valid &= validateThatCommodityCodeIsActive(item);
93              }
94          }
95  
96          return valid;
97      }
98  
99      protected boolean validateThatCommodityCodeIsActive(PurApItem item) {
100         if (!((PurchasingItemBase) item).getCommodityCode().isActive()) {
101             //This is the case where the commodity code on the item is not active.
102             GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_COMMODITY_CODE, PurapKeyConstants.PUR_COMMODITY_CODE_INACTIVE, " in " + item.getItemIdentifierString());
103             return false;
104         }
105         return true;
106     }
107 
108     /**
109      * Predicate to do a parameter lookup and tell us whether a commodity code is required.
110      * Override in child classes.
111      *
112      * @return True if a commodity code is required.
113      */
114     protected boolean commodityCodeIsRequired() {
115         return false;
116     }
117 
118     public BusinessObjectService getBusinessObjectService() {
119         return businessObjectService;
120     }
121 
122     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
123         this.businessObjectService = businessObjectService;
124     }
125 
126     public DataDictionaryService getDataDictionaryService() {
127         return dataDictionaryService;
128     }
129 
130     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
131         this.dataDictionaryService = dataDictionaryService;
132     }
133 
134 }