Source code: com/RuntimeCollective/webapps/bean/UserGroupType.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/bean/UserGroupType.java,v 1.8 2003/09/30 15:13:10 joe Exp $
2 * $Revision: 1.8 $
3 * $Date: 2003/09/30 15:13:10 $
4 *
5 * ====================================================================
6 *
7 * Josephine : http://www.runtime-collective.com/josephine/index.html
8 *
9 * Copyright (C) 2003 Runtime Collective
10 *
11 * This product includes software developed by the
12 * Apache Software Foundation (http://www.apache.org/).
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 */
29
30 package com.RuntimeCollective.webapps.bean;
31
32 import com.RuntimeCollective.webapps.RuntimeParameters;
33 import com.RuntimeCollective.webapps.RuntimeDataSource;
34 import com.RuntimeCollective.webapps.bean.EntityBean;
35 import com.RuntimeCollective.webapps.WebappsException;
36
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Vector;
40
41 import java.sql.SQLException;
42
43 /**
44 * A type of user group.
45 *
46 * @version $Id: UserGroupType.java,v 1.8 2003/09/30 15:13:10 joe Exp $
47 * @see com.RuntimeCollective.webapps.form.UserGroupTypeForm
48 */
49 public class UserGroupType implements EntityBean {
50
51
52 /** The name of a table (in the default database) that indexes this bean type. This table must have a PK column called "id" and a row for each of the beans of this type. */
53 public static final String DATABASE_TABLE = "user_group_type";
54
55
56 // == Instance variables ===================================================
57
58 /** Unique identifier */
59 private int iId;
60
61 /** The name of this type of user group. */
62 private String iName;
63
64
65
66 // == Constructors ===================================================
67
68
69 /** Default constructor generates a new blank bean with a new unique ID.
70 */
71 public UserGroupType() {
72 try {
73 setId( RuntimeDataSource.nextId() );
74 } catch( Exception e ) {
75 RuntimeParameters.logError( this, "Problem getting new id", e );
76 throw new WebappsException( "Problem getting new id "+e );
77 }
78 }
79
80
81 /** Generate a bean from the database for the given primary key.
82 * @param id The primary key of the bean to find.
83 */
84 public UserGroupType( int id ) {
85
86 Object[] results = null;
87
88 try {
89 results = RuntimeDataSource.queryRow( "select name from "+DATABASE_TABLE+" where id="+id );
90 } catch ( Exception e ) {
91 RuntimeParameters.logError( this, "problem getting user group type with id="+id, e );
92 throw new WebappsException( e );
93 }
94
95 if ( results.length==1 ) {
96 setId( id );
97 setName( results[0].toString() );
98 } else {
99 RuntimeParameters.logError( this, "no user group type found for id ="+id );
100 throw new WebappsException( "no user group type found for id ="+id );
101 }
102 }
103
104
105
106 // == Bean property methods ===================================================
107
108
109 /** Get unique identifier */
110 public int getId() { return iId; }
111
112
113 /** Set unique identifier */
114 public void setId(int id) { iId = id; }
115
116
117 /** Get the name of this type of user group. */
118 public String getName() { return iName; }
119
120
121 /** Set the name of this type of user group. */
122 public void setName(String name) { iName = name; }
123
124
125
126 // == Entity Bean methods ===========================================
127
128
129 /** Save this bean to the database. */
130 public void save() {
131 try {
132 RuntimeDataSource.save( getId(), DATABASE_TABLE,
133 new String[] { "name" },
134 new Object[] { getName() }
135 );
136 } catch (Exception e) {
137 RuntimeParameters.logError( this, "problem saving user group type id="+getId(), e );
138 throw new WebappsException( "problem saving user group type id="+getId() );
139 }
140 }
141
142
143 /** Delete this bean from the database. */
144 public void delete() {
145 try {
146 RuntimeDataSource.update( "delete from "+DATABASE_TABLE+" where id="+getId() );
147 } catch (Exception e) {
148 RuntimeParameters.logError( this, "problem deleting user group type id="+getId(), e );
149 throw new WebappsException( "problem deleting user group type id="+getId() );
150 }
151 }
152
153
154 /** Get all the user group types.
155 * @return An iteration of UserGroupTypes. */
156 public static Iterator getAllTypes() {
157 return getAllTypesAsList().iterator();
158 }
159
160 /** Get all the user group types.
161 * @return An iteration of UserGroupTypes. */
162 public static List getAllTypesAsList() {
163 Vector types = new Vector();
164 try {
165 int[] ids = RuntimeDataSource.queryInts( "select id from "+DATABASE_TABLE );
166 for (int i=0; i<ids.length; i++)
167 types.add( (UserGroupType) RuntimeParameters.getStore().get( "com.RuntimeCollective.webapps.bean.UserGroupType", ids[i] ) );
168 } catch (Exception e) {
169 RuntimeParameters.logError( "com.RuntimeCollective.webapps.bean.UserGroupType", "Problem finding group types", e );
170 throw new WebappsException(e);
171 }
172 return types;
173 }
174
175
176 /** Get a user group type for the given name.
177 * @return The user group type with the given name, or null if none exists
178 */
179 public static UserGroupType getForName( String name ) {
180 try {
181 int[] id = RuntimeDataSource.queryInts( "select id from "+DATABASE_TABLE+" where name='"+name+"'" );
182 if ( id.length==0 ) return null;
183 else return (UserGroupType) RuntimeParameters.getStore().get( "com.RuntimeCollective.webapps.bean.UserGroupType", id[0] );
184 } catch (SQLException e) {
185 RuntimeParameters.logError( "com.RuntimeCollective.webapps.bean.UserGroupType", "Problem finding group type for name="+name, e );
186 throw new WebappsException(e);
187 }
188 }
189
190
191 public String toString() {
192 return iName;
193 }
194
195 }
196
197
198
199
200
201
202
203
204