KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sellwin > db > RolePermissionDB


1 package sellwin.db;
2
3 import sellwin.domain.*;
4 import sellwin.utils.*;
5
6 import java.sql.*;
7 import java.util.ArrayList JavaDoc;
8
9 // SellWin http://sourceforge.net/projects/sellwincrm
10
//Contact support@open-app.com for commercial help with SellWin
11
//This software is provided "AS IS", without a warranty of any kind.
12

13 /**
14  * This class implements the DBInterface for
15  * the RolePermission class objects which are stored
16  * in the role_permission database table.
17  */

18 public class RolePermissionDB extends DBType implements DBInterface {
19     private Connection con;
20     private final static String JavaDoc deleteQuery =
21         "DELETE FROM role_permission WHERE pk = ";
22     private final static String JavaDoc deleteUserRoleQuery=
23         "DELETE FROM role_permission WHERE user_role_pk=";
24
25     private final static String JavaDoc insertQuery =
26         "INSERT INTO role_permission " +
27         "VALUES (";
28     private final static String JavaDoc updateQuery =
29             "UPDATE role_permission " +
30             "SET ";
31     private final static String JavaDoc selectQuery =
32             "SELECT user_role_pk, name, r, " +
33             "w, modified_by, modified_date "+
34             "FROM role_permission "+
35             "WHERE pk = ";
36     private final static String JavaDoc selectAllQuery =
37             "SELECT pk, name, r, " +
38             "w, modified_by, modified_date "+
39             "FROM role_permission "+
40             "WHERE user_role_pk = ";
41
42     /**
43      * a do-nothing constructor but necessary to
44      * do the operations offered by this class
45      */

46     public RolePermissionDB() {
47     }
48
49     /**
50      * construct, use a db type
51      * @param dbType the db type to assume
52      */

53     public RolePermissionDB(int dbType) {
54         DB_TYPE = dbType;
55     }
56
57     /**
58      * a version of the constructor when a connection
59      * is alry obtained from somewhere else
60      *
61      * @param con the Connection to use
62      */

63     public RolePermissionDB(Connection con) {
64         this.con = con;
65     }
66
67     /**
68      * return the Connection in use
69      *
70      * @return the Connection in use
71      */

72     public Connection getConnection() {
73         return this.con;
74     }
75
76     /**
77      * set the Connection to use
78      *
79      * @param con the Connection to use for any future IO's
80      */

81     public final void setConnection(Connection con)
82         throws SQLException {
83
84         this.con = con;
85     }
86
87     /**
88      * select a single role_permission row using the passed
89      * primary key
90      *
91      * @param name description
92      * @return the RolePermission row(s) that were selected
93      * @exception java.sql.SQLException
94      */

95     public final Object JavaDoc selectRow(Object JavaDoc pk)
96         throws SQLException {
97
98         RolePermission rp = new RolePermission();
99         rp.setPK(((Long JavaDoc)pk).longValue());
100
101         Statement stmt = null;
102         ResultSet rs = null;
103         String JavaDoc query = selectQuery + rp.getPK();
104
105         try {
106             stmt = con.createStatement();
107             if (Prefs.DEBUG) LogWrite.write(query);
108             rs = stmt.executeQuery(query);
109
110             int i;
111
112             while (rs.next()) {
113                 i=1;
114                 rp.setUserRolePK(rs.getLong(i)); i++;
115                 rp.setName(rs.getString(i)); i++;
116                 String JavaDoc x = rs.getString(i); i++;
117                 boolean val;
118                 if (x.charAt(0) == 'Y') val=true; else val=false;
119                 rp.setRead(val);
120                 x = rs.getString(i); i++;
121                 if (x.charAt(0) == 'Y') val=true; else val=false;
122                 rp.setWrite(val);
123                 rp.setModifiedBy(rs.getString(i)); i++;
124                 rp.setModifiedDate(rs.getDate(i));
125             }
126
127         } catch (SQLException e) {
128             throw e;
129         } finally {
130             try {
131                 if (rs != null) rs.close();
132             } catch (SQLException x) { throw x; }
133             try {
134                 if (stmt != null) stmt.close();
135             } catch (SQLException x) { throw x; }
136         }
137
138         return rp;
139     }
140
141
142     /**
143      * update a single role_permission row using the passed
144      * RolePermission object's attributes. All columns
145      * get updated by this routine regardless of whether
146      * an attribute was modified or not.
147      *
148      * @param name description
149      * @exception java.sql.SQLException
150      */

151     public final void updateRow(Object JavaDoc obj)
152         throws SQLException {
153
154         RolePermission role_permission = (RolePermission)obj;
155
156         StringBuffer JavaDoc query = new StringBuffer JavaDoc(updateQuery);
157         Statement stmt = con.createStatement();
158
159         query.append("name=");
160         query.append(JDBC.quoteMore(role_permission.getName()));
161         String JavaDoc x;
162         query.append("r=");
163         if (role_permission.getRead()) x="Y"; else x="N";
164         query.append(JDBC.quoteMore(x));
165         query.append("w=");
166         if (role_permission.getWrite()) x="Y"; else x="N";
167         query.append(JDBC.quoteMore(x));
168         query.append("modified_by=");
169         query.append(JDBC.quoteMore(role_permission.getModifiedBy()));
170         query.append("modified_date=");
171         if (DB_TYPE == Prefs.MYSQL)
172             query.append("CURRENT_DATE");
173         else
174             query.append("SYSDATE");
175         query.append(" WHERE pk=");
176         query.append(role_permission.getPK());
177
178         if (Prefs.DEBUG) LogWrite.write(query.toString());
179         int updatedRows = stmt.executeUpdate(query.toString());
180     }
181
182     /**
183      * insert a new role_permission row using the passed
184      * RolePermission object as the column values.
185      *
186      * @param obj the RolePermission we are adding or loading
187      * @param load true if loading, false if adding
188      * @return the newly assigned primary key of the new row
189      * @exception java.sql.SQLException
190      */

191     public final long insertRow(Object JavaDoc obj, boolean load)
192         throws SQLException {
193
194         RolePermission role_permission = (RolePermission)obj;
195
196         if (!load)
197             role_permission.setPK(DBUtils.generatePK());
198
199         StringBuffer JavaDoc query = new StringBuffer JavaDoc(insertQuery);
200         Statement stmt = con.createStatement();
201
202         query.append(role_permission.getPK()).append(",");
203         query.append(role_permission.getUserRolePK()).append(",");
204         query.append(JDBC.quoteMore(role_permission.getName()));
205         String JavaDoc x;
206         if (role_permission.getRead()) x="Y"; else x="N";
207         query.append(JDBC.quoteMore(x));
208         if (role_permission.getWrite()) x="Y"; else x="N";
209         query.append(JDBC.quoteMore(x));
210         query.append(JDBC.quoteMore(role_permission.getModifiedBy()));
211         if (DB_TYPE == Prefs.MYSQL)
212             query.append("CURRENT_DATE");
213         else
214             query.append("SYSDATE");
215         query.append(")");
216
217         if (Prefs.DEBUG) LogWrite.write(query.toString());
218         int rc = stmt.executeUpdate(query.toString());
219
220         return role_permission.getPK();
221     }
222
223     /**
224      * delete a single role_permission row using the passed
225      * primary key value
226      *
227      * @param ojb primary key stored in a Long
228      * @exception java.sql.SQLException
229      */

230     public final void deleteRow(Object JavaDoc obj)
231         throws SQLException {
232
233         long pkValue = ((Long JavaDoc)obj).longValue();
234
235         String JavaDoc query = deleteQuery + pkValue;
236
237         Statement stmt = null;
238
239         try {
240             stmt = con.createStatement();
241             if (Prefs.DEBUG) LogWrite.write(query);
242             stmt.executeUpdate(query);
243         } catch (SQLException e) {
244             throw e;
245         } finally {
246             try {
247                 if (stmt != null) stmt.close();
248             } catch (SQLException x) { }
249         }
250     }
251
252     /**
253      * delete all role_permission rows for a given user role
254      * using the passed user_roleprimary key value
255      *
256      * @param ojb primary key stored in a Long
257      * @exception java.sql.SQLException
258      */

259     public final void deleteUserRoleRows(Object JavaDoc obj)
260         throws SQLException {
261
262         long pkValue = ((Long JavaDoc)obj).longValue();
263
264         String JavaDoc query = deleteUserRoleQuery + pkValue;
265
266         Statement stmt = null;
267
268         try {
269             stmt = con.createStatement();
270             if (Prefs.DEBUG) LogWrite.write(query);
271             stmt.executeUpdate(query);
272         } catch (SQLException e) {
273             throw e;
274         } finally {
275             try {
276                 if (stmt != null) stmt.close();
277             } catch (SQLException x) { }
278         }
279     }
280
281     /**
282      * select all role_permission rows using the passed
283      * user_role primary key
284      *
285      * @param name description
286      * @return the List of RolePermission row(s) that were selected
287      * @exception java.sql.SQLException
288      */

289     public final void selectRows(long user_role_pk, ArrayList JavaDoc perms)
290         throws SQLException {
291
292         RolePermission rp;
293
294         Statement stmt = null;
295         ResultSet rs = null;
296         String JavaDoc query = selectAllQuery + user_role_pk;
297
298         try {
299             stmt = con.createStatement();
300             if (Prefs.DEBUG) LogWrite.write(query);
301             rs = stmt.executeQuery(query);
302
303             int i;
304
305             while (rs.next()) {
306                 i=1;
307                 rp = new RolePermission();
308                 rp.setPK(rs.getLong(i)); i++;
309                 rp.setUserRolePK(user_role_pk); //from the passed parm
310
rp.setName(rs.getString(i)); i++;
311                 String JavaDoc x = rs.getString(i); i++;
312                 boolean val;
313                 if (x.charAt(0) == 'Y') val=true; else val=false;
314                 rp.setRead(val);
315                 x = rs.getString(i); i++;
316                 if (x.charAt(0) == 'Y') val=true; else val=false;
317                 rp.setWrite(val);
318                 rp.setModifiedBy(rs.getString(i)); i++;
319                 rp.setModifiedDate(rs.getDate(i));
320                 perms.add(rp);
321             }
322         } catch (SQLException e) {
323             throw e;
324         } finally {
325             try {
326                 if (rs != null) rs.close();
327             } catch (SQLException x) { throw x; }
328             try {
329                 if (stmt != null) stmt.close();
330             } catch (SQLException x) { throw x; }
331         }
332
333     }
334     /**
335      * truncate the whole table
336      *
337      * @exception java.sql.SQLException
338      */

339     public final void truncate()
340         throws SQLException {
341
342         String JavaDoc query = "truncate table role_permission";
343    
344         Statement stmt = null;
345         try {
346             stmt = con.createStatement();
347             if (Prefs.DEBUG) LogWrite.write(query);
348             stmt.executeUpdate(query);
349         } catch (SQLException e) {
350             throw e;
351         } finally {
352             try { if (stmt != null) stmt.close();
353             } catch (SQLException x) { }
354         }
355     }
356
357 }
358
Popular Tags