01: /*
02: * Copyright 2007 Dan Shellman
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.iscreen.samples;
17:
18: import java.util.List;
19: import java.util.Locale;
20: import org.iscreen.DocumentationIterator;
21: import org.iscreen.ValidationException;
22:
23: import org.iscreen.ValidationFactory;
24: import org.iscreen.ValidationFactoryConfig;
25: import org.iscreen.ValidationServiceWrapper;
26:
27: /**
28: * This is a sample class that validates an order object graph.
29: *
30: * @author Shellman, Dan
31: */
32: public class OrderValidationSample {
33: private static final String VALIDATION_CONFIG = "org/iscreen/samples/my_validations.xml";
34:
35: public static void main(String[] args) {
36: ValidationFactoryConfig factory;
37: ValidationServiceWrapper service;
38: DocumentationIterator it;
39: Order order;
40:
41: factory = new ValidationFactoryConfig(
42: ValidationFactory.FACTORY_OGNL_XML, VALIDATION_CONFIG,
43: Locale.getDefault(), null);
44: service = new ValidationServiceWrapper(factory,
45: "org.iscreen.samples.order");
46:
47: order = constructOrder();
48: try {
49: it = service.getDocumentation();
50: System.out.println("Documentation:");
51: for (int i = 0; i < it.getCount(); i++) {
52: System.out
53: .println(it.getName(i) + " = " + it.getDoc(i));
54: }
55: System.out.println("End Documentation\n\n");
56:
57: service.validate(order);
58: System.out.println("Validation successful!");
59: } catch (ValidationException e) {
60: List messages;
61:
62: System.out.println("Validation Failures found!");
63: messages = e.getFailureMessages();
64: for (int i = 0; i < messages.size(); i++) {
65: System.out.println(messages.get(i));
66: }
67: }
68: } //end main()
69:
70: private static Order constructOrder() {
71: Order order = new Order("abc123def4");
72: Customer cust = new Customer("123987456012");
73: Address addr = new Address();
74: LineItem item1 = new LineItem("p123456789z");
75: LineItem item2 = new LineItem("p987654321m");
76:
77: order.addItem(item1);
78: order.addItem(item2);
79: order.setCustomer(cust);
80: cust.setAddress(addr);
81: cust.setCreditCard("1234567890123456");
82: cust.setName("Acme, Inc.");
83: addr.setAddress1("123 Main St.");
84: addr.setCity("some city");
85: addr.setState("NY");
86: addr.setZip("12345");
87:
88: return order;
89: } //end constructOrder()
90: } //end OrderValidationSample
|