KickJava   Java API By Example, From Geeks To Geeks.

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


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.ActionForward;
27 import org.apache.struts.action.DynaActionForm;
28 import org.apache.struts.action.ActionMessages;
29 import org.apache.struts.util.MessageResources;
30 import org.apache.struts.Globals;
31 import org.apache.struts.util.LabelValueBean;
32 import com.methodhead.event.Event;
33
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36 import com.methodhead.aikp.AutoIntKeyPersistable;
37 import com.methodhead.aikp.AikpAction;
38 import com.methodhead.aikp.IntKey;
39 import com.methodhead.util.OperationContext;
40 import com.methodhead.util.StrutsUtil;
41 import com.methodhead.sitecontext.SiteContext;
42 import java.util.List JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Iterator JavaDoc;
45 import org.apache.commons.lang.StringUtils;
46
47
48 public class HtmlFragmentAction
49 extends
50   AikpAction {
51
52   // constructors /////////////////////////////////////////////////////////////
53

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

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

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

60   protected AutoIntKeyPersistable createPersistable(
61     OperationContext op ) {
62     HtmlFragment fragment = new HtmlFragment();
63     fragment.setSiteContext( SiteContext.getContext( op.request ) );
64     return fragment;
65   }
66
67   protected void populateForm(
68     DynaActionForm form,
69     AutoIntKeyPersistable persistable ) {
70
71     populateFormProperty( "name", form, persistable );
72     populateFormProperty( "value", form, persistable );
73   }
74
75   protected void populatePersistable(
76     AutoIntKeyPersistable persistable,
77     DynaActionForm form ) {
78
79     populatePersistableField( "name", persistable, form );
80     populatePersistableField( "value", persistable, form );
81   }
82
83   /**
84    * Loads all fragments for the site, creates a list of options for them, and
85    * puts those options in <tt>form.list</tt>.
86    */

87   protected ActionForward doList(
88     OperationContext op,
89     Object JavaDoc policy ) {
90
91     String JavaDoc msg = ( ( ShimPolicy )policy ).isHtmlFragmentListAuthorized( op );
92     if ( msg != null ) {
93       StrutsUtil.addMessage( op.request, msg, null, null, null );
94       return op.mapping.findForward( "accessDenied" );
95     }
96
97     op.form.set( "list", ShimUtils.getHtmlFragmentOptions( op.request ) );
98
99     //
100
// update the action
101
//
102
op.form.set( "action", "edit" );
103
104     return op.mapping.findForward( "list" );
105   }
106
107   /**
108    * Calls base class function and forwards to
109    * <tt>/htmlFragment.do&#63;action=list</tt>.
110    */

111   protected ActionForward doSaveNew(
112     OperationContext op,
113     Object JavaDoc policy ) {
114
115     String JavaDoc msg = ( ( ShimPolicy )policy ).isHtmlFragmentSaveNewAuthorized( op );
116     if ( msg != null ) {
117       StrutsUtil.addMessage( op.request, msg, null, null, null );
118       return op.mapping.findForward( "accessDenied" );
119     }
120
121     super.doSaveNew( op, policy );
122
123     Event.log(
124       SiteContext.getContext( op.request ),
125       op.user.getLogin(),
126       "shim",
127       "Created html fragment \"" + op.form.get( "name" ) + "\"." );
128
129     return new ActionForward( "/htmlFragment.do?action=list" );
130   }
131
132   protected ActionForward doNew(
133     OperationContext op,
134     Object JavaDoc policy ) {
135
136     String JavaDoc msg = ( ( ShimPolicy )policy ).isHtmlFragmentNewAuthorized( op );
137     if ( msg != null ) {
138       StrutsUtil.addMessage( op.request, msg, null, null, null );
139       return op.mapping.findForward( "accessDenied" );
140     }
141
142     return super.doNew( op, policy );
143   }
144
145   protected ActionForward doEdit(
146     OperationContext op,
147     Object JavaDoc policy ) {
148
149     String JavaDoc msg = ( ( ShimPolicy )policy ).isHtmlFragmentEditAuthorized( op );
150     if ( msg != null ) {
151       StrutsUtil.addMessage( op.request, msg, null, null, null );
152       return op.mapping.findForward( "accessDenied" );
153     }
154
155     return super.doEdit( op, policy );
156   }
157
158   /**
159    * Calls base class function and forwards to
160    * <tt>/htmlFragment.do&#63;action=list</tt>.
161    */

162   protected ActionForward doSave(
163     OperationContext op,
164     Object JavaDoc policy ) {
165
166     String JavaDoc msg = ( ( ShimPolicy )policy ).isHtmlFragmentSaveAuthorized( op );
167     if ( msg != null ) {
168       StrutsUtil.addMessage( op.request, msg, null, null, null );
169       return op.mapping.findForward( "accessDenied" );
170     }
171
172     super.doSave( op, policy );
173
174     Event.log(
175       SiteContext.getContext( op.request ),
176       op.user.getLogin(),
177       "shim",
178       "Saved html fragment \"" + op.form.get( "name" ) + "\"." );
179
180     return new ActionForward( "/htmlFragment.do?action=list" );
181   }
182
183   /**
184    * Returns a forward to <tt>list</tt>.
185    */

186   protected ActionForward doCancelSave(
187     OperationContext op,
188     Object JavaDoc policy ) {
189
190     return new ActionForward( "/htmlFragment.do?action=list" );
191   }
192
193   /**
194    * Forwards to <tt>/htmlFragment.do&#63;action=list</tt>.
195    */

196   protected ActionForward doDelete(
197     OperationContext op,
198     Object JavaDoc policy ) {
199
200     String JavaDoc msg = ( ( ShimPolicy )policy ).isHtmlFragmentDeleteAuthorized( op );
201     if ( msg != null ) {
202       StrutsUtil.addMessage( op.request, msg, null, null, null );
203       return op.mapping.findForward( "accessDenied" );
204     }
205
206     super.doDelete( op, policy );
207
208     Event.log(
209       SiteContext.getContext( op.request ),
210       op.user.getLogin(),
211       "shim",
212       "Deleted html fragment \"" + op.form.get( "name" ) + "\"." );
213
214     return new ActionForward( "/htmlFragment.do?action=list" );
215   }
216
217   /**
218    *
219    */

220   protected ActionForward doConfirm(
221     OperationContext op,
222     Object JavaDoc policy ) {
223
224     //
225
// load the fragment
226
//
227
HtmlFragment fragment = ( HtmlFragment )createPersistable( op );
228     fragment.load( new IntKey( op.form.get( "id" ) ) );
229
230     //
231
// see if any text modules depend on it
232
//
233
ShimPolicy shimPolicy = ( ShimPolicy )policy;
234     TextModule textModule = shimPolicy.newTextModule();
235     List JavaDoc pages = textModule.getDependentPages( fragment );
236
237     if ( pages.size() > 0 ) {
238
239       //
240
// concatenate page names
241
//
242
StrutsUtil.addMessage(
243         op.request,
244         "shim.htmlFragment.fragmentInUse",
245         StringUtils.join( pages.iterator(), ", " ),
246         null,
247         null );
248
249       return op.mapping.getInputForward();
250     }
251
252     return super.doConfirm( op, policy );
253   }
254
255   // properties ///////////////////////////////////////////////////////////////
256

257   // attributes ///////////////////////////////////////////////////////////////
258
}
259
Popular Tags