1 // Copyright 2004-2007 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
.ActiveRecordScaffold
18 using System
.Runtime
.CompilerServices
;
19 using Castle
.MonoRail
.Framework
;
20 using Castle
.Components
.Common
.TemplateEngine
;
21 using Castle
.Components
.Common
.TemplateEngine
.NVelocityTemplateEngine
;
24 /// Uses the dynamic action infrastructure to
25 /// add new actions to an existing controller.
28 /// Provided that a controller uses <see cref="ScaffoldingAttribute"/>
29 /// like the following code:
31 /// [Scaffolding( typeof(Account) )]
32 /// public class AdminController : Controller
36 /// Then the following dynamic actions will be added:
37 /// <list type="bullet">
38 /// <item><term>newAccount</term>
39 /// <description>Presents a form to the user fill in order to create the item on the database</description>
41 /// <item><term>createAccount</term>
42 /// <description>Takes the information submited by the newAccount and creates the item</description>
44 /// <item><term>editAccount</term>
45 /// <description>Presents a form to the user fill in order to update the item on the database</description>
47 /// <item><term>updateAccount</term>
48 /// <description>Takes the information submited by the editAccount and changes the item</description>
50 /// <item><term>listAccount</term>
51 /// <description>Presents a paginated list of items saved</description>
53 /// <item><term>confirmAccount</term>
54 /// <description>Asks the user if he/she confirms the removal of the item</description>
56 /// <item><term>removeAccount</term>
57 /// <description>Attempt to remove the item and presents the results</description>
61 public class ScaffoldingSupport
: IScaffoldingSupport
63 private static ITemplateEngine templateEngine
= null;
65 public void Process(Controller controller
)
67 InitializeTemplateEngine();
69 bool useDefaultLayout
= controller
.MetaDescriptor
.Layout
== null;
71 if (controller
.MetaDescriptor
.Scaffoldings
.Count
== 1)
73 ScaffoldingAttribute scaffoldAtt
= (ScaffoldingAttribute
)
74 controller
.MetaDescriptor
.Scaffoldings
[0];
76 controller
.DynamicActions
["new"] =
77 new NewAction(scaffoldAtt
.Model
, templateEngine
, false, useDefaultLayout
);
78 controller
.DynamicActions
["create"] =
79 new CreateAction(scaffoldAtt
.Model
, templateEngine
, false, useDefaultLayout
);
80 controller
.DynamicActions
["edit"] =
81 new EditAction(scaffoldAtt
.Model
, templateEngine
, false, useDefaultLayout
);
82 controller
.DynamicActions
["update"] =
83 new UpdateAction(scaffoldAtt
.Model
, templateEngine
, false, useDefaultLayout
);
84 controller
.DynamicActions
["remove"] =
85 new RemoveAction(scaffoldAtt
.Model
, templateEngine
, false, useDefaultLayout
);
86 controller
.DynamicActions
["confirm"] =
87 new ConfirmRemoveAction(scaffoldAtt
.Model
, templateEngine
, false, useDefaultLayout
);
88 controller
.DynamicActions
["list"] =
89 new ListAction(scaffoldAtt
.Model
, templateEngine
, false, useDefaultLayout
);
93 foreach(ScaffoldingAttribute scaffoldAtt
in controller
.MetaDescriptor
.Scaffoldings
)
95 String name
= scaffoldAtt
.Model
.Name
;
97 controller
.DynamicActions
[String
.Format("new{0}", name
)] =
98 new NewAction(scaffoldAtt
.Model
, templateEngine
, true, useDefaultLayout
);
99 controller
.DynamicActions
[String
.Format("create{0}", name
)] =
100 new CreateAction(scaffoldAtt
.Model
, templateEngine
, true, useDefaultLayout
);
101 controller
.DynamicActions
[String
.Format("edit{0}", name
)] =
102 new EditAction(scaffoldAtt
.Model
, templateEngine
, true, useDefaultLayout
);
103 controller
.DynamicActions
[String
.Format("update{0}", name
)] =
104 new UpdateAction(scaffoldAtt
.Model
, templateEngine
, true, useDefaultLayout
);
105 controller
.DynamicActions
[String
.Format("remove{0}", name
)] =
106 new RemoveAction(scaffoldAtt
.Model
, templateEngine
, true, useDefaultLayout
);
107 controller
.DynamicActions
[String
.Format("confirm{0}", name
)] =
108 new ConfirmRemoveAction(scaffoldAtt
.Model
, templateEngine
, true, useDefaultLayout
);
109 controller
.DynamicActions
[String
.Format("list{0}", name
)] =
110 new ListAction(scaffoldAtt
.Model
, templateEngine
, true, useDefaultLayout
);
115 [MethodImpl(MethodImplOptions
.Synchronized
)]
116 private static void InitializeTemplateEngine()
118 if (templateEngine
== null)
120 NVelocityTemplateEngine nvelTemplateEng
= new NVelocityTemplateEngine();
122 #if USE_LOCAL_TEMPLATES
123 nvelTemplateEng
.TemplateDir
= @"E:\dev\castle\trunk\MonoRail\Castle.MonoRail.ActiveRecordScaffold\Templates\";
124 nvelTemplateEng
.BeginInit();
125 nvelTemplateEng
.EndInit();
127 nvelTemplateEng
.AddResourceAssembly("Castle.MonoRail.ActiveRecordScaffold");
128 nvelTemplateEng
.BeginInit();
129 nvelTemplateEng
.EndInit();
132 templateEngine
= nvelTemplateEng
;