View Javadoc

1   /*
2    * Copyright 2006-2007 the original author or authors.
3    *
4    * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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  
17  package org.springframework.batch.sample.domain.order.internal.valang;
18  
19  import java.math.BigDecimal;
20  import java.util.List;
21  
22  import org.springframework.batch.sample.domain.order.LineItem;
23  import org.springmodules.validation.valang.functions.AbstractFunction;
24  import org.springmodules.validation.valang.functions.Function;
25  
26  
27  /**
28   * @author peter.zozom
29   *
30   */
31  public class ValidateTotalPricesFunction extends AbstractFunction {
32      private static final BigDecimal BD_MIN = new BigDecimal(0.0);
33      private static final BigDecimal BD_MAX = new BigDecimal(99999999.99);
34      private static final BigDecimal BD_100 = new BigDecimal(100.00);
35  
36      public ValidateTotalPricesFunction(Function[] arguments, int line, int column) {
37          super(arguments, line, column);
38          definedExactNumberOfArguments(1);
39      }
40  
41      /**
42       * @see org.springmodules.validation.valang.functions.AbstractFunction#doGetResult(java.lang.Object)
43       */
44      @SuppressWarnings("unchecked")
45  	protected Object doGetResult(Object target) throws Exception {
46      	 List<LineItem> lineItems = (List<LineItem>) getArguments()[0].getResult(target);
47  
48           for (LineItem item : lineItems) {
49  
50              if ((BD_MIN.compareTo(item.getTotalPrice()) > 0)
51                      || (BD_MAX.compareTo(item.getTotalPrice()) < 0)) {
52                  return Boolean.FALSE;
53              }
54  
55              //calculate total price
56  
57              //discount coeficient = (100.00 - discountPerc) / 100.00
58              BigDecimal coef = BD_100.subtract(item.getDiscountPerc())
59                                      .divide(BD_100, 4, BigDecimal.ROUND_HALF_UP);
60  
61              //discountedPrice = (price * coef) - discountAmount
62              //at least one of discountPerc and discountAmount is 0 - this is validated by ValidateDiscountsFunction 
63              BigDecimal discountedPrice = item.getPrice().multiply(coef)
64                                               .subtract(item.getDiscountAmount());
65  
66              //price for single item = discountedPrice + shipping + handling
67              BigDecimal singleItemPrice = discountedPrice.add(item.getShippingPrice())
68                                                          .add(item.getHandlingPrice());
69  
70              //total price = singleItemPrice * quantity
71              BigDecimal quantity = new BigDecimal(item.getQuantity());
72              BigDecimal totalPrice = singleItemPrice.multiply(quantity)
73                                                     .setScale(2, BigDecimal.ROUND_HALF_UP);
74  
75              //calculatedPrice should equal to item.totalPrice  
76              if (totalPrice.compareTo(item.getTotalPrice()) != 0) {
77                  return Boolean.FALSE;
78              }
79          }
80  
81          return Boolean.TRUE;
82      }
83  }