KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > sitecontext > SiteContextAikp


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 com.methodhead.aikp.AutoIntKeyPersistable;
24 import com.methodhead.persistable.Key;
25 import org.apache.commons.beanutils.DynaClass;
26 import java.util.List JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 /**
30  * A base class for {@link com.methodhead.aikp.AutoIntKeyPersistable
31  * AutoIntKeyPersistable}s that implement {@link SiteContextCapable}. Objects
32  * deriving from this class must have an integer <tt>sitecontext_id</tt> field.
33  */

34 public class SiteContextAikp
35 extends
36   AutoIntKeyPersistable {
37
38   // constructors /////////////////////////////////////////////////////////////
39

40   public SiteContextAikp(
41     DynaClass dynaClass ) {
42
43     super( dynaClass );
44   }
45
46   // constants ////////////////////////////////////////////////////////////////
47

48   // classes //////////////////////////////////////////////////////////////////
49

50   // methods //////////////////////////////////////////////////////////////////
51

52   // properties ///////////////////////////////////////////////////////////////
53

54   /**
55    * Returns this object's site context or throws a <tt>RuntimeException</tt>
56    * if the site context has not been set.
57    */

58   public SiteContext getSiteContext() {
59     if ( siteContext_ == null )
60       throw new RuntimeException JavaDoc( "Site context has not been set." );
61
62     return siteContext_;
63   }
64
65   /**
66    * Sets this object's site context. Throws a <tt>RuntimeException</tt> if
67    * <tt>siteContext</tt> is <tt>null</tt>.
68    */

69   public void setSiteContext(
70     SiteContext siteContext ) {
71
72     if ( siteContext == null )
73       throw new RuntimeException JavaDoc( "Trying to set a null site context." );
74
75     siteContext_ = siteContext;
76
77     setInt( "sitecontext_id", siteContext.getInt( "id" ) );
78   }
79
80   /**
81    * Extends default behavior to set site context id.
82    */

83   public void saveNew() {
84     setInt( "sitecontext_id", getSiteContext().getInt( "id" ) );
85     super.saveNew();
86   }
87
88   /**
89    * Extends default behavior to set site context id.
90    */

91   public void save() {
92     setInt( "sitecontext_id", getSiteContext().getInt( "id" ) );
93     super.save();
94   }
95
96   /**
97    * Extends default behavior to load for site context.
98    */

99   public void load(
100     Key key ) {
101
102     super.load(
103       key.getWhereClause() + " AND sitecontext_id=" +
104       getSiteContext().getInt( "id" ) );
105   }
106
107   /**
108    * Extends default behavior to load for site context.
109    */

110   public void load(
111     String JavaDoc whereClause ) {
112
113     super.load(
114       whereClause + " AND sitecontext_id=" +
115       getSiteContext().getInt( "id" ) );
116   }
117
118   /**
119    * Loads all objects for the site context.
120    */

121   public List JavaDoc loadAll(
122     String JavaDoc whereClause,
123     String JavaDoc orderByClause ) {
124
125     //
126
// add sitecontext to where clause
127
//
128
if ( whereClause == null )
129       whereClause = "sitecontext_id=" + getSiteContext().getInt( "id" );
130     else
131       whereClause += " AND sitecontext_id=" + getSiteContext().getInt( "id" );
132
133     //
134
// set site context on each object
135
//
136
List JavaDoc list = loadAll( getDynaClass(), whereClause, orderByClause );
137
138     for ( Iterator JavaDoc iter = list.iterator(); iter.hasNext(); ) {
139       SiteContextAikp aikp = ( SiteContextAikp )iter.next();
140       aikp.setSiteContext( getSiteContext() );
141     }
142
143     return list;
144   }
145
146   // attributes ///////////////////////////////////////////////////////////////
147

148   private SiteContext siteContext_ = null;
149 }
150
Popular Tags