KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
25 import javax.servlet.jsp.JspWriter JavaDoc;
26 import org.apache.struts.action.ActionMapping;
27 import org.apache.struts.action.ActionForward;
28 import org.apache.struts.action.DynaActionForm;
29 import com.methodhead.persistable.Persistable;
30 import com.methodhead.persistable.PersistableException;
31 import org.apache.commons.beanutils.DynaClass;
32 import org.apache.commons.beanutils.DynaProperty;
33 import org.apache.commons.beanutils.BasicDynaClass;
34 import java.io.IOException JavaDoc;
35 import com.methodhead.sitecontext.SiteContext;
36 import javax.servlet.RequestDispatcher JavaDoc;
37 import javax.servlet.ServletException JavaDoc;
38 import org.apache.commons.lang.exception.ExceptionUtils;
39
40 /**
41  * A Shim module to manage HTML text.
42  * <ul>
43  * <li><tt>int page_id = 0</tt></li>
44  * <li><tt>String panel = ""</tt></li>
45  * <li><tt>String jsp = ""</tt></li>
46  * </ul>
47  */

48 public class IncludeModule
49 extends
50   Persistable
51 implements
52   Module {
53
54   private static DynaClass dynaClass_ = null;
55
56   static {
57     DynaProperty[] dynaProperties =
58       new DynaProperty[] {
59         new DynaProperty( "page_id", Integer JavaDoc.class ),
60         new DynaProperty( "panel", String JavaDoc.class ),
61         new DynaProperty( "jsp", String JavaDoc.class )
62       };
63
64     dynaClass_ =
65       new BasicDynaClass(
66         "shim_include", IncludeModule.class, dynaProperties );
67   }
68
69   // constructors /////////////////////////////////////////////////////////////
70

71   public IncludeModule() {
72     super( dynaClass_ );
73     init();
74   }
75
76   public IncludeModule(
77     DynaClass dynaClass ) {
78     super( dynaClass );
79     init();
80   }
81
82   // constants ////////////////////////////////////////////////////////////////
83

84   // classes //////////////////////////////////////////////////////////////////
85

86   // methods //////////////////////////////////////////////////////////////////
87

88   /**
89    * Initializes the module's fields.
90    */

91   private void init() {
92     setInt( "page_id", 0 );
93     setString( "panel", "" );
94     setString( "jsp", "" );
95   }
96
97   /**
98    * Sets the module's <tt>page</tt> and <tt>panel</tt>. This method is called
99    * before any other operations are performed.
100    */

101   public void init(
102     Page page,
103     String JavaDoc panel ) {
104
105     if ( ( page == null ) || ( panel == null ) )
106       throw new ShimException( "Page and/or panel is null." );
107
108     if ( page.getPanels().get( panel ) == null )
109       throw new ShimException( "Page has no panel \"" + panel + "\"" );
110
111     setInt( "page_id", page.getInt( "id" ) );
112     setString( "panel", panel );
113   }
114
115   /**
116    * Loads the module for <tt>page</tt> and <tt>panel</tt> set by {@link
117    * #init init()}.
118    */

119   public void load() {
120     super.load(
121       "page_id=" + getInt( "page_id" ) + " AND panel=" +
122       getSqlLiteral( getString( "panel" ) ) );
123   }
124
125   /**
126    * Saves the module for <tt>page</tt> and <tt>panel</tt> set by {@link
127    * #init init()}. The module should already have been created with
128    * {@link #create create()}.
129    */

130   public void save() {
131     super.save(
132       "page_id=" + getInt( "page_id" ) + " AND panel=" +
133       getSqlLiteral( getString( "panel" ) ) );
134   }
135
136   /**
137    * Returns a short descriptive name for the module.
138    */

139   public String JavaDoc getName() {
140     return "Include";
141   }
142
143   /**
144    * Returns <tt>true</tt> if the module has a configuration interface; that
145    * is, if {@link #configure} actually does something.
146    */

147   public boolean isConfigurable() {
148     return true;
149   }
150
151   /**
152    * Creates the module. This method is called when a module is instantiated
153    * for a page and panel. The module should be initialized such that future
154    * calls to {@link #configure} and {@link #display} are successful.
155    */

156   public void create() {
157     //
158
// try to load first and create it only if it doesn't already exist
159
//
160
try {
161       load();
162     }
163     catch ( PersistableException e ) {
164       setString( "jsp", "" );
165       saveNew();
166     }
167   }
168
169   /**
170    * Manages the configuration interface for the module.
171    */

172   public ActionForward configure(
173     ActionMapping mapping,
174     DynaActionForm form,
175     HttpServletRequest JavaDoc request,
176     HttpServletResponse JavaDoc response ) {
177
178     return new ActionForward(
179       "/configureIncludeModuleForm.do?pageid=" + form.get( "pageid" ) +
180       "&panel=" + form.get( "panel" ) );
181   }
182
183   public boolean isEditable() {
184     return false;
185   }
186
187   public void update(
188     String JavaDoc text ) {
189
190     throw new ShimException( "Method not implemented." );
191   }
192
193   /**
194    * Displays the module.
195    */

196   public void display(
197     HttpServletRequest JavaDoc request,
198     HttpServletResponse JavaDoc response,
199     JspWriter JavaDoc out )
200   throws
201     IOException JavaDoc {
202
203     try {
204       load();
205       
206       SiteContext siteContext = SiteContext.getContext( request );
207
208       out.flush();
209
210       RequestDispatcher JavaDoc dispatcher =
211         request.getSession().getServletContext().getRequestDispatcher(
212           "/WEB-INF/resources/" + siteContext.getInt( "id" ) + "/" +
213           getString( "jsp" ) );
214
215       dispatcher.include( request, response );
216     }
217     catch ( ServletException JavaDoc e ) {
218       out.println( ExceptionUtils.getStackTrace( e ) );
219     }
220   }
221
222   /**
223    * Destroys the module, freeing any resources associated with the module.
224    */

225   public void destroy() {
226     deleteAll(
227       dynaClass_, "page_id=" + getInt( "page_id" ) + " AND panel=" +
228       getSqlLiteral( getString( "panel" ) ) );
229   }
230
231   public void copyTo(
232     Page page ) {
233
234     //
235
// load this module
236
//
237
load();
238
239     //
240
// load the module to copy to
241
//
242
IncludeModule includeModule = new IncludeModule();
243     includeModule.init( page, getString( "panel" ) );
244
245     //
246
// already exists?
247
//
248
try {
249       includeModule.load();
250
251       //
252
// update it
253
//
254
includeModule.setString( "jsp", getString( "jsp" ) );
255       includeModule.save();
256     }
257     catch ( PersistableException e ) {
258
259       //
260
// save new
261
//
262
includeModule.setString( "jsp", getString( "jsp" ) );
263       includeModule.saveNew();
264     }
265   }
266
267   // properties ///////////////////////////////////////////////////////////////
268

269   // attributes ///////////////////////////////////////////////////////////////
270
}
271
Popular Tags