001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.sitecontext;
022:
023: import com.methodhead.aikp.AutoIntKeyPersistable;
024: import com.methodhead.persistable.Key;
025: import org.apache.commons.beanutils.DynaClass;
026: import java.util.List;
027: import java.util.Iterator;
028:
029: /**
030: * A base class for {@link com.methodhead.aikp.AutoIntKeyPersistable
031: * AutoIntKeyPersistable}s that implement {@link SiteContextCapable}. Objects
032: * deriving from this class must have an integer <tt>sitecontext_id</tt> field.
033: */
034: public class SiteContextAikp extends AutoIntKeyPersistable {
035:
036: // constructors /////////////////////////////////////////////////////////////
037:
038: public SiteContextAikp(DynaClass dynaClass) {
039:
040: super (dynaClass);
041: }
042:
043: // constants ////////////////////////////////////////////////////////////////
044:
045: // classes //////////////////////////////////////////////////////////////////
046:
047: // methods //////////////////////////////////////////////////////////////////
048:
049: // properties ///////////////////////////////////////////////////////////////
050:
051: /**
052: * Returns this object's site context or throws a <tt>RuntimeException</tt>
053: * if the site context has not been set.
054: */
055: public SiteContext getSiteContext() {
056: if (siteContext_ == null)
057: throw new RuntimeException("Site context has not been set.");
058:
059: return siteContext_;
060: }
061:
062: /**
063: * Sets this object's site context. Throws a <tt>RuntimeException</tt> if
064: * <tt>siteContext</tt> is <tt>null</tt>.
065: */
066: public void setSiteContext(SiteContext siteContext) {
067:
068: if (siteContext == null)
069: throw new RuntimeException(
070: "Trying to set a null site context.");
071:
072: siteContext_ = siteContext;
073:
074: setInt("sitecontext_id", siteContext.getInt("id"));
075: }
076:
077: /**
078: * Extends default behavior to set site context id.
079: */
080: public void saveNew() {
081: setInt("sitecontext_id", getSiteContext().getInt("id"));
082: super .saveNew();
083: }
084:
085: /**
086: * Extends default behavior to set site context id.
087: */
088: public void save() {
089: setInt("sitecontext_id", getSiteContext().getInt("id"));
090: super .save();
091: }
092:
093: /**
094: * Extends default behavior to load for site context.
095: */
096: public void load(Key key) {
097:
098: super .load(key.getWhereClause() + " AND sitecontext_id="
099: + getSiteContext().getInt("id"));
100: }
101:
102: /**
103: * Extends default behavior to load for site context.
104: */
105: public void load(String whereClause) {
106:
107: super .load(whereClause + " AND sitecontext_id="
108: + getSiteContext().getInt("id"));
109: }
110:
111: /**
112: * Loads all objects for the site context.
113: */
114: public List loadAll(String whereClause, String orderByClause) {
115:
116: //
117: // add sitecontext to where clause
118: //
119: if (whereClause == null)
120: whereClause = "sitecontext_id="
121: + getSiteContext().getInt("id");
122: else
123: whereClause += " AND sitecontext_id="
124: + getSiteContext().getInt("id");
125:
126: //
127: // set site context on each object
128: //
129: List list = loadAll(getDynaClass(), whereClause, orderByClause);
130:
131: for (Iterator iter = list.iterator(); iter.hasNext();) {
132: SiteContextAikp aikp = (SiteContextAikp) iter.next();
133: aikp.setSiteContext(getSiteContext());
134: }
135:
136: return list;
137: }
138:
139: // attributes ///////////////////////////////////////////////////////////////
140:
141: private SiteContext siteContext_ = null;
142: }
|