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