KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.methodhead.persistable.ConnectionSingleton;
32 import com.methodhead.aikp.IntKey;
33 import org.apache.commons.beanutils.DynaClass;
34 import org.apache.commons.beanutils.DynaProperty;
35 import org.apache.commons.beanutils.BasicDynaClass;
36 import org.apache.commons.lang.exception.ExceptionUtils;
37 import java.io.IOException JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.sql.ResultSet JavaDoc;
41 import java.sql.SQLException JavaDoc;
42 import java.util.regex.Pattern JavaDoc;
43 import java.util.regex.Matcher JavaDoc;
44 import org.apache.commons.lang.exception.ExceptionUtils;
45 import org.apache.log4j.Logger;
46
47 /**
48  * A Shim module to manage HTML text.
49  * <ul>
50  * <li><tt>int page_id = 0</tt></li>
51  * <li><tt>String panel = ""</tt></li>
52  * <li><tt>String value = ""</tt></li>
53  * <li><tt>int htmlfragment_id = NULL</tt></li>
54  * </ul>
55  */

56 public class TextModule
57 extends
58   Persistable
59 implements
60   Module {
61
62   private static DynaClass dynaClass_ = null;
63
64   static {
65     DynaProperty[] dynaProperties =
66       new DynaProperty[] {
67         new DynaProperty( "page_id", Integer JavaDoc.class ),
68         new DynaProperty( "panel", String JavaDoc.class ),
69         new DynaProperty( "value", String JavaDoc.class ),
70         new DynaProperty( "htmlfragment_id", Integer JavaDoc.class )
71       };
72
73     dynaClass_ =
74       new BasicDynaClass(
75         "shim_text", TextModule.class, dynaProperties );
76   }
77
78   // constructors /////////////////////////////////////////////////////////////
79

80   public TextModule() {
81     super( dynaClass_ );
82     init();
83   }
84
85   public TextModule(
86     DynaClass dynaClass ) {
87     super( dynaClass );
88     init();
89   }
90
91   // constants ////////////////////////////////////////////////////////////////
92

93   // classes //////////////////////////////////////////////////////////////////
94

95   // methods //////////////////////////////////////////////////////////////////
96

97   /**
98    * Loads the text module for <tt>page</tt> and <tt>panel</tt> set by {@link
99    * #init init()}.
100    */

101   public void load() {
102     super.load(
103       "page_id=" + getInt( "page_id" ) + " AND panel=" +
104       getSqlLiteral( getString( "panel" ) ) );
105   }
106
107   /**
108    * Saves the text module for <tt>page</tt> and <tt>panel</tt> set by {@link
109    * #init init()}. The text module should already have been created with
110    * {@link #create create()}.
111    */

112   public void save() {
113     //
114
// force fragment id to null if it's zero; this let's us use the foreign
115
// key in the database
116
//
117
if ( getInt( "htmlfragment_id" ) == 0 )
118       set( "htmlfragment_id", null );
119
120     super.save(
121       "page_id=" + getInt( "page_id" ) + " AND panel=" +
122       getSqlLiteral( getString( "panel" ) ) );
123   }
124
125   /**
126    * Saves the text module for <tt>page</tt> and <tt>panel</tt> set by {@link
127    * #init init()}. The text module should already have been created with
128    * {@link #create create()}.
129    */

130   public void saveNew() {
131     //
132
// force fragment id to null if it's zero; this let's us use the foreign
133
// key in the database
134
//
135
if ( getInt( "htmlfragment_id" ) == 0 )
136       set( "htmlfragment_id", null );
137
138     super.saveNew();
139   }
140
141   private void init() {
142     setInt( "page_id", 0 );
143     setString( "panel", "" );
144     setString( "value", "" );
145     set( "htmlfragment_id", null );
146   }
147
148   /**
149    * Returns a short descriptive name for the module.
150    */

151   public String JavaDoc getName() {
152     return "Text";
153   }
154
155   /**
156    * Returns <tt>true</tt> if the module has a configuration interface; that
157    * is, if {@link #configure} actually does something.
158    */

159   public boolean isConfigurable() {
160     return true;
161   }
162
163   /**
164    * Sets the module's <tt>page</tt> and <tt>panel</tt>. This method is called
165    * before any other operations are performed.
166    */

167   public void init(
168     Page page,
169     String JavaDoc panel ) {
170
171     if ( ( page == null ) || ( panel == null ) )
172       throw new ShimException( "Page and/or panel is null." );
173
174     if ( page.getPanels().get( panel ) == null )
175       throw new ShimException( "Page has no panel \"" + panel + "\"" );
176
177     setInt( "page_id", page.getInt( "id" ) );
178     setString( "panel", panel );
179
180     if ( !"".equals( page.getSiteContext().getString( "path" ) ) )
181       siteContextPath_ = page.getSiteContext().getString( "path" );
182   }
183
184   /**
185    * Creates the module. This method is called when a module is instantiated
186    * for a page and panel. The module should be initialized such that future
187    * calls to {@link #configure} and {@link #display} are successful.
188    */

189   public void create() {
190     //
191
// try to load it first; leave text alone if it already exists
192
//
193
try {
194       load();
195     }
196     catch ( PersistableException e ) {
197
198       //
199
// otherwise, create the text
200
//
201
setString( "value", "Insert your text here..." );
202       saveNew();
203     }
204   }
205
206   /**
207    * Manages the configuration interface for the module.
208    */

209   public ActionForward configure(
210     ActionMapping mapping,
211     DynaActionForm form,
212     HttpServletRequest JavaDoc request,
213     HttpServletResponse JavaDoc response ) {
214
215     return new ActionForward(
216       "/configureTextModuleForm.do?pageid=" + form.get( "pageid" ) +
217       "&panel=" + form.get( "panel" ) );
218   }
219
220   public boolean isEditable() {
221     return true;
222   }
223
224   public void update(
225     String JavaDoc text ) {
226
227     load(
228       "page_id=" + getInt( "page_id" ) + " AND panel=" +
229       getSqlLiteral( getString( "panel" ) ) );
230
231     //
232
// referencing a fragment?
233
//
234
if ( getInt( "htmlfragment_id" ) != 0 ) {
235       HtmlFragment fragment = new HtmlFragment();
236       fragment.load( new IntKey( getInt( "htmlfragment_id" ) ) );
237       fragment.setString( "value", text );
238       fragment.save();
239     }
240     else {
241       setString( "value", text );
242       save();
243     }
244   }
245
246   /**
247    * Displays the module.
248    */

249   public void display(
250     HttpServletRequest JavaDoc request,
251     HttpServletResponse JavaDoc response,
252     JspWriter JavaDoc out )
253   throws
254     IOException JavaDoc {
255
256     load(
257       "page_id=" + getInt( "page_id" ) + " AND panel=" +
258       getSqlLiteral( getString( "panel" ) ) );
259
260     //
261
// referencing a fragment?
262
//
263
if ( getInt( "htmlfragment_id" ) != 0 ) {
264       HtmlFragment fragment = new HtmlFragment();
265       fragment.load( new IntKey( getInt( "htmlfragment_id" ) ) );
266       out.println( fragment.getString( "value" ) );
267     }
268     else {
269       out.println( getString( "value" ) );
270     }
271   }
272
273   /**
274    * Destroys the module, freeing any resources associated with the module.
275    */

276   public void destroy() {
277     deleteAll(
278       dynaClass_, "page_id=" + getInt( "page_id" ) + " AND panel=" +
279       getSqlLiteral( getString( "panel" ) ) );
280   }
281
282   public void copyTo(
283     Page page ) {
284
285     //
286
// load this module
287
//
288
load();
289
290     //
291
// load the module to copy to
292
//
293
TextModule textModule = new TextModule();
294     textModule.init( page, getString( "panel" ) );
295
296     //
297
// already exists?
298
//
299
try {
300       textModule.load();
301
302       //
303
// update it
304
//
305
textModule.setString( "value", getString( "value" ) );
306       textModule.setInt( "htmlfragment_id", getInt( "htmlfragment_id" ) );
307       textModule.save();
308     }
309     catch ( PersistableException e ) {
310
311       //
312
// save new
313
//
314
textModule.setString( "value", getString( "value" ) );
315       textModule.setInt( "htmlfragment_id", getInt( "htmlfragment_id" ) );
316       textModule.saveNew();
317     }
318   }
319
320   /**
321    * Returns a list of pages which use text modules that refer to
322    * <tt>fragment</tt>. <tt>fragment</tt> is expected to be loaded when this
323    * method is called.
324    */

325   public List JavaDoc getDependentPages(
326     HtmlFragment fragment ) {
327
328     List JavaDoc pages = new ArrayList JavaDoc();
329
330     String JavaDoc sql =
331       "SELECT " +
332       " shim_page.id AS id " +
333       "FROM " +
334       " shim_page " +
335       "LEFT JOIN " +
336       " shim_text ON " +
337       " shim_text.page_id=shim_page.id " +
338       "WHERE " +
339       " shim_text.htmlfragment_id=" + fragment.getInt( "id" );
340
341     ResultSet JavaDoc rs = null;
342     try {
343       rs = ConnectionSingleton.runQuery( sql );
344
345       if ( rs == null ) {
346         throw new SQLException JavaDoc( "Null result set." );
347       }
348
349       while ( rs.next() ) {
350         Page page = new Page();
351         page.setSiteContext( fragment.getSiteContext() );
352         page.load( new IntKey( rs.getInt( "id" ) ) );
353         pages.add( page );
354       }
355     }
356     catch ( SQLException JavaDoc e ) {
357       String JavaDoc msg = "Doing something. " + ExceptionUtils.getStackTrace( e );
358       logger_.error( msg );
359       throw new RuntimeException JavaDoc( msg );
360     }
361     finally {
362       ConnectionSingleton.close( rs );
363     }
364
365     return pages;
366   }
367
368   // properties ///////////////////////////////////////////////////////////////
369

370   // attributes ///////////////////////////////////////////////////////////////
371

372   private String JavaDoc siteContextPath_ = null;
373
374   private static Logger logger_ = Logger.getLogger( TextModule.class );
375 }
376
Popular Tags