1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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 Castle
.MonoRail
.Framework
.Internal
18 using System
.Collections
;
21 /// Utility class for wizard related queries and operations
23 public static class WizardUtils
26 /// Constructs the wizard namespace.
28 /// <param name="controllerContext">The controller context.</param>
29 /// <returns></returns>
30 public static String
ConstructWizardNamespace(IControllerContext controllerContext
)
32 return String
.Format("wizard.{0}", controllerContext
.Name
);
36 /// Determines whether the current request is within a wizard context.
38 /// <param name="engineContext">The engine context.</param>
39 /// <param name="controllerContext">The controller context.</param>
41 /// <c>true</c> if is on wizard context; otherwise, <c>false</c>.
43 public static bool IsOnWizard(IEngineContext engineContext
, IControllerContext controllerContext
)
45 String wizardName
= WizardUtils
.ConstructWizardNamespace(controllerContext
);
46 return engineContext
.Session
.Contains(wizardName
+ "currentstepindex");
50 /// Determines whether the current wizard has a previous step.
52 /// <param name="engineContext">The engine context.</param>
53 /// <param name="controller">The controller.</param>
54 /// <param name="controllerContext">The controller context.</param>
56 /// <c>true</c> if has previous step; otherwise, <c>false</c>.
58 public static bool HasPreviousStep(IEngineContext engineContext
, IController controller
, IControllerContext controllerContext
)
60 if (!IsOnWizard(engineContext
, controllerContext
))
65 String wizardName
= WizardUtils
.ConstructWizardNamespace(controllerContext
);
67 int currentIndex
= (int) engineContext
.Session
[wizardName
+ "currentstepindex"];
69 return currentIndex
> 0;
73 /// Determines whether the current wizard has a next step.
75 /// <param name="engineContext">The engine context.</param>
76 /// <param name="controller">The controller.</param>
77 /// <param name="controllerContext">The controller context.</param>
79 /// <c>true</c> if has next step; otherwise, <c>false</c>.
81 public static bool HasNextStep(IEngineContext engineContext
, IController controller
, IControllerContext controllerContext
)
83 if (!IsOnWizard(engineContext
, controllerContext
))
88 String wizardName
= WizardUtils
.ConstructWizardNamespace(controllerContext
);
90 IList stepList
= (IList
) engineContext
.Items
["wizard.step.list"];
92 int currentIndex
= (int) engineContext
.Session
[wizardName
+ "currentstepindex"];
94 return (currentIndex
+ 1) < stepList
.Count
;
98 /// Gets the index of the current step.
100 /// <param name="engineContext">The engine context.</param>
101 /// <param name="controller">The controller.</param>
102 /// <param name="controllerContext">The controller context.</param>
103 /// <returns></returns>
104 public static int GetCurrentStepIndex(IEngineContext engineContext
, IController controller
, IControllerContext controllerContext
)
106 if (!IsOnWizard(engineContext
, controllerContext
))
111 String wizardName
= WizardUtils
.ConstructWizardNamespace(controllerContext
);
113 int curIndex
= (int) engineContext
.Session
[wizardName
+ "currentstepindex"];
119 /// Gets the name of the current step.
121 /// <param name="engineContext">The engine context.</param>
122 /// <param name="controller">The controller.</param>
123 /// <param name="controllerContext">The controller context.</param>
124 /// <returns></returns>
125 public static String
GetCurrentStepName(IEngineContext engineContext
, IController controller
, IControllerContext controllerContext
)
127 if (!IsOnWizard(engineContext
, controllerContext
))
132 String wizardName
= WizardUtils
.ConstructWizardNamespace(controllerContext
);
134 int curIndex
= (int) engineContext
.Session
[wizardName
+ "currentstepindex"];
136 IList stepList
= (IList
) engineContext
.Items
["wizard.step.list"];
138 return (String
) stepList
[curIndex
];
142 /// Gets the name of the previous step.
144 /// <param name="engineContext">The engine context.</param>
145 /// <param name="controller">The controller.</param>
146 /// <param name="controllerContext">The controller context.</param>
147 /// <returns></returns>
148 public static String
GetPreviousStepName(IEngineContext engineContext
, IController controller
, IControllerContext controllerContext
)
150 if (!IsOnWizard(engineContext
, controllerContext
))
155 String wizardName
= WizardUtils
.ConstructWizardNamespace(controllerContext
);
157 int curIndex
= (int) engineContext
.Session
[wizardName
+ "currentstepindex"];
159 IList stepList
= (IList
) engineContext
.Items
["wizard.step.list"];
161 if ((curIndex
- 1) >= 0)
163 return (String
) stepList
[curIndex
- 1];
170 /// Gets the name of the next step.
172 /// <param name="index">The step index.</param>
173 /// <param name="engineContext">The engine context.</param>
174 /// <param name="controller">The controller.</param>
175 /// <param name="controllerContext">The controller context.</param>
176 /// <returns></returns>
177 public static string GetStepName(int index
, IEngineContext engineContext
, IController controller
, IControllerContext controllerContext
)
179 if (!IsOnWizard(engineContext
, controllerContext
))
184 IList stepList
= (IList
) engineContext
.Items
["wizard.step.list"];
186 if ((index
) < stepList
.Count
)
188 return (String
) stepList
[index
];
195 /// Gets the name of the next step.
197 /// <param name="engineContext">The engine context.</param>
198 /// <param name="controller">The controller.</param>
199 /// <param name="controllerContext">The controller context.</param>
200 /// <returns></returns>
201 public static String
GetNextStepName(IEngineContext engineContext
, IController controller
, IControllerContext controllerContext
)
203 if (!IsOnWizard(engineContext
, controllerContext
))
208 String wizardName
= WizardUtils
.ConstructWizardNamespace(controllerContext
);
210 int curIndex
= (int) engineContext
.Session
[wizardName
+ "currentstepindex"];
212 IList stepList
= (IList
) engineContext
.Items
["wizard.step.list"];
214 if ((curIndex
+ 1) < stepList
.Count
)
216 return (String
) stepList
[curIndex
+ 1];
223 /// Registers the current step info/state.
225 /// <param name="engineContext">The engine context.</param>
226 /// <param name="controller">The controller.</param>
227 /// <param name="controllerContext">The controller context.</param>
228 /// <param name="actionName">Name of the action.</param>
229 public static void RegisterCurrentStepInfo(IEngineContext engineContext
, IController controller
,
230 IControllerContext controllerContext
, String actionName
)
232 IList stepList
= (IList
) engineContext
.Items
["wizard.step.list"];
234 for(int i
= 0; i
< stepList
.Count
; i
++)
236 String stepName
= (String
) stepList
[i
];
238 if (actionName
== stepName
)
240 RegisterCurrentStepInfo(engineContext
, controller
, controllerContext
, i
, stepName
);
247 /// Registers the current step info/state.
249 /// <param name="engineContext">The engine context.</param>
250 /// <param name="controller">The controller.</param>
251 /// <param name="controllerContext">The controller context.</param>
252 /// <param name="stepIndex">Index of the step.</param>
253 /// <param name="stepName">Name of the step.</param>
254 public static void RegisterCurrentStepInfo(IEngineContext engineContext
, IController controller
,
255 IControllerContext controllerContext
, int stepIndex
, String stepName
)
257 String wizardName
= WizardUtils
.ConstructWizardNamespace(controllerContext
);
259 engineContext
.Session
[wizardName
+ "currentstepindex"] = stepIndex
;
260 engineContext
.Session
[wizardName
+ "currentstep"] = stepName
;