KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.methodhead.aikp.AutoIntKeyPersistable;
24
25 import org.apache.commons.beanutils.DynaClass;
26 import org.apache.commons.beanutils.DynaProperty;
27 import org.apache.commons.beanutils.BasicDynaClass;
28 import java.util.Map JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.Collections JavaDoc;
33 import com.methodhead.persistable.Persistable;
34 import com.methodhead.persistable.PersistableException;
35 import com.methodhead.persistable.Key;
36 import com.methodhead.sitecontext.SiteContextCapable;
37 import com.methodhead.sitecontext.SiteContext;
38 import org.apache.commons.lang.StringUtils;
39
40
41 /**
42  * A Page. The following fields are defined:
43  * <ul>
44  * <li><tt>int id = 0</tt></li>
45  * <li><tt>int sitecontext_id = 0</tt></li>
46  * <li><tt>String title = ""</tt></li>
47  * <li><tt>String alttitle = ""</tt></li>
48  * <li><tt>String aliasname = ""</tt></li>
49  * <li><tt>String template = ""</tt></li>
50  * <li><tt>boolean hidden = false</tt></li>
51  * <li><tt>String metadescription = ""</tt></li>
52  * <li><tt>String metakeywords = ""</tt></li>
53  * </ul>
54  */

55 public class Page
56 extends
57   AutoIntKeyPersistable
58 implements
59   SiteContextCapable,
60   Comparable JavaDoc {
61
62   private static DynaClass dynaClass_ = null;
63   private static DynaClass panelDynaClass_ = null;
64
65   static {
66     DynaProperty[] dynaProperties =
67       new DynaProperty[] {
68         new DynaProperty( "id", Integer JavaDoc.class ),
69         new DynaProperty( "sitecontext_id", Integer JavaDoc.class ),
70         new DynaProperty( "title", String JavaDoc.class ),
71         new DynaProperty( "alttitle", String JavaDoc.class ),
72         new DynaProperty( "aliasname", String JavaDoc.class ),
73         new DynaProperty( "template", String JavaDoc.class ),
74         new DynaProperty( "hidden", Boolean JavaDoc.class ),
75         new DynaProperty( "metadescription", String JavaDoc.class ),
76         new DynaProperty( "metakeywords", String JavaDoc.class )
77       };
78
79     dynaClass_ =
80       new BasicDynaClass(
81         "shim_page", Page.class, dynaProperties );
82
83     dynaProperties =
84       new DynaProperty[] {
85         new DynaProperty( "page_id", Integer JavaDoc.class ),
86         new DynaProperty( "name", String JavaDoc.class ),
87         new DynaProperty( "module", String JavaDoc.class )
88       };
89
90     panelDynaClass_ =
91       new BasicDynaClass(
92         "shim_panel", Persistable.class, dynaProperties );
93   }
94
95   // constructors /////////////////////////////////////////////////////////////
96

97   public Page() {
98     super( dynaClass_ );
99     init();
100   }
101
102   public Page( DynaClass dynaClass ) {
103     super( dynaClass );
104     init();
105   }
106
107   // constants ////////////////////////////////////////////////////////////////
108

109   // classes //////////////////////////////////////////////////////////////////
110

111   // methods //////////////////////////////////////////////////////////////////
112

113   protected void init() {
114     setInt( "id", 0 );
115     setInt( "sitecontext_id", 0 );
116     setString( "title", "" );
117     setString( "alttitle", "" );
118     setString( "aliasname", "" );
119     setString( "template", "" );
120     setBoolean( "hidden", false );
121     setString( "metadescription", "" );
122     setString( "metakeywords", "" );
123   }
124
125   public String JavaDoc toString() {
126     return getString( "title" );
127   }
128
129   public int compareTo(
130     Object JavaDoc o ) {
131
132     if ( o == null )
133       return 1;
134
135     if ( !Page.class.isInstance( o ) )
136       return 1;
137
138     Page p = ( Page )o;
139
140     return
141       getString( "title" ).toLowerCase().compareTo(
142         p.getString( "title" ).toLowerCase() );
143   }
144
145   /**
146    * Returns the current site context for this object, throwing an exception if
147    * it hasn't been set.
148    */

149   public SiteContext getSiteContext() {
150     if ( siteContext_ == null )
151       throw new ShimException( "Site context has not been set." );
152
153     return siteContext_;
154   }
155
156   /**
157    * Implements <tt>SiteContextCapable.setSiteContext()</tt>.
158    */

159   public void setSiteContext(
160     SiteContext siteContext ) {
161
162     siteContext_ = siteContext;
163   }
164
165   /**
166    * Returns an alias name for <tt>title</tt> by converting it to lower case
167    * and removing any whitespace or special characters; if a page exists with
168    * the alias, a number is appended and incremented until a unique alias is
169    * calculated.
170    */

171   protected void setDefaultAliasName() {
172
173     String JavaDoc title = getString( "title" );
174
175     //
176
// blank title?
177
//
178
if ( StringUtils.isBlank( title ) )
179       throw new ShimException(
180         "Can't set default alias name for empty title." );
181
182     //
183
// convert to lower case
184
//
185
String JavaDoc alias = title.toLowerCase();
186
187     //
188
// strip out non-alphanumeric characters
189
//
190
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
191
192     for ( int i = 0; i < alias.length(); i++ ) {
193       char c = alias.charAt( i );
194
195       if ( Character.isLetterOrDigit( c ) )
196         buf.append( c );
197     }
198
199     alias = buf.toString();
200
201     //
202
// make sure it hasn't already been used
203
//
204
Page p = new Page();
205     p.setSiteContext( getSiteContext() );
206
207     try {
208       p.loadForAlias( alias );
209
210       //
211
// did we find ourself?
212
//
213
if ( p.getInt( "id" ) != getInt( "id" ) ) {
214
215         //
216
// try to find an alternative
217
//
218
String JavaDoc tmp = alias;
219         for ( int i = 2; true; i++ ) {
220           alias = tmp + i;
221
222           try {
223             p.loadForAlias( alias );
224           }
225           catch ( PersistableException e ) {
226             break;
227           }
228         }
229       }
230     }
231     catch ( PersistableException e ) {
232       //
233
// no such page
234
//
235
}
236     
237     setString( "aliasname", alias );
238   }
239
240   /**
241    * Adds a panel to the page.
242    */

243   public void addPanel(
244     Panel panel ) {
245
246     if ( panels_.containsKey( panel.getName() ) )
247       throw new ShimException(
248         "Page already has panel \"" + panel.getName() + "\"" );
249
250     panels_.put( panel.getName(), panel );
251   }
252
253   /**
254    * Deletes any panels associated with this page from the database.
255    */

256   private void deletePanels() {
257     Persistable.deleteAll(
258       panelDynaClass_, "page_id=" + getInt( "id" ) );
259   }
260
261   /**
262    * Saves any panels associated with this site context to the database.
263    */

264   private void savePanels() {
265     Persistable p = new Persistable( panelDynaClass_ );
266     p.setInt( "page_id", getInt( "id" ) );
267
268     for ( Iterator JavaDoc iter = panels_.values().iterator(); iter.hasNext(); ) {
269       Panel panel = ( Panel )iter.next();
270       p.set( "name", panel.getName() );
271       p.set( "module", panel.getModuleClass() );
272       p.saveNew();
273     }
274   }
275
276   /**
277    * Loads any panels associated with this site context from the database.
278    */

279   private void loadPanels() {
280     panels_.clear();
281
282     List JavaDoc l =
283       Persistable.loadAll(
284         panelDynaClass_, "page_id=" + getInt( "id" ), "name" );
285
286     for ( Iterator JavaDoc iter = l.iterator(); iter.hasNext(); ) {
287       Persistable p = ( Persistable )iter.next();
288       Panel panel = new Panel();
289       panel.setName( p.getString( "name" ) );
290       panel.setModuleClass( p.getString( "module" ) );
291       panels_.put( panel.getName(), panel );
292     }
293   }
294
295   /**
296    * Instantiates modules for each panel and calls their <tt>init()</tt>
297    * method.
298    */

299   private void initPanels() {
300     for ( Iterator JavaDoc iter = panels_.values().iterator(); iter.hasNext(); ) {
301       Panel panel = ( Panel )iter.next();
302
303       Module module = null;
304       try {
305         module =
306           ( Module )Class.forName( panel.getModuleClass() ).newInstance();
307       }
308       catch ( Exception JavaDoc e ) {
309         throw new ShimException(
310           "Unexpected exception while instantiating module \"" +
311           panel.getModuleClass() + "\":" + e.toString() );
312       }
313
314       module.init( this, panel.getName() );
315
316       panel.setModule( module );
317     }
318   }
319
320   public void saveNew() {
321
322     setInt( "sitecontext_id", getSiteContext().getInt( "id" ) );
323
324     //
325
// save
326
//
327
super.saveNew();
328     savePanels();
329   }
330
331   public void load(
332     Key key ) {
333
334     //
335
// load needs only the id, but to enforce a call to setSiteContext(), make
336
// sure the site context has been set
337
//
338
super.load(
339       key.getWhereClause() + " AND sitecontext_id=" +
340       getSiteContext().getInt( "id" ) );
341
342     loadPanels();
343   }
344
345   public void save() {
346     super.save();
347     deletePanels();
348     savePanels();
349   }
350
351   /**
352    * Deletes any modules associated with this page using {@link Module#destroy
353    * Module.destroy()} and deletes the page; be sure to call {@link #loadFull
354    * loadFull()} before calling this method.
355    */

356   public void delete() {
357
358     for ( Iterator JavaDoc iter = getPanels().values().iterator(); iter.hasNext(); ) {
359       Panel panel = ( Panel )iter.next();
360       panel.getModule().destroy();
361     }
362
363     deletePanels();
364     super.delete();
365   }
366
367   /**
368    * Loads the page and any modules referenced by its panels. {@link
369    * Module#init init()} is called for each module during this operation.
370    */

371   public void loadFull(
372     Key key ) {
373
374     load( key );
375     initPanels();
376   }
377
378   /**
379    * Loads the page for <tt>alias</tt> and any modules referenced by its
380    * panels. {@link Module#init init()} is called for each module during this
381    * operation.
382    */

383   public void loadFullForAlias(
384     String JavaDoc alias ) {
385
386     loadForAlias( alias );
387     initPanels();
388   }
389
390   /**
391    * Loads all pages in the system; a shallow load is performed.
392    */

393   public List JavaDoc loadAll() {
394     List JavaDoc l = loadAll(
395       dynaClass_,
396       "sitecontext_id=" + getSiteContext().getInt( "id" ),
397       "title" );
398
399     //
400
// set the site context for each page
401
//
402
for ( Iterator JavaDoc iter = l.iterator(); iter.hasNext(); ) {
403       Page p = ( Page )iter.next();
404       p.setSiteContext( getSiteContext() );
405     }
406
407     //
408
// sort the pages
409
//
410
Collections.sort( l );
411
412     return l;
413   }
414
415   /**
416    * Loads the page for <tt>alias</tt>, returning <tt>true</tt> if the page was
417    * found.
418    */

419   public void loadForAlias(
420     String JavaDoc alias ) {
421
422     super.load(
423       "sitecontext_id=" + getSiteContext().getInt( "id" ) + " AND aliasname=" +
424       getSqlLiteral( alias ) );
425
426     loadPanels();
427   }
428
429   /**
430    * Copies the page and all of its modules.
431    */

432   public Page copy() {
433
434     Page copy = new Page();
435
436     copy.setSiteContext( getSiteContext() );
437     copy.setString( "title", getString( "title" ) );
438     copy.setString( "alttitle", getString( "alttitle" ) );
439     copy.setDefaultAliasName();
440     copy.setString( "template", getString( "template" ) );
441     copy.setBoolean( "hidden", getBoolean( "hidden" ) );
442     copy.setString( "metadescription", getString( "metadescription" ) );
443     copy.setString( "metakeywords", getString( "metakeywords" ) );
444
445     for ( Iterator JavaDoc iter = getPanels().values().iterator(); iter.hasNext(); ) {
446       Panel panel = ( Panel )iter.next();
447       copy.addPanel( ( Panel )panel.clone() );
448     }
449
450     copy.saveNew();
451
452     for ( Iterator JavaDoc iter = getPanels().values().iterator(); iter.hasNext(); ) {
453       Panel panel = ( Panel )iter.next();
454       panel.getModule().copyTo( copy );
455     }
456
457     return copy;
458   }
459
460   // properties ///////////////////////////////////////////////////////////////
461

462   /**
463    * Returns a map of {@link Panel}s.
464    */

465   public Map JavaDoc getPanels() {
466     return panels_;
467   }
468
469   // attributes ///////////////////////////////////////////////////////////////
470

471   protected Map JavaDoc panels_ = new HashMap JavaDoc();
472
473   protected SiteContext siteContext_ = null;
474 }
475
Popular Tags