KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > sitecontext > 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.sitecontext;
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.validator.DynaValidatorForm;
28 import org.apache.struts.action.ActionForward;
29 import com.methodhead.auth.AuthAction;
30
31 import javax.servlet.http.HttpServletRequest JavaDoc;
32 import javax.servlet.http.HttpServletResponse JavaDoc;
33
34 import com.methodhead.auth.AuthUtil;
35 import com.methodhead.auth.AuthUser;
36 import com.methodhead.util.OperationContext;
37 import com.methodhead.MhfPolicy;
38 import com.methodhead.util.StrutsUtil;
39 import java.util.Collections JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import org.apache.commons.lang.StringUtils;
43 import com.methodhead.aikp.AikpAction;
44 import com.methodhead.aikp.IntKey;
45 import com.methodhead.aikp.AutoIntKeyPersistable;
46 import com.methodhead.event.Event;
47
48 public class SiteContextAction
49 extends
50   AikpAction {
51
52   // constructors /////////////////////////////////////////////////////////////
53

54   // constants ////////////////////////////////////////////////////////////////
55

56   // classes //////////////////////////////////////////////////////////////////
57

58   // methods //////////////////////////////////////////////////////////////////
59

60   /**
61    * NOT UNIT TESTED Returns a new <tt>SiteContext</tt>.
62    */

63   protected AutoIntKeyPersistable createPersistable(
64     OperationContext op ) {
65     return new SiteContext();
66   }
67
68   /**
69    * Updates site context's domains from <tt>form.domains</tt>.
70    */

71   protected void populatePersistable(
72     AutoIntKeyPersistable persistable,
73     DynaActionForm form ) {
74
75     super.populatePersistable( persistable, form );
76
77     SiteContext siteContext = ( SiteContext )persistable;
78
79     siteContext.getDomains().clear();
80     siteContext.getDomains().addAll( ( List JavaDoc )form.get( "domains" ) );
81   }
82
83   /**
84    * Joins the site context's domain names to a newline-separated string and
85    * sets <tt>form.domainsText</tt> to it.
86    */

87   protected void populateForm(
88     DynaActionForm form,
89     AutoIntKeyPersistable persistable ) {
90
91     super.populateForm( form, persistable );
92
93     SiteContext siteContext = ( SiteContext )persistable;
94
95     form.set(
96       "domainsText",
97       StringUtils.join( siteContext.getDomains().iterator(), "\n" ) );
98   }
99
100   /**
101    * Fully loads each site context loaded by the default behavior and sorts the
102    * resulting list.
103    */

104   protected ActionForward doList(
105     OperationContext op,
106     Object JavaDoc policy ) {
107
108     String JavaDoc msg =
109       ( ( SiteContextPolicy )policy ).isSiteContextListAuthorized( op );
110     if ( msg != null ) {
111       StrutsUtil.addMessage( op.request, msg, null, null, null );
112       return op.mapping.findForward( "accessDenied" );
113     }
114
115     ActionForward forward = super.doList( op, policy );
116
117     List JavaDoc list = ( List JavaDoc )op.form.get( "list" );
118     for ( Iterator JavaDoc iter = list.iterator(); iter.hasNext(); ) {
119       SiteContext siteContext = ( SiteContext )iter.next();
120       siteContext.load( new IntKey( siteContext.get( "id" ) ) );
121     }
122
123     Collections.sort( list );
124
125     return forward;
126   }
127
128   /**
129    * Performs policy check and event logging.
130    */

131   protected ActionForward doDelete(
132     OperationContext op,
133     Object JavaDoc policy ) {
134
135     String JavaDoc msg =
136       ( ( SiteContextPolicy )policy ).isSiteContextDeleteAuthorized( op );
137     if ( msg != null ) {
138       StrutsUtil.addMessage( op.request, msg, null, null, null );
139       return op.mapping.findForward( "accessDenied" );
140     }
141
142     //
143
// get the site context so we can get the name to log
144
//
145
SiteContext siteContext = ( SiteContext )createPersistable( op );
146     siteContext.load( new IntKey( op.form.get( "id" ) ) );
147
148     ActionForward forward = super.doDelete( op, policy );
149
150     Event.log(
151       SiteContext.getContext( op.request ),
152       op.user.getLogin(),
153       "shim",
154       "Deleted site context \"" + siteContext.getDomains().get( 0 ) + "\"." );
155
156     return forward;
157   }
158
159   /**
160    * Performs policy check and event logging.
161    */

162   protected ActionForward doSaveNew(
163     OperationContext op,
164     Object JavaDoc policy ) {
165
166     String JavaDoc msg =
167       ( ( SiteContextPolicy )policy ).isSiteContextSaveNewAuthorized( op );
168     if ( msg != null ) {
169       StrutsUtil.addMessage( op.request, msg, null, null, null );
170       return op.mapping.findForward( "accessDenied" );
171     }
172
173     ActionForward forward = super.doSaveNew( op, policy );
174
175     SiteContext siteContext = ( SiteContext )createPersistable( op );
176     siteContext.load( new IntKey( op.form.get( "id" ) ) );
177
178     Event.log(
179       SiteContext.getContext( op.request ),
180       op.user.getLogin(),
181       "shim",
182       "Created site context \"" + siteContext.getDomains().get( 0 ) + "\"." );
183
184     return forward;
185   }
186
187   /**
188    * Performs policy check and event logging.
189    */

190   protected ActionForward doEdit(
191     OperationContext op,
192     Object JavaDoc policy ) {
193
194     String JavaDoc msg =
195       ( ( SiteContextPolicy )policy ).isSiteContextEditAuthorized( op );
196     if ( msg != null ) {
197       StrutsUtil.addMessage( op.request, msg, null, null, null );
198       return op.mapping.findForward( "accessDenied" );
199     }
200
201     return super.doEdit( op, policy );
202   }
203
204   /**
205    * Performs policy check and event logging.
206    */

207   protected ActionForward doNew(
208     OperationContext op,
209     Object JavaDoc policy ) {
210
211     String JavaDoc msg =
212       ( ( SiteContextPolicy )policy ).isSiteContextNewAuthorized( op );
213     if ( msg != null ) {
214       StrutsUtil.addMessage( op.request, msg, null, null, null );
215       return op.mapping.findForward( "accessDenied" );
216     }
217
218     return super.doNew( op, policy );
219   }
220
221   /**
222    * Performs policy check and event logging.
223    */

224   protected ActionForward doSave(
225     OperationContext op,
226     Object JavaDoc policy ) {
227
228     String JavaDoc msg =
229       ( ( SiteContextPolicy )policy ).isSiteContextSaveAuthorized( op );
230     if ( msg != null ) {
231       StrutsUtil.addMessage( op.request, msg, null, null, null );
232       return op.mapping.findForward( "accessDenied" );
233     }
234
235     ActionForward forward = super.doSave( op, policy );
236
237     SiteContext siteContext = ( SiteContext )createPersistable( op );
238     siteContext.load( new IntKey( op.form.get( "id" ) ) );
239
240     Event.log(
241       SiteContext.getContext( op.request ),
242       op.user.getLogin(),
243       "shim",
244       "Updated site context \"" + siteContext.getDomains().get( 0 ) + "\"." );
245
246     return forward;
247   }
248
249   /**
250    * Returns the <tt>list</tt> forward.
251    */

252   protected ActionForward doCancelSave(
253     OperationContext op,
254     Object JavaDoc policy ) {
255
256     return new ActionForward( "/siteContext.do?action=list" );
257   }
258
259   // properties ///////////////////////////////////////////////////////////////
260

261   // attributes ///////////////////////////////////////////////////////////////
262
}
263
Popular Tags