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.util.List;
20  
21  import org.springframework.batch.sample.domain.order.LineItem;
22  import org.springmodules.validation.valang.functions.AbstractFunction;
23  import org.springmodules.validation.valang.functions.Function;
24  
25  
26  /**
27   * Validates total items count in Order.
28   *
29   * @author peter.zozom
30   */
31  public class TotalOrderItemsFunction extends AbstractFunction {
32      public TotalOrderItemsFunction(Function[] arguments, int line, int column) {
33          super(arguments, line, column);
34          definedExactNumberOfArguments(2);
35      }
36  
37      /**
38       * @see org.springmodules.validation.valang.functions.AbstractFunction#doGetResult(java.lang.Object)
39       */
40      @SuppressWarnings("unchecked")
41  	protected Object doGetResult(Object target) throws Exception {
42          //get arguments
43          int count = (Integer) getArguments()[0].getResult(target);
44          Object value = getArguments()[1].getResult(target);
45  
46          Boolean result;
47  
48          //count items in list of order lines
49          if (value instanceof List) {
50              int totalItems = 0;
51  
52  			for (LineItem lineItem : ((List<LineItem>) value)) {
53  				totalItems += lineItem.getQuantity();
54  			}
55  
56  			result = (totalItems == count) ? Boolean.TRUE : Boolean.FALSE;
57          } else {
58              throw new Exception("No list for validation");
59          }
60  
61          return result;
62      }
63  }