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.ItemFields;
19  import org.kuali.ole.module.purap.PurapConstants.ItemTypeCodes;
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.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.rice.kns.service.DataDictionaryService;
27  import org.kuali.rice.krad.util.GlobalVariables;
28  import org.kuali.rice.krad.util.ObjectUtils;
29  
30  import java.math.BigDecimal;
31  
32  public class PurchasingItemUnitPriceValidation extends GenericValidation {
33  
34      private PurApItem itemForValidation;
35      private DataDictionaryService dataDictionaryService;
36  
37      /**
38       * Validates the unit price for all applicable item types. It validates that the unit price field was
39       * entered on the item, and that the price is in the right range for the item type.
40       */
41      public boolean validate(AttributedDocumentEvent event) {
42          boolean valid = true;
43          if (itemForValidation.getItemType().isLineItemIndicator()) {
44              if (ObjectUtils.isNull(itemForValidation.getItemUnitPrice())) {
45                  valid = false;
46                  String attributeLabel = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(itemForValidation.getClass().getName()).
47                          getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_PRICE).getLabel();
48                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, OLEKeyConstants.ERROR_REQUIRED, attributeLabel + " in " + itemForValidation.getItemIdentifierString());
49              }
50          }
51  
52          if (ObjectUtils.isNotNull(itemForValidation.getItemUnitPrice())) {
53              if ((BigDecimal.ZERO.compareTo(itemForValidation.getItemUnitPrice()) > 0) && ((!itemForValidation.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_ORDER_DISCOUNT_CODE)) && (!itemForValidation.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_TRADE_IN_CODE)))) {
54                  // If the item type is not full order discount or trade in items, don't allow negative unit price.
55                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, PurapKeyConstants.ERROR_ITEM_AMOUNT_BELOW_ZERO, ItemFields.UNIT_COST, itemForValidation.getItemIdentifierString());
56                  valid = false;
57              } else if ((BigDecimal.ZERO.compareTo(itemForValidation.getItemUnitPrice()) < 0) && ((itemForValidation.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_ORDER_DISCOUNT_CODE)) || (itemForValidation.getItemTypeCode().equals(ItemTypeCodes.ITEM_TYPE_TRADE_IN_CODE)))) {
58                  // If the item type is full order discount or trade in items, its unit price must be negative.
59                  GlobalVariables.getMessageMap().putError(PurapPropertyConstants.ITEM_UNIT_PRICE, PurapKeyConstants.ERROR_ITEM_AMOUNT_NOT_BELOW_ZERO, ItemFields.UNIT_COST, itemForValidation.getItemIdentifierString());
60                  valid = false;
61              }
62          }
63  
64          return valid;
65      }
66  
67      public PurApItem getItemForValidation() {
68          return itemForValidation;
69      }
70  
71      public void setItemForValidation(PurApItem itemForValidation) {
72          this.itemForValidation = itemForValidation;
73      }
74  
75      public DataDictionaryService getDataDictionaryService() {
76          return dataDictionaryService;
77      }
78  
79      public void setDataDictionaryService(DataDictionaryService dataDictionaryService) {
80          this.dataDictionaryService = dataDictionaryService;
81      }
82  
83  }