1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
28
29
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
39
40 @SuppressWarnings("unchecked")
41 protected Object doGetResult(Object target) throws Exception {
42
43 int count = (Integer) getArguments()[0].getResult(target);
44 Object value = getArguments()[1].getResult(target);
45
46 Boolean result;
47
48
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 }