KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > property > PropertyAction


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.property;
22
23 import java.util.List JavaDoc;
24
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27
28 import org.apache.struts.action.Action;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.DynaActionForm;
33 import com.methodhead.sitecontext.SiteContext;
34 import com.methodhead.auth.AuthAction;
35 import com.methodhead.auth.AuthUser;
36 import com.methodhead.auth.AuthUtil;
37 import com.methodhead.util.OperationContext;
38 import com.methodhead.util.StrutsUtil;
39
40 /**
41 <p>
42   Use <tt>PropertyAction</tt> to build a web interface to manage
43   properties. This action will respond to the mappings
44   <tt>/listProperties</tt>, <tt>/setPropertyForm</tt>, and
45   <tt>/setProperty</tt>, calling <tt>doListProperties()</tt>,
46   <tt>doSetPropertyForm()</tt>, and <tt>doSetProperty()</tt>
47   respectively.
48 </p>
49 <p>
50   The following are example mappings:
51 </p>
52 <xmp>
53   <action
54     path = "/listProperties"
55     type = "com.methodhead.property.PropertyAction"
56     scope = "request"
57     name = "propertyForm"
58     input = "/properties.jsp"
59     validate = "false">
60     <forward name="list" path="/properties.jsp"/>
61   </action>
62
63   <action
64     path = "/setPropertyForm"
65     type = "com.methodhead.property.PropertyAction"
66     scope = "request"
67     name = "propertyForm"
68     input = "/property.jsp"
69     validate = "false">
70   </action>
71
72   <action
73     path = "/setProperty"
74     type = "com.methodhead.property.PropertyAction"
75     scope = "request"
76     name = "propertyForm"
77     input = "/property.jsp"
78     validate = "false">
79     <forward name="saved" path="/listProperties.do"/>
80   </action>
81 </xmp>
82 <p>
83   The action expects a <tt>DynaActionForm</tt> with at least the
84   following configuration:
85 </p>
86 <xmp>
87   <form-bean
88     name = "propertyForm"
89     dynamic = "true"
90     type = "org.apache.struts.action.DynaActionForm">
91
92     <form-property name="name" type="java.lang.String"/>
93     <form-property name="value" type="java.lang.String"/>
94     <form-property name="description" type="java.lang.String"/>
95     <form-property name="system" type="java.lang.String"/>
96     <form-property name="submit" type="java.lang.String"/>
97     <form-property name="properties" type="java.util.List" />
98
99   </form-bean>
100 </xmp>
101  */

102 public class PropertyAction
103 extends
104   AuthAction {
105
106   // constructors /////////////////////////////////////////////////////////////
107

108   // constants ////////////////////////////////////////////////////////////////
109

110   // classes //////////////////////////////////////////////////////////////////
111

112   // methods //////////////////////////////////////////////////////////////////
113

114   /**
115    * Loads all properties and puts them in
116    * <tt>properties</tt>.
117    */

118   protected ActionForward doListProperties(
119     OperationContext op,
120     PropertyPolicy policy )
121   throws
122     Exception JavaDoc {
123
124     //
125
// authorized?
126
//
127
String JavaDoc msg = policy.isListPropertiesAuthorized( op );
128     if ( msg != null ) {
129       StrutsUtil.addMessage( op.request, msg, null, null, null );
130       return op.mapping.findForward( "accessDenied" );
131     }
132
133     op.form.set( "properties", Property.loadAll( SiteContext.getContext( op.request ) ) );
134
135     return op.mapping.findForward( "list" );
136   }
137
138   /**
139    * Loads the property named <tt>name</tt> and sets <tt>value</tt>,
140    * <tt>description</tt>, and <tt>system</tt>.
141    */

142   protected ActionForward doSetPropertyForm(
143     OperationContext op,
144     PropertyPolicy policy )
145   throws
146     Exception JavaDoc {
147
148     //
149
// authorized?
150
//
151
String JavaDoc msg = policy.isSetPropertyFormAuthorized( op );
152     if ( msg != null ) {
153       StrutsUtil.addMessage( op.request, msg, null, null, null );
154       return op.mapping.findForward( "accessDenied" );
155     }
156
157     Property p = new Property();
158     p.setSiteContext( SiteContext.getContext( op.request ) );
159     p.loadForName( ( String JavaDoc )op.form.get( "name" ) );
160
161     op.form.set( "value", p.getString( "value" ) );
162     op.form.set( "description", p.getString( "description" ) );
163     op.form.set( "system", p.getBoolean( "system_property" ) ? "on" : "" );
164
165     return new ActionForward( op.mapping.getInput() );
166   }
167
168   /**
169    * Loads the property named <tt>name</tt> and sets its value to
170    * <tt>value</tt>.
171    */

172   protected ActionForward doSetProperty(
173     OperationContext op,
174     PropertyPolicy policy )
175   throws
176     Exception JavaDoc {
177
178     //
179
// authorized?
180
//
181
String JavaDoc msg = policy.isSetPropertyAuthorized( op );
182     if ( msg != null ) {
183       StrutsUtil.addMessage( op.request, msg, null, null, null );
184       return op.mapping.findForward( "accessDenied" );
185     }
186
187     Property.setProperty(
188       SiteContext.getContext( op.request ),
189       ( String JavaDoc )op.form.get( "name" ),
190       ( String JavaDoc )op.form.get( "value" ) );
191
192     Property p = new Property();
193     p.setSiteContext( SiteContext.getContext( op.request ) );
194     p.loadForName( ( String JavaDoc )op.form.get( "name" ) );
195
196     op.form.set( "value", p.getString( "value" ) );
197     op.form.set( "description", p.getString( "description" ) );
198     op.form.set( "system", p.getBoolean( "system_property" ) ? "on" : "" );
199
200     return op.mapping.findForward( "saved" );
201   }
202
203   public ActionForward doExecute(
204     ActionMapping mapping,
205     ActionForm form,
206     HttpServletRequest JavaDoc request,
207     HttpServletResponse JavaDoc response )
208   throws
209     Exception JavaDoc {
210
211     //
212
// get some things we'll need
213
//
214
DynaActionForm dynaForm = ( DynaActionForm )form;
215     PropertyPolicy policy = ( PropertyPolicy )StrutsUtil.getPolicy( mapping );
216     AuthUser user = AuthUtil.getUser( request );
217
218     OperationContext op = new OperationContext( mapping, dynaForm, request, response, user );
219
220     if ( mapping.getPath().equals( "/listProperties" ) ) {
221       return doListProperties( op, policy );
222     }
223
224     if ( mapping.getPath().equals( "/setPropertyForm" ) ) {
225       return doSetPropertyForm( op, policy );
226     }
227
228     if ( mapping.getPath().equals( "/setProperty" ) ) {
229       return doSetProperty( op, policy );
230     }
231
232     throw
233       new Exception JavaDoc( "Unexpected mapping path \"" + mapping.getPath() + "\"" );
234   }
235
236   // properties ///////////////////////////////////////////////////////////////
237

238   // attributes ///////////////////////////////////////////////////////////////
239
}
240
Popular Tags