KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > transfer > SiteContextAction


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.transfer;
22
23 import org.apache.struts.action.ActionForward;
24 import org.apache.struts.action.DynaActionForm;
25 import org.apache.struts.action.ActionForm;
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.commons.lang.StringUtils;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33 import com.methodhead.sitecontext.SiteContextPolicy;
34 import com.methodhead.sitecontext.SiteContext;
35 import com.methodhead.persistable.Persistable;
36 import com.methodhead.util.StrutsUtil;
37 import com.methodhead.util.OperationContext;
38 import com.methodhead.reg.User;
39 import com.methodhead.reg.RegPolicy;
40 import com.methodhead.aikp.IntKey;
41 import com.methodhead.aikp.AutoIntKeyPersistable;
42 import com.methodhead.auth.AuthUser;
43 import com.methodhead.auth.AuthUtil;
44 import com.methodhead.event.Event;
45
46 /**
47  * Subclasses SiteContextAction to disassociate a user from a site context when
48  * it is deleted.
49  */

50 public class SiteContextAction
51 extends
52   com.methodhead.shim.SiteContextAction {
53
54   // constructors /////////////////////////////////////////////////////////////
55

56   // constants ////////////////////////////////////////////////////////////////
57

58   // classes //////////////////////////////////////////////////////////////////
59

60   // methods //////////////////////////////////////////////////////////////////
61

62   /**
63    * Extends the default behavior to disassociate any users from the site being
64    * deleted.
65    */

66   public ActionForward doDelete(
67     OperationContext op,
68     Object JavaDoc policy ) {
69
70     //
71
// authorized?
72
//
73
String JavaDoc msg =
74       ( ( SiteContextPolicy )policy ).isSiteContextDeleteAuthorized( op );
75     if ( msg != null ) {
76       StrutsUtil.addMessage( op.request, msg, null, null, null );
77       return op.mapping.findForward( "accessDenied" );
78     }
79
80     //
81
// disassociate any users from this context
82
//
83
SiteContext siteContext = ( SiteContext )createPersistable( op );
84     siteContext.load( new IntKey( op.form.get( "id" ) ) );
85
86     User user = new User();
87     List JavaDoc users = user.loadAllForSiteContext( siteContext );
88
89     for ( Iterator JavaDoc iter = users.iterator(); iter.hasNext(); ) {
90       User u = ( User )iter.next();
91
92 throw new RuntimeException JavaDoc( "FIX THIS" );
93 /*
94       for ( Iterator iter2 = u.getSiteContexts().iterator(); iter2.hasNext(); ) {
95         SiteContext sc = ( SiteContext )iter2.next();
96
97         if ( sc.equals( siteContext ) ) {
98           iter2.remove();
99         }
100       }
101
102       u.save();
103 */

104     }
105
106     //
107
// invoke default behavior
108
//
109
ActionForward forward = super.doDelete( op, policy );
110
111     return forward;
112   }
113
114   /**
115    * Initializes the extension <tt>form.classname</tt> for site context with
116    * the id <tt>form.id</tt> and returns a forward to edit the site context.
117    */

118   public ActionForward doInitExtension(
119     OperationContext op,
120     TransferPolicy policy ) {
121
122     //
123
// authorized?
124
//
125
String JavaDoc msg = policy.isInitExtensionAuthorized( op );
126     if ( msg != null ) {
127       StrutsUtil.addMessage( op.request, msg, null, null, null );
128       return op.mapping.findForward( "accessDenied" );
129     }
130
131     //
132
// load the site context
133
//
134
SiteContext siteContext = new SiteContext();
135     siteContext.load( new IntKey( op.form.get( "id" ) ) );
136
137     //
138
// instantiate the extension
139
//
140
SiteExtension siteExtension = policy.newSiteExtension();
141     Extension extension =
142       siteExtension.instantiateExtension(
143         ( String JavaDoc )op.form.get( "classname" ) );
144
145     //
146
// call it's init methodhead
147
//
148
extension.init( siteContext );
149
150     //
151
// save the site extension
152
//
153
siteExtension.setString(
154       "class_name", ( String JavaDoc )op.form.get( "classname" ) );
155     siteExtension.setInt( "sitecontext_id", siteContext.getInt( "id" ) );
156     siteExtension.setBoolean( "enabled", true );
157     siteExtension.saveNew();
158
159     //
160
// log the event
161
//
162
Event.log(
163       SiteContext.getDefaultContext(),
164       op.user.getLogin(),
165       "sitecontext",
166       "Initialized extension \"" + extension.getName() +
167         "\" for site \"" + siteContext + "\"." );
168
169     //
170
// return a forward to edit
171
//
172
return new ActionForward(
173       "/siteContext.do?action=edit&id=" + siteContext.getInt( "id" ) );
174   }
175
176   /**
177    * Destroys the extension <tt>form.classname</tt> for site context with
178    * the id <tt>form.id</tt> and returns a forward to edit the site context.
179    */

180   public ActionForward doDestroyExtension(
181     OperationContext op,
182     TransferPolicy policy ) {
183
184     //
185
// authorized?
186
//
187
String JavaDoc msg = policy.isDestroyExtensionAuthorized( op );
188     if ( msg != null ) {
189       StrutsUtil.addMessage( op.request, msg, null, null, null );
190       return op.mapping.findForward( "accessDenied" );
191     }
192
193     //
194
// cancelled?
195
//
196
if ( StringUtils.isNotBlank( ( String JavaDoc )op.form.get( "cancel" ) ) ) {
197       return new ActionForward(
198         "/siteContext.do?action=edit&id=" + op.form.get( "id" ) );
199     }
200
201     //
202
// instantiate the extension
203
//
204
SiteExtension siteExtension = policy.newSiteExtension();
205     Extension extension =
206       siteExtension.instantiateExtension(
207         ( String JavaDoc )op.form.get( "classname" ) );
208
209     //
210
// confirmed?
211
//
212
if ( StringUtils.isBlank( ( String JavaDoc )op.form.get( "confirm" ) ) ) {
213       StrutsUtil.addMessage(
214         op.request,
215         "transfer.confirmDestroyExtension",
216         extension.getName(),
217         null,
218         null );
219
220       return op.mapping.findForward( "confirm" );
221     }
222
223     //
224
// load the site context
225
//
226
SiteContext siteContext = new SiteContext();
227     siteContext.load( new IntKey( op.form.get( "id" ) ) );
228
229     //
230
// call it's destroy methodhead
231
//
232
extension.destroy( siteContext );
233
234     //
235
// delete the extension
236
//
237
siteExtension.deleteAll(
238       "sitecontext_id=" + siteContext.getInt( "id" ) + " AND class_name=" +
239       Persistable.getSqlLiteral( ( String JavaDoc )op.form.get( "classname" ) ) );
240
241     //
242
// log the event
243
//
244
Event.log(
245       SiteContext.getDefaultContext(),
246       op.user.getLogin(),
247       "sitecontext",
248       "Destroyed extension \"" + extension.getName() +
249         "\" for site \"" + siteContext + "\"." );
250
251     //
252
// return a forward to edit
253
//
254
return new ActionForward(
255       "/siteContext.do?action=edit&id=" + siteContext.getInt( "id" ) );
256   }
257
258   /**
259    * Disables the extension <tt>form.classname</tt> for site context with
260    * the id <tt>form.id</tt> and returns a forward to edit the site context.
261    */

262   public ActionForward doDisableExtension(
263     OperationContext op,
264     TransferPolicy policy ) {
265
266     //
267
// authorized?
268
//
269
String JavaDoc msg = policy.isDisableExtensionAuthorized( op );
270     if ( msg != null ) {
271       StrutsUtil.addMessage( op.request, msg, null, null, null );
272       return op.mapping.findForward( "accessDenied" );
273     }
274
275     //
276
// load the site context
277
//
278
SiteContext siteContext = new SiteContext();
279     siteContext.load( new IntKey( op.form.get( "id" ) ) );
280
281     //
282
// disable the site extension
283
//
284
SiteExtension siteExtension = policy.newSiteExtension();
285     siteExtension.load(
286       siteContext, ( String JavaDoc )op.form.get( "classname" ) );
287     siteExtension.setBoolean( "enabled", false );
288     siteExtension.save();
289
290     //
291
// log the event
292
//
293
Extension extension =
294       siteExtension.instantiateExtension(
295         siteExtension.getString( "class_name" ) );
296
297     Event.log(
298       SiteContext.getDefaultContext(),
299       op.user.getLogin(),
300       "sitecontext",
301       "Disabled extension \"" + extension.getName() +
302         "\" for site \"" + siteContext + "\"." );
303
304     //
305
// return a forward to edit
306
//
307
return new ActionForward(
308       "/siteContext.do?action=edit&id=" + siteContext.getInt( "id" ) );
309   }
310
311   /**
312    * Enables the extension <tt>form.classname</tt> for site context with
313    * the id <tt>form.id</tt> and returns a forward to edit the site context.
314    */

315   public ActionForward doEnableExtension(
316     OperationContext op,
317     TransferPolicy policy ) {
318
319     //
320
// authorized?
321
//
322
String JavaDoc msg = policy.isEnableExtensionAuthorized( op );
323     if ( msg != null ) {
324       StrutsUtil.addMessage( op.request, msg, null, null, null );
325       return op.mapping.findForward( "accessDenied" );
326     }
327
328     //
329
// load the site context
330
//
331
SiteContext siteContext = new SiteContext();
332     siteContext.load( new IntKey( op.form.get( "id" ) ) );
333
334     //
335
// enable the site extension
336
//
337
SiteExtension siteExtension = policy.newSiteExtension();
338     siteExtension.load(
339       siteContext, ( String JavaDoc )op.form.get( "classname" ) );
340     siteExtension.setBoolean( "enabled", true );
341     siteExtension.save();
342
343     //
344
// log the event
345
//
346
Extension extension =
347       siteExtension.instantiateExtension(
348         siteExtension.getString( "class_name" ) );
349
350     Event.log(
351       SiteContext.getDefaultContext(),
352       op.user.getLogin(),
353       "sitecontext",
354       "Enabled extension \"" + extension.getName() +
355         "\" for site \"" + siteContext + "\"." );
356
357     //
358
// return a forward to edit
359
//
360
return new ActionForward(
361       "/siteContext.do?action=edit&id=" + siteContext.getInt( "id" ) );
362   }
363
364   /**
365    * Extends default behaviour to handle extension-related actions.
366    */

367   public ActionForward doExecute(
368     ActionMapping mapping,
369     ActionForm form,
370     HttpServletRequest JavaDoc request,
371     HttpServletResponse JavaDoc response ) {
372
373     //
374
// get some things we'll need
375
//
376
DynaActionForm dynaForm = ( DynaActionForm )form;
377     TransferPolicy policy =
378       ( TransferPolicy )StrutsUtil.getPolicy( mapping );
379     AuthUser user = AuthUtil.getUser( request );
380
381     OperationContext op =
382       new OperationContext( mapping, dynaForm, request, response, user );
383
384     //
385
// execute the appopriate method
386
//
387
if ( mapping.getPath().equals( "/initExtension" ) ) {
388       return doInitExtension( op, policy );
389     }
390
391     if ( mapping.getPath().equals( "/destroyExtension" ) ) {
392       return doDestroyExtension( op, policy );
393     }
394
395     if ( mapping.getPath().equals( "/disableExtension" ) ) {
396       return doDisableExtension( op, policy );
397     }
398
399     if ( mapping.getPath().equals( "/enableExtension" ) ) {
400       return doEnableExtension( op, policy );
401     }
402
403     return super.doExecute( mapping, form, request, response );
404   }
405
406   // properties ///////////////////////////////////////////////////////////////
407

408   // attributes ///////////////////////////////////////////////////////////////
409
}
410
Popular Tags