Relaxed the need for Types that represent database boundaries to extend from ActiveRe...
[castle.git] / Samples / MonoRail / WizardSample / WizardSampleSite / Controllers / SimpleWizardController.cs
blob336b5d5d43cd6da0401a64a139f76ba85e7acfde
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace WizardSampleSite.Controllers
17 using System;
18 using System.Collections;
20 using Castle.MonoRail.Framework;
22 using WizardSampleSite.Model;
25 [DynamicActionProvider( typeof(WizardActionProvider) )]
26 public class SimpleWizardController : Controller, IWizardController
28 public WizardStepPage[] GetSteps(IRailsEngineContext context)
30 return new WizardStepPage[]
32 new IntroductionStep(),
33 new MainInfoStep(),
34 new SubscribeStep(),
35 new ConfirmationStep(),
36 new ResultStep()
40 public void OnWizardStart()
44 public bool OnBeforeStep(string wizardName, string stepName, WizardStepPage step)
46 return true;
49 public void OnAfterStep(string wizardName, string stepName, WizardStepPage step)
54 // Please note that we put the steps on the same file
55 // for brevity's sake
57 /// <summary>
58 /// Presents a small introduction
59 /// </summary>
60 class IntroductionStep : WizardStepPage
64 class MainInfoStep : WizardStepPage
66 protected override void Reset()
68 Session.Remove("account");
71 public void Save([DataBind("account")] Account accountFromForm)
73 Account account = WizSessionUtil.GetAccountFromSession(Session);
75 // Update the account on session with the data from the form
77 account.Name = accountFromForm.Name;
78 account.Username = accountFromForm.Username;
79 account.Email = accountFromForm.Email;
80 account.Pwd = accountFromForm.Pwd;
81 account.PwdConfirmation = accountFromForm.PwdConfirmation;
83 // Some naive validation
85 IList errors = ValidateAccount(account);
87 if (errors.Count != 0)
89 // Not good
91 Flash["errors"] = errors;
93 // User can't go to the next step yet
95 RedirectToStep("MainInfoStep");
97 else
99 DoNavigate();
103 private IList ValidateAccount(Account account)
105 IList errors = new ArrayList();
107 if (account.Name == null || account.Name.Length == 0)
109 errors.Add("Full name field must be filled");
111 if (account.Username == null || account.Username.Length == 0)
113 errors.Add("User name field must be filled");
115 if (account.Email == null || account.Email.Length == 0)
117 errors.Add("E-mail field must be filled");
119 if (account.Pwd != account.PwdConfirmation)
121 errors.Add("Password don't match with confirmation");
124 return errors;
127 /// <summary>
128 /// Note that you can override
129 /// this method and render a different
130 /// view (the default behavior is to render
131 /// the step name, i.e. MainInfoStep in this step)
132 /// </summary>
133 protected override void RenderWizardView()
135 PropertyBag.Add("account", WizSessionUtil.GetAccountFromSession(Session));
137 base.RenderWizardView();
141 class SubscribeStep : WizardStepPage
143 /// <summary>
144 /// You can also use this to give the view some data.
145 /// Here we send the presaved (if any) selections
146 /// </summary>
147 protected override void RenderWizardView()
149 PropertyBag.Add("source", new String[] { "Sports", "Science", "Nature", "History" });
151 PropertyBag.Add("account", WizSessionUtil.GetAccountFromSession(Session));
153 base.RenderWizardView();
156 public void Save([DataBind("account")] Account accountFromForm)
158 Account account = WizSessionUtil.GetAccountFromSession(Session);
160 account.Interests = accountFromForm.Interests;
162 DoNavigate();
166 class ConfirmationStep : WizardStepPage
168 /// <summary>
169 /// This is an example of a pre-condition check.
170 /// </summary>
171 protected override bool IsPreConditionSatisfied(IRailsEngineContext context)
173 Account account = WizSessionUtil.GetAccountFromSession(Session);
175 if (account.IsEmpty)
177 Flash.Add("error", "No account information found. Please enter the data");
178 RedirectToStep("MainInfoStep");
179 return false;
182 return true;
185 protected override void RenderWizardView()
187 PropertyBag.Add("account", WizSessionUtil.GetAccountFromSession(Session));
189 base.RenderWizardView();
193 class ResultStep : WizardStepPage
195 protected override void RenderWizardView()
197 Account account = WizSessionUtil.GetAccountFromSession(Session);
199 // Here you would take the proper actions like creating
200 // the account in the database.
202 // AccountService.Create(account);
203 // Session.Remove("account");
205 base.RenderWizardView();
209 class WizSessionUtil
211 internal static Account GetAccountFromSession(IDictionary session)
213 Account account = session["stored.account"] as Account;
215 if (account == null)
217 account = new Account();
219 session["stored.account"] = account;
222 return account;