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.apache.commons.lang.StringUtils;
19  import org.kuali.ole.module.purap.PurapConstants;
20  import org.kuali.ole.module.purap.PurapKeyConstants;
21  import org.kuali.ole.module.purap.PurapPropertyConstants;
22  import org.kuali.ole.module.purap.businessobject.PurApItem;
23  import org.kuali.ole.module.purap.businessobject.PurchasingItemBase;
24  import org.kuali.ole.sys.OLEKeyConstants;
25  import org.kuali.ole.sys.OLEPropertyConstants;
26  import org.kuali.ole.sys.businessobject.UnitOfMeasure;
27  import org.kuali.ole.sys.document.validation.GenericValidation;
28  import org.kuali.ole.sys.document.validation.event.AttributedDocumentEvent;
29  import org.kuali.rice.kns.service.DataDictionaryService;
30  import org.kuali.rice.krad.service.BusinessObjectService;
31  import org.kuali.rice.krad.util.GlobalVariables;
32  
33  import java.util.HashMap;
34  import java.util.Map;
35  
36  public class PurchasingUnitOfMeasureValidation extends GenericValidation {
37  
38      private PurApItem itemForValidation;
39      private DataDictionaryService dataDictionaryService;
40      private BusinessObjectService businessObjectService;
41  
42      /**
43       * Validates that if the item type is quantity based, the unit of measure is required.
44       */
45      @Override
46      public boolean validate(AttributedDocumentEvent event) {
47          boolean valid = true;
48          PurchasingItemBase purItem = (PurchasingItemBase) itemForValidation;
49          GlobalVariables.getMessageMap().addToErrorPath(PurapConstants.ITEM_TAB_ERRORS);
50  
51          // Validations for quantity based item type
52          if (purItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
53              String uomCode = purItem.getItemUnitOfMeasureCode();
54              if (StringUtils.isEmpty(uomCode)) {
55                  valid = false;
56                  String attributeLabel = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(purItem.getClass().getName()).
57                          getAttributeDefinition(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE).
58                          getLabel();
59                  GlobalVariables.getMessageMap().putError(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + purItem.getItemIdentifierString());
60              } else {
61                  //Find out whether the unit of measure code has existed in the database
62                  Map<String, String> fieldValues = new HashMap<String, String>();
63                  fieldValues.put(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, purItem.getItemUnitOfMeasureCode());
64                  if (businessObjectService.countMatching(UnitOfMeasure.class, fieldValues) != 1) {
65                      //This is the case where the unit of measure code on the item does not exist in the database.
66                      valid = false;
67                      GlobalVariables.getMessageMap().putError(OLEPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE, PurapKeyConstants.PUR_ITEM_UNIT_OF_MEASURE_CODE_INVALID, " in " + purItem.getItemIdentifierString());
68                  }
69              }
70          }
71  
72          GlobalVariables.getMessageMap().clearErrorPath();
73  
74          // MSU Contribution OLEMI-5041 DTT-3371 OLECNTRB-955
75          if (purItem.getItemType().isAmountBasedGeneralLedgerIndicator()
76                  && StringUtils.isNotBlank(purItem.getItemUnitOfMeasureCode())) {
77              valid = false;
78              if(itemForValidation.getItemLineNumber() != null){
79              String errorPrefix = OLEPropertyConstants.DOCUMENT + "." + PurapPropertyConstants.ITEM + "["
80                      + (itemForValidation.getItemLineNumber() - 1) + "]."
81                      + PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE;
82              String attributeLabel = dataDictionaryService.getDataDictionary()
83                      .getBusinessObjectEntry(purItem.getClass().getName())
84                      .getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_OF_MEASURE_CODE).getLabel();
85              GlobalVariables.getMessageMap().putError(errorPrefix, PurapKeyConstants.ERROR_ITEM_UOM_NOT_ALLOWED,
86                      attributeLabel + " in " + purItem.getItemIdentifierString());
87          }
88          }
89  
90          return valid;
91      }
92  
93      public PurApItem getItemForValidation() {
94          return itemForValidation;
95      }
96  
97      public void setItemForValidation(PurApItem itemForValidation) {
98          this.itemForValidation = itemForValidation;
99      }
100 
101     public DataDictionaryService getDataDictionaryService() {
102         return dataDictionaryService;
103     }
104 
105     public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
106         this.dataDictionaryService = dataDictionaryService;
107     }
108 
109     public BusinessObjectService getBusinessObjectService() {
110         return businessObjectService;
111     }
112 
113     public void setBusinessObjectService(BusinessObjectService businessObjectService) {
114         this.businessObjectService = businessObjectService;
115     }
116 
117 }