KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > aikp > AutoIntKeyPersistable


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.aikp;
22
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.SQLException JavaDoc;
25
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import com.methodhead.persistable.ConnectionSingleton;
30 import com.methodhead.persistable.Persistable;
31 import com.methodhead.persistable.Key;
32 import com.methodhead.persistable.KeyedPersistable;
33 import com.methodhead.persistable.KeyFactory;
34 import com.methodhead.persistable.PersistableException;
35 import org.apache.commons.beanutils.DynaClass;
36 import org.apache.log4j.Logger;
37 import org.apache.commons.lang.exception.ExceptionUtils;
38 import com.methodhead.persistable.ConnectionSingleton;
39
40 /**
41  * Extends <tt>KeyedPersistable</tt> to generate new <tt>IntKeys</tt>
42  * automatically. Ids are managed using a simple database table; make sure
43  * this table exists in the database:
44  * <pre>
45  * CREATE TABLE mh_id (
46  * name VARCHAR(32),
47  * value INT
48  * );</pre>
49  * <tt>saveNew(Key)</tt> is overridden and made protected. Instead, use
50  * <tt>saveNew()</tt> which generates the id itself.
51  */

52 public abstract class AutoIntKeyPersistable
53 extends
54   KeyedPersistable {
55
56   // constructors /////////////////////////////////////////////////////////////
57

58   public AutoIntKeyPersistable(
59     DynaClass dynaClass ) {
60
61     super( dynaClass );
62   }
63
64   // constants ////////////////////////////////////////////////////////////////
65

66   // classes //////////////////////////////////////////////////////////////////
67

68   private static class IntKeyFactory
69   implements
70     KeyFactory {
71
72     public Key newInstance() {
73       return new IntKey();
74     }
75   }
76
77   // methods //////////////////////////////////////////////////////////////////
78

79   /**
80    * Returns a new id for the specified <tt>key</tt>.
81    */

82   protected synchronized IntKey newKey(
83     String JavaDoc name ) {
84
85     String JavaDoc sql = null;
86
87     ResultSet JavaDoc rs = null;
88
89     try {
90       sql =
91         "SELECT " +
92         " value " +
93         "FROM " +
94         " mh_id " +
95         "WHERE " +
96         " name=" + getSqlLiteral( name );
97
98       rs = ConnectionSingleton.runQuery( sql );
99
100       if ( rs == null ) {
101         throw new SQLException JavaDoc( "Null result set." );
102       }
103
104       if ( !rs.next() ) {
105         sql =
106           "INSERT INTO " +
107           " mh_id " +
108           "( " +
109           " name, " +
110           " value ) " +
111           "VALUES ( " +
112           " " + getSqlLiteral( name ) + ", " +
113           " 2" +
114           ")";
115
116         ConnectionSingleton.runUpdate( sql );
117
118         return new IntKey( 1 );
119       }
120       else {
121         int value = rs.getInt( "value" );
122
123         sql =
124           "UPDATE " +
125           " mh_id " +
126           "SET " +
127           " value=" + ( value + 1 ) + " " +
128           "WHERE " +
129           " name=" + getSqlLiteral( name );
130
131         ConnectionSingleton.runUpdate( sql );
132
133         return new IntKey( value );
134       }
135     }
136     catch ( SQLException JavaDoc e ) {
137       String JavaDoc msg = "Creating new key for \"" + name + "\". " + ExceptionUtils.getStackTrace( e );
138       logger_.error( msg );
139       throw new RuntimeException JavaDoc( msg );
140     }
141     finally {
142       ConnectionSingleton.close( rs );
143     }
144   }
145
146   /**
147    * Saves the persistable, automatically generating a new id for it.
148    */

149   public void saveNew() {
150     super.saveNew( newKey( getDynaClass().getName() ) );
151   }
152
153   /**
154    * Loads the persistable like {@link
155    * com.methodhead.persistable.Persistable#load(java.lang.String)
156    * Persistable.load()} and sets its key appropriately.
157    */

158   public void load(
159     String JavaDoc whereClause ) {
160
161     super.load( whereClause, new IntKeyFactory() );
162     
163   }
164
165   /**
166    * See super class's documentation.
167    */

168   public static List JavaDoc loadAll(
169     DynaClass dynaClass,
170     String JavaDoc whereClause,
171     String JavaDoc orderByClause ) {
172
173     return
174       KeyedPersistable.loadAll(
175         dynaClass,
176         whereClause,
177         orderByClause,
178         new IntKeyFactory() );
179   }
180
181   // properties ///////////////////////////////////////////////////////////////
182

183   // attributes ///////////////////////////////////////////////////////////////
184

185   private static Logger logger_ = Logger.getLogger( AutoIntKeyPersistable.class );
186 }
187
Popular Tags