01 /**
02 *
03 * All content copyright (c) 2003-2008 Terracotta, Inc.,
04 * except as may otherwise be noted in a separate copyright notice.
05 * All rights reserved.
06 *
07 */
08 package demo.continuations;
09
10 import com.uwyn.rife.continuations.ContinuationContext;
11 import com.uwyn.rife.engine.Element;
12 import com.uwyn.rife.engine.annotations.Elem;
13 import com.uwyn.rife.engine.annotations.Param;
14 import com.uwyn.rife.engine.annotations.Submission;
15 import com.uwyn.rife.engine.annotations.SubmissionBean;
16 import com.uwyn.rife.site.ValidatedConstrained;
17 import com.uwyn.rife.template.Template;
18
19 /**
20 * This element handles a basic multi-step order checkout process. <p>
21 *
22 * Continuations are used to handle the intermediate data submissions. By
23 * providing a custom <code>clone()</code> implementation, the individual
24 * steps are prefilled with earlier submitted data if the user has pressed
25 * the back button.
26 *
27 *@author Geert Bevin (gbevin[remove] at uwyn dot com)
28 */
29 @Elem(
30 submissions={
31 @Submission(
32 name="selectShipping",
33 beans={@SubmissionBean(beanclass=OrderData.class, group=OrderDataMetaData.GROUP_SHIPPING) }) ,
34 @Submission(
35 name="provideCreditCard",
36 beans={@SubmissionBean(beanclass=OrderData.class, group=OrderDataMetaData.GROUP_CREDITCARD) },
37 params={@Param(name=Order.PARAM_BACK) })
38 })
39 public class Order extends Element {
40
41 private OrderData order = new OrderData();
42 /**
43 * Description of the Field
44 */
45 public static final String PARAM_BACK = "back";
46
47 public void processElement() {
48 Template template = getHtmlTemplate("order");
49
50 // handle the submission of the shipping details
51 do {
52 generateForm(template, order);
53 template.setBlock("content_form", "content_shipping");
54 print(template);
55 pause();
56
57 template.clear();
58 ((ValidatedConstrained) order).resetValidation();
59 fillSubmissionBean(order);
60 } while (duringStepBack() || !((ValidatedConstrained) order).validateGroup(OrderDataMetaData.GROUP_SHIPPING));
61
62 // handle the submission of the credit card details
63 do {
64 generateForm(template, order);
65 template.setBlock("content_form", "content_creditcard");
66 print(template);
67 pause();
68
69 template.clear();
70 ((ValidatedConstrained) order).resetValidation();
71 fillSubmissionBean(order);
72 if (hasParameterValue(PARAM_BACK)) {
73 stepBack();
74 }
75 } while (!((ValidatedConstrained) order).validateGroup(OrderDataMetaData.GROUP_CREDITCARD));
76
77 // provide an overview of everything that has been submitted
78 template.setBean(order);
79 template.setBlock("content", "content_overview");
80 print(template);
81
82 // remove any continuation contexts that are active in this tree
83 ContinuationContext.getActiveContext().removeContextTree();
84 }
85
86 public Object clone()
87 throws CloneNotSupportedException {
88 // This clone implementation uses the standard Element clone method.
89 // The order member variable will be preserved as-is however.
90 // The result is that even when the user presses the back button and
91 // re-submits a previous step, the earlier submitted data will be
92 // used to automatically fill in the already answered steps.
93 Order cloned = (Order) super.clone();
94 cloned.order = order;
95
96 return cloned;
97 }
98 }
|