KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > persistable > KeyedPersistable


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.persistable;
22
23 import java.sql.SQLException JavaDoc;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28 import org.apache.commons.beanutils.DynaClass;
29
30 /**
31  * <tt>KeyedPersistable</tt> uniquely identifies persistables using a
32  * {@link com.methodhead.persistable.Key Key}.
33  */

34 public class KeyedPersistable
35 extends
36   Persistable {
37
38   // constructors /////////////////////////////////////////////////////////////
39

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

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

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

52   /**
53    * Saves the persistable by inserting a new row into the table using the
54    * specified <tt>key</tt> to specify key field value(s).
55    */

56   public void saveNew(
57     Key key ) {
58
59     key_ = key;
60     key_.setProperties( this );
61     super.saveNew();
62   }
63
64   /**
65    * Saves the persistable by updating an existing row in the database. Be
66    * sure call <tt>load()</tt> or <tt>saveNew()</tt> before calling this
67    * method.
68    */

69   public void save() {
70
71     if ( key_ == null )
72       throw new PersistableException(
73         "Key has not been set; call load() or saveNew() first." );
74
75     super.save( key_.getWhereClause() );
76   }
77
78   /**
79    * Loads the persistable for the specified <tt>key</tt>, throwing an
80    * exception if no such peristable exists.
81    */

82   public void load(
83     Key key )
84   throws
85     PersistableException {
86
87     key_ = key;
88     super.load( key_.getWhereClause() );
89   }
90
91   /**
92    * This method should not be called and will always throw an exception. Use
93    * {@link KeyedPersistable#load(java.lang.String whereClause, KeyFactory keyFactory) load()} instead.
94    */

95   public void load(
96     String JavaDoc whereClause ) {
97     throw new PersistableException(
98       "This version of load() is illegal for KeyedPersistable; use " +
99       "the version that accepts a KeyFactory as an argument." );
100   }
101
102   /**
103    * Loads the persistable like {@link
104    * com.methodhead.persistable.Persistable#load(java.lang.String)
105    * Persistable.load()} and sets it key with a new key from
106    * <tt>keyFactory</tt>.
107    */

108   public void load(
109     String JavaDoc whereClause,
110     KeyFactory keyFactory ) {
111
112     super.load( whereClause );
113
114     key_ = keyFactory.newInstance();
115     key_.setKeyValue( this );
116   }
117
118   /**
119    * This method should not be called and will always throw an exception. Use
120    * {@link KeyedPersistable#loadAll(org.apache.commons.beanutils.DynaClass,
121    * java.lang.String whereClause, java.lang.String orderByClause,
122    * KeyFactory keyFactory) loadAll()} instead.
123    */

124   public static List JavaDoc loadAll(
125     DynaClass dynaClass,
126     String JavaDoc whereClause,
127     String JavaDoc orderByClause ) {
128     throw new PersistableException(
129       "This version of loadAll() is illegal for KeyedPersistable; use " +
130       "the version that accepts a KeyFactory as an argument." );
131   }
132
133   /**
134    * Loads all persistables like {@link
135    * com.methodhead.persistable.Persistable#loadAll
136    * Persistable.loadAll()} and sets the key for each persistable.
137    */

138   public static List JavaDoc loadAll(
139     DynaClass dynaClass,
140     String JavaDoc whereClause,
141     String JavaDoc orderByClause,
142     KeyFactory keyFactory )
143   throws
144     PersistableException {
145
146     List JavaDoc l = Persistable.loadAll( dynaClass, whereClause, orderByClause );
147
148     //
149
// set the key for all returned persistables
150
//
151
Iterator JavaDoc iter = l.iterator();
152     while ( iter.hasNext() ) {
153       KeyedPersistable p = ( KeyedPersistable )iter.next();
154       Key k = keyFactory.newInstance();
155       k.setKeyValue( p );
156       p.setKey( k );
157     }
158
159     return l;
160   }
161
162   /**
163    * Deletes the persistable. Be sure call <tt>load()</tt> or
164    * <tt>saveNew()</tt> before calling this method.
165    */

166   public void delete() {
167
168     if ( key_ == null )
169       throw new PersistableException(
170         "Key has not been set; call load() or saveNew() first." );
171
172     super.deleteAll( getDynaClass(), key_.getWhereClause() );
173   }
174
175   // properties ///////////////////////////////////////////////////////////////
176

177   public void setKey( Key key ) {
178     key_ = key;
179   }
180
181   public Key getKey() {
182     return key_;
183   }
184
185   // attributes ///////////////////////////////////////////////////////////////
186

187   protected Key key_ = null;
188 }
189
Popular Tags