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.persistable;
022:
023: import java.sql.SQLException;
024:
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.Map;
028: import org.apache.commons.beanutils.DynaClass;
029:
030: /**
031: * <tt>KeyedPersistable</tt> uniquely identifies persistables using a
032: * {@link com.methodhead.persistable.Key Key}.
033: */
034: public class KeyedPersistable extends Persistable {
035:
036: // constructors /////////////////////////////////////////////////////////////
037:
038: public KeyedPersistable(DynaClass dynaClass) {
039:
040: super (dynaClass);
041: }
042:
043: // constants ////////////////////////////////////////////////////////////////
044:
045: // classes //////////////////////////////////////////////////////////////////
046:
047: // methods //////////////////////////////////////////////////////////////////
048:
049: /**
050: * Saves the persistable by inserting a new row into the table using the
051: * specified <tt>key</tt> to specify key field value(s).
052: */
053: public void saveNew(Key key) {
054:
055: key_ = key;
056: key_.setProperties(this );
057: super .saveNew();
058: }
059:
060: /**
061: * Saves the persistable by updating an existing row in the database. Be
062: * sure call <tt>load()</tt> or <tt>saveNew()</tt> before calling this
063: * method.
064: */
065: public void save() {
066:
067: if (key_ == null)
068: throw new PersistableException(
069: "Key has not been set; call load() or saveNew() first.");
070:
071: super .save(key_.getWhereClause());
072: }
073:
074: /**
075: * Loads the persistable for the specified <tt>key</tt>, throwing an
076: * exception if no such peristable exists.
077: */
078: public void load(Key key) throws PersistableException {
079:
080: key_ = key;
081: super .load(key_.getWhereClause());
082: }
083:
084: /**
085: * This method should not be called and will always throw an exception. Use
086: * {@link KeyedPersistable#load(java.lang.String whereClause, KeyFactory keyFactory) load()} instead.
087: */
088: public void load(String whereClause) {
089: throw new PersistableException(
090: "This version of load() is illegal for KeyedPersistable; use "
091: + "the version that accepts a KeyFactory as an argument.");
092: }
093:
094: /**
095: * Loads the persistable like {@link
096: * com.methodhead.persistable.Persistable#load(java.lang.String)
097: * Persistable.load()} and sets it key with a new key from
098: * <tt>keyFactory</tt>.
099: */
100: public void load(String whereClause, KeyFactory keyFactory) {
101:
102: super .load(whereClause);
103:
104: key_ = keyFactory.newInstance();
105: key_.setKeyValue(this );
106: }
107:
108: /**
109: * This method should not be called and will always throw an exception. Use
110: * {@link KeyedPersistable#loadAll(org.apache.commons.beanutils.DynaClass,
111: * java.lang.String whereClause, java.lang.String orderByClause,
112: * KeyFactory keyFactory) loadAll()} instead.
113: */
114: public static List loadAll(DynaClass dynaClass, String whereClause,
115: String orderByClause) {
116: throw new PersistableException(
117: "This version of loadAll() is illegal for KeyedPersistable; use "
118: + "the version that accepts a KeyFactory as an argument.");
119: }
120:
121: /**
122: * Loads all persistables like {@link
123: * com.methodhead.persistable.Persistable#loadAll
124: * Persistable.loadAll()} and sets the key for each persistable.
125: */
126: public static List loadAll(DynaClass dynaClass, String whereClause,
127: String orderByClause, KeyFactory keyFactory)
128: throws PersistableException {
129:
130: List l = Persistable.loadAll(dynaClass, whereClause,
131: orderByClause);
132:
133: //
134: // set the key for all returned persistables
135: //
136: Iterator iter = l.iterator();
137: while (iter.hasNext()) {
138: KeyedPersistable p = (KeyedPersistable) iter.next();
139: Key k = keyFactory.newInstance();
140: k.setKeyValue(p);
141: p.setKey(k);
142: }
143:
144: return l;
145: }
146:
147: /**
148: * Deletes the persistable. Be sure call <tt>load()</tt> or
149: * <tt>saveNew()</tt> before calling this method.
150: */
151: public void delete() {
152:
153: if (key_ == null)
154: throw new PersistableException(
155: "Key has not been set; call load() or saveNew() first.");
156:
157: super .deleteAll(getDynaClass(), key_.getWhereClause());
158: }
159:
160: // properties ///////////////////////////////////////////////////////////////
161:
162: public void setKey(Key key) {
163: key_ = key;
164: }
165:
166: public Key getKey() {
167: return key_;
168: }
169:
170: // attributes ///////////////////////////////////////////////////////////////
171:
172: protected Key key_ = null;
173: }
|