Removed untyped contructor from ComponentRegistration and add a protected setter.
[castle.git] / MonoRail / Castle.MonoRail.ActiveRecordSupport / Scaffold / UpdateAction.cs
blobe2d45d46b7454f2e8739ff2bcb9a90bd507baa06
1 // Copyright 2004-2008 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 Castle.MonoRail.ActiveRecordSupport.Scaffold
17 using System;
18 using System.Collections;
19 using System.Reflection;
20 using Castle.ActiveRecord;
21 using Castle.Components.Common.TemplateEngine;
22 using Castle.MonoRail.Framework;
24 /// <summary>
25 /// Performs the update
26 /// </summary>
27 /// <remarks>
28 /// Searchs for a template named <c>create{name}</c>
29 /// </remarks>
30 public class UpdateAction : AbstractScaffoldAction
32 public UpdateAction(Type modelType, ITemplateEngine templateEngine, bool useModelName, bool useDefaultLayout) :
33 base(modelType, templateEngine, useModelName, useDefaultLayout)
37 protected override string ComputeTemplateName(IControllerContext controller)
39 return controller.Name + "\\update{1}";
42 protected override void PerformActionProcess(IEngineContext engineContext, IController controller, IControllerContext controllerContext)
44 object instance = null;
46 try
48 AssertIsPost(engineContext.Request.HttpMethod);
50 instance = binder.BindObject(Model.Type, Model.Type.Name, builder.BuildSourceNode(engineContext.Request.Form));
52 CommonOperationUtils.SaveInstance(instance, controller, errors, ref prop2Validation, false);
54 SessionScope.Current.Flush();
56 if (UseModelName)
58 engineContext.Response.RedirectUsingRoute(
59 controllerContext.AreaName, controllerContext.Name, "list" + Model.Type.Name, true);
61 else
63 engineContext.Response.RedirectUsingRoute(
64 controllerContext.AreaName, controllerContext.Name, "list", true);
67 catch(Exception ex)
69 errors.Add("Could not save " + Model.Type.Name + ". " + ex.Message);
72 if (errors.Count != 0)
74 engineContext.Flash[Model.Type.Name] = instance;
75 engineContext.Flash["errors"] = errors;
77 PropertyInfo keyProp = ObtainPKProperty();
78 IDictionary props = new Hashtable();
80 if (instance != null)
82 props[keyProp.Name] = keyProp.GetValue(instance, null);
85 if (UseModelName)
87 engineContext.Response.Redirect(controllerContext.AreaName, controllerContext.Name, "edit" + Model.Type.Name, props);
89 else
91 engineContext.Response.Redirect(controllerContext.AreaName, controllerContext.Name, "edit", props);
96 protected override void RenderStandardHtml(IEngineContext engineContext, IController controller, IControllerContext controllerContext)