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