KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > shim > ModuleAction


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.shim;
22
23 import org.apache.struts.action.Action;
24 import org.apache.struts.action.ActionMapping;
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.DynaActionForm;
27 import org.apache.struts.action.ActionForward;
28
29 import javax.servlet.http.HttpServletRequest JavaDoc;
30 import javax.servlet.http.HttpServletResponse JavaDoc;
31
32 import com.methodhead.auth.AuthAction;
33 import com.methodhead.util.OperationContext;
34 import com.methodhead.sitecontext.SiteContext;
35 import com.methodhead.auth.AuthUser;
36 import com.methodhead.auth.AuthUtil;
37 import com.methodhead.aikp.IntKey;
38 import com.methodhead.util.StrutsUtil;
39 import com.methodhead.event.Event;
40 import org.apache.commons.lang.StringUtils;
41
42 public class ModuleAction
43 extends
44   AuthAction {
45
46   // constructors /////////////////////////////////////////////////////////////
47

48   // constants ////////////////////////////////////////////////////////////////
49

50   // classes //////////////////////////////////////////////////////////////////
51

52   // methods //////////////////////////////////////////////////////////////////
53

54   /**
55    * Populates the action form for the text module at <tt>form.pageid</tt> and
56    * <tt>form.panel</tt>.
57    */

58   protected ActionForward doConfigureTextModuleForm(
59     OperationContext op,
60     ShimPolicy policy )
61   throws
62     Exception JavaDoc {
63
64     String JavaDoc msg = policy.isConfigureTextModuleAuthorized( op );
65     if ( msg != null ) {
66       StrutsUtil.addMessage( op.request, msg, null, null, null );
67       return op.mapping.findForward( "accessDenied" );
68     }
69
70     TextModule textModule = policy.newTextModule();
71     Page page = policy.newPage();
72     page.setSiteContext( SiteContext.getContext( op.request ) );
73
74     page.load( new IntKey( op.form.get( "pageid" ) ) );
75     textModule.init( page, ( String JavaDoc )op.form.get( "panel" ) );
76     textModule.load();
77
78     op.form.set( "text", textModule.getString( "value" ) );
79
80     if ( textModule.getInt( "htmlfragment_id" ) != 0 ) {
81       op.form.set( "usefragment", "yes" );
82       op.form.set( "fragment", "" + textModule.getInt( "htmlfragment_id" ) );
83     }
84
85     return op.mapping.findForward( "form" );
86   }
87
88   /**
89    * Updates the text model at <tt>form.pageid</tt> and
90    * <tt>form.panel</tt>.
91    */

92   protected ActionForward doConfigureTextModule(
93     OperationContext op,
94     ShimPolicy policy )
95   throws
96     Exception JavaDoc {
97
98     String JavaDoc msg = policy.isConfigureTextModuleAuthorized( op );
99     if ( msg != null ) {
100       StrutsUtil.addMessage( op.request, msg, null, null, null );
101       return op.mapping.findForward( "accessDenied" );
102     }
103
104     TextModule textModule = policy.newTextModule();
105     Page page = policy.newPage();
106     page.setSiteContext( SiteContext.getContext( op.request ) );
107
108     page.load( new IntKey( op.form.get( "pageid" ) ) );
109     textModule.init( page, ( String JavaDoc )op.form.get( "panel" ) );
110     textModule.load();
111
112     textModule.setString( "value", ( String JavaDoc )op.form.get( "text" ) );
113
114     if ( !StringUtils.isBlank( ( String JavaDoc )op.form.get( "usefragment" ) ) ) {
115       textModule.setInt(
116         "htmlfragment_id",
117         Integer.parseInt( ( String JavaDoc )op.form.get( "fragment" ) ) );
118     }
119     else {
120       textModule.setInt( "htmlfragment_id", 0 );
121     }
122
123     textModule.save();
124
125     Event.log(
126       SiteContext.getContext( op.request ),
127       op.user.getLogin(),
128       "shim",
129       "Configured text module for page \"" + page.getString( "title" ) +
130         "\" and panel \"" + op.form.get( "panel" ) + "\"." );
131
132     return new ActionForward( "/editPage.do?id=" + page.getInt( "id" ) );
133   }
134
135   /**
136    * Populates the action form for the text module at <tt>form.pageid</tt> and
137    * <tt>form.panel</tt>.
138    */

139   protected ActionForward doConfigureNavModuleForm(
140     OperationContext op,
141     ShimPolicy policy )
142   throws
143     Exception JavaDoc {
144
145     String JavaDoc msg = policy.isConfigureNavModuleAuthorized( op );
146     if ( msg != null ) {
147       StrutsUtil.addMessage( op.request, msg, null, null, null );
148       return op.mapping.findForward( "accessDenied" );
149     }
150
151     NavModule navModule = policy.newNavModule();
152     Page page = policy.newPage();
153     page.setSiteContext( SiteContext.getContext( op.request ) );
154
155     page.load( new IntKey( op.form.get( "pageid" ) ) );
156     navModule.init( page, ( String JavaDoc )op.form.get( "panel" ) );
157     navModule.load();
158
159     if ( NavModule.TYPE_TEXTSEPARATED.equals( navModule.getString( "type" ) ) )
160       op.form.set( "type", "textseparated" );
161     else if ( NavModule.TYPE_JSP.equals( navModule.getString( "type" ) ) )
162       op.form.set( "type", "jsp" );
163     else if ( NavModule.TYPE_FOLDING.equals( navModule.getString( "type" ) ) )
164       op.form.set( "type", "folding" );
165
166     op.form.set( "header", navModule.getString( "header" ) );
167     op.form.set( "link", navModule.getString( "link" ) );
168     op.form.set( "curlink", navModule.getString( "curlink" ) );
169     op.form.set( "footer", navModule.getString( "footer" ) );
170
171     op.form.set( "separator", navModule.getString( "separator" ) );
172     op.form.set( "hideroot", navModule.getBoolean( "hideroot" ) ? "yes" : "" );
173     op.form.set( "jsp", navModule.getString( "jsp" ) );
174
175     op.form.set( "startlev", "" + navModule.getInt( "startlev" ) );
176     op.form.set( "contextlev", "" + navModule.getInt( "contextlev" ) );
177     op.form.set( "depthlev", "" + navModule.getInt( "depthlev" ) );
178     op.form.set( "toplev", "" + navModule.getInt( "toplev" ) );
179
180     return op.mapping.findForward( "form" );
181   }
182
183   /**
184    * Updates the text model at <tt>form.pageid</tt> and
185    * <tt>form.panel</tt>.
186    */

187   protected ActionForward doConfigureNavModule(
188     OperationContext op,
189     ShimPolicy policy )
190   throws
191     Exception JavaDoc {
192
193     String JavaDoc msg = policy.isConfigureNavModuleAuthorized( op );
194     if ( msg != null ) {
195       StrutsUtil.addMessage( op.request, msg, null, null, null );
196       return op.mapping.findForward( "accessDenied" );
197     }
198
199     //
200
// load the page
201
//
202
Page page = policy.newPage();
203     page.setSiteContext( SiteContext.getContext( op.request ) );
204     page.load( new IntKey( op.form.get( "pageid" ) ) );
205
206     //
207
// load the nav module
208
//
209
NavModule navModule = policy.newNavModule();
210     navModule.init( page, ( String JavaDoc )op.form.get( "panel" ) );
211     navModule.load();
212
213     //
214
// set common fields
215
//
216
navModule.setString( "header", ( String JavaDoc )op.form.get( "header" ) );
217     navModule.setString( "link", ( String JavaDoc )op.form.get( "link" ) );
218     navModule.setString( "curlink", ( String JavaDoc )op.form.get( "curlink" ) );
219     navModule.setString( "footer", ( String JavaDoc )op.form.get( "footer" ) );
220
221     //
222
// set type-specific fields
223
//
224
if ( "textseparated".equals( op.form.get( "type" ) ) ) {
225
226       //
227
// text-separated fields
228
//
229
navModule.setString( "type", NavModule.TYPE_TEXTSEPARATED );
230       navModule.setString(
231         "separator", ( String JavaDoc )op.form.get( "separator" ) );
232       navModule.setBoolean(
233         "hideroot",
234         !StringUtils.isBlank( ( String JavaDoc )op.form.get( "hideroot" ) ) );
235     }
236     else if ( "jsp".equals( op.form.get( "type" ) ) ) {
237
238       //
239
// jsp fields
240
//
241
navModule.setString( "type", NavModule.TYPE_JSP );
242       navModule.setString( "jsp", ( String JavaDoc )op.form.get( "jsp" ) );
243     }
244     else if ( "folding".equals( op.form.get( "type" ) ) ) {
245
246       //
247
// folding fields
248
//
249
navModule.setString( "type", NavModule.TYPE_FOLDING );
250       navModule.setInt(
251         "startlev", Integer.parseInt( ( String JavaDoc )op.form.get( "startlev" ) ) );
252       navModule.setInt(
253         "contextlev",
254         Integer.parseInt( ( String JavaDoc )op.form.get( "contextlev" ) ) );
255       navModule.setInt(
256         "depthlev", Integer.parseInt( ( String JavaDoc )op.form.get( "depthlev" ) ) );
257       navModule.setInt(
258         "toplev", Integer.parseInt( ( String JavaDoc )op.form.get( "toplev" ) ) );
259     }
260     else
261       throw new ShimException(
262         "Unexpected nav type \"" + op.form.get( "type" ) + "\"" );
263
264     //
265
// save the module
266
//
267
navModule.save();
268
269     //
270
// log the event
271
//
272
Event.log(
273       SiteContext.getContext( op.request ),
274       op.user.getLogin(),
275       "shim",
276       "Configured nav module for page \"" + page.getString( "title" ) +
277         "\" and panel \"" + op.form.get( "panel" ) + "\"." );
278
279     return new ActionForward( "/editPage.do?id=" + page.getInt( "id" ) );
280   }
281
282   /**
283    * Populates the action form for the text module at <tt>form.pageid</tt> and
284    * <tt>form.panel</tt>.
285    */

286   protected ActionForward doConfigureIncludeModuleForm(
287     OperationContext op,
288     ShimPolicy policy )
289   throws
290     Exception JavaDoc {
291
292     String JavaDoc msg = policy.isConfigureIncludeModuleAuthorized( op );
293     if ( msg != null ) {
294       StrutsUtil.addMessage( op.request, msg, null, null, null );
295       return op.mapping.findForward( "accessDenied" );
296     }
297
298     IncludeModule includeModule = policy.newIncludeModule();
299     Page page = policy.newPage();
300     page.setSiteContext( SiteContext.getContext( op.request ) );
301
302     page.load( new IntKey( op.form.get( "pageid" ) ) );
303     includeModule.init( page, ( String JavaDoc )op.form.get( "panel" ) );
304     includeModule.load();
305
306     op.form.set( "jsp", includeModule.getString( "jsp" ) );
307
308     return op.mapping.findForward( "form" );
309   }
310
311   /**
312    * Updates the text model at <tt>form.pageid</tt> and
313    * <tt>form.panel</tt>.
314    */

315   protected ActionForward doConfigureIncludeModule(
316     OperationContext op,
317     ShimPolicy policy )
318   throws
319     Exception JavaDoc {
320
321     String JavaDoc msg = policy.isConfigureIncludeModuleAuthorized( op );
322     if ( msg != null ) {
323       StrutsUtil.addMessage( op.request, msg, null, null, null );
324       return op.mapping.findForward( "accessDenied" );
325     }
326
327     IncludeModule includeModule = policy.newIncludeModule();
328     Page page = policy.newPage();
329     page.setSiteContext( SiteContext.getContext( op.request ) );
330
331     page.load( new IntKey( op.form.get( "pageid" ) ) );
332     includeModule.init( page, ( String JavaDoc )op.form.get( "panel" ) );
333     includeModule.load();
334
335     includeModule.setString( "jsp", ( String JavaDoc )op.form.get( "jsp" ) );
336     includeModule.save();
337
338     Event.log(
339       SiteContext.getContext( op.request ),
340       op.user.getLogin(),
341       "shim",
342       "Configured include module for page \"" + page.getString( "title" ) +
343         "\" and panel \"" + op.form.get( "panel" ) + "\"." );
344
345     return new ActionForward( "/editPage.do?id=" + page.getInt( "id" ) );
346   }
347
348   public ActionForward doExecute(
349     ActionMapping mapping,
350     ActionForm form,
351     HttpServletRequest JavaDoc request,
352     HttpServletResponse JavaDoc response )
353   throws
354     Exception JavaDoc {
355
356     //
357
// set up the operation context
358
//
359
DynaActionForm dynaForm = ( DynaActionForm )form;
360     ShimPolicy policy = ( ShimPolicy )StrutsUtil.getPolicy( mapping );
361     AuthUser user = AuthUtil.getUser( request );
362
363     OperationContext op =
364       new OperationContext(
365         mapping,
366         ( DynaActionForm )form,
367         request, response,
368         AuthUtil.getUser( request ) );
369
370     //
371
// mapping authorized?
372
//
373
if ( !policy.isMappingAuthorized( user, mapping.getPath() ) )
374       return mapping.findForward( "accessDenied" );
375
376     //
377
// cancelling?
378
//
379
if ( !StringUtils.isBlank( ( String JavaDoc )op.form.get( "cancel" ) ) )
380       return new ActionForward( "/editPage.do?id=" + op.form.get( "pageid" ) );
381
382     //
383
// execute the appopriate method
384
//
385
if ( mapping.getPath().equals( "/configureTextModuleForm" ) ) {
386       return doConfigureTextModuleForm( op, policy );
387     }
388     if ( mapping.getPath().equals( "/configureTextModule" ) ) {
389       return doConfigureTextModule( op, policy );
390     }
391     if ( mapping.getPath().equals( "/configureNavModuleForm" ) ) {
392       return doConfigureNavModuleForm( op, policy );
393     }
394     if ( mapping.getPath().equals( "/configureNavModule" ) ) {
395       return doConfigureNavModule( op, policy );
396     }
397     if ( mapping.getPath().equals( "/configureIncludeModuleForm" ) ) {
398       return doConfigureIncludeModuleForm( op, policy );
399     }
400     if ( mapping.getPath().equals( "/configureIncludeModule" ) ) {
401       return doConfigureIncludeModule( op, policy );
402     }
403
404     throw
405       new Exception JavaDoc( "Unexpected mapping path \"" + mapping.getPath() + "\"" );
406   }
407
408   // properties ///////////////////////////////////////////////////////////////
409

410   // attributes ///////////////////////////////////////////////////////////////
411
}
412
Popular Tags