KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > api > dataxs > OwxUserRolesManager


1 package com.raptus.owxv3.api.dataxs;
2
3 import java.util.Vector JavaDoc;
4 import java.sql.*;
5 import com.raptus.owxv3.api.dataxs.*;
6 import com.raptus.owxv3.*;
7
8 /******************SQL2JAVA_IMPORT_BEGIN******************/
9
10 /******************SQL2JAVA_IMPORT_END********************/
11
12
13 public class OwxUserRolesManager extends SQLDatabaseManager
14 /******************SQL2JAVA_EXTENDS_BEGIN******************/
15
16 /******************SQL2JAVA_EXTENDS_END********************/
17
18 {
19
20 /******************SQL2JAVA_CLASS_BEGIN******************/
21
22 /******************SQL2JAVA_CLASS_END********************/
23
24
25     protected static final String JavaDoc tableNamebase = "owx_user_roles";
26
27     public OwxUserRolesManager()
28     {
29         tableID = tableNamebase;
30         initialize();
31     }
32
33     public OwxUserRolesManager(String JavaDoc tableNamespace)
34     {
35         this.tableNamespace = tableNamespace;
36         if(tableNamespace != null && tableNamespace.length() > 0)
37             tableID = tableNamebase + tableNamespace;
38         else
39             tableID = tableNamebase;
40         initialize();
41     }
42
43     public static final int USER_NAME = 0;
44     public static final int ROLE_NAME = 1;
45     public static final int USER_ID = 2;
46
47     protected void initialize()
48     {
49         ALL_FIELDS = "\"user_name\""
50                             + ",\"role_name\""
51                             + ",\"user_id\"";
52         S2J_FIELD_NAMES = new String JavaDoc[]
53         {
54         tableID + ".\"user_name\"",
55         tableID + ".\"role_name\"",
56         tableID + ".\"user_id\""
57         };
58     }
59
60     public OwxUserRoles loadByKey(int _userId) throws SQLException {
61         Connection _conn = null;
62         try {
63             _conn = openConnection();
64             OwxUserRoles _val = loadByKey(_userId, _conn);
65             closeConnection(_conn);
66             return _val;
67         }
68         catch(SQLException e) {
69             if(_conn != null) try { closeConnection(_conn); } catch(SQLException e2) { }
70             throw e;
71         }
72     }
73
74     public OwxUserRoles loadByKey(int _userId, Connection _conn) throws SQLException {
75         PreparedStatement _pstmt = null;
76         try {
77             _pstmt = _conn.prepareStatement("SELECT " + ALL_FIELDS + " FROM " + tableID + " WHERE \"user_id\"=?");
78             _pstmt.setLong(1, _userId);
79             OwxUserRoles _list[] = loadByPreparedStatement(_pstmt);
80             _pstmt.close();
81             if(_list.length < 1) return null;
82             else return _list[0];
83         }
84         catch(SQLException e) {
85             if(_pstmt != null) try { _pstmt.close(); } catch(SQLException e2) { }
86             throw e;
87         }
88     }
89
90     public OwxUserRoles[] loadAll() throws SQLException {
91         Connection _conn = null;
92         try {
93             _conn = openConnection();
94             OwxUserRoles _list[] = loadAll(_conn);
95             closeConnection(_conn);
96             return _list;
97         }
98         catch(SQLException e) {
99             if(_conn != null) try { closeConnection(_conn); } catch(SQLException e2) { }
100             throw e;
101         }
102     }
103
104     public OwxUserRoles[] loadAll(Connection _conn) throws SQLException {
105         PreparedStatement _pstmt = null;
106         try {
107             _pstmt = _conn.prepareStatement("SELECT " + ALL_FIELDS + " FROM " + tableID );
108             OwxUserRoles _list[] = loadByPreparedStatement(_pstmt);
109             _pstmt.close();
110             return _list;
111         }
112         catch(SQLException e) {
113             if(_pstmt != null) try { _pstmt.close(); } catch(SQLException e2) { }
114             throw e;
115         }
116     }
117
118     public OwxUserRoles[] loadByPreparedStatement(PreparedStatement pstmt) throws SQLException {
119         return loadByPreparedStatement(pstmt, null, 0);
120     }
121
122     public OwxUserRoles[] loadByPreparedStatement(PreparedStatement pstmt, int maxRows) throws SQLException {
123         return loadByPreparedStatement(pstmt, null, maxRows);
124     }
125
126     public OwxUserRoles[] loadByPreparedStatement(PreparedStatement pstmt, int[] fieldList) throws SQLException {
127         return loadByPreparedStatement(pstmt, fieldList, 0);
128     }
129
130     public OwxUserRoles[] loadByPreparedStatement(PreparedStatement pstmt, int[] fieldList, int maxRows) throws SQLException {
131         ResultSet rs = null;
132         try {
133             rs = pstmt.executeQuery();
134             java.util.Vector JavaDoc v = new java.util.Vector JavaDoc();
135             while(rs.next() && (maxRows == 0 || v.size() < maxRows)) {
136                 if(fieldList == null) v.addElement(decodeRow(rs));
137                 else v.addElement(decodeRow(rs, fieldList));
138             }
139             rs.close();
140             OwxUserRoles list[] = new OwxUserRoles[v.size()];
141             v.copyInto(list);
142             return list;
143         }
144         catch(SQLException e) {
145             if(rs!=null) try { rs.close();} catch(Exception JavaDoc e2) {}
146             throw e;
147         }
148     }
149
150     public OwxUserRoles[] loadByWhere(String JavaDoc where) throws SQLException {
151         return loadByWhere(where, (int[])null, 0, (String JavaDoc)null);
152     }
153     public OwxUserRoles[] loadByWhere(String JavaDoc where, int maxRows) throws SQLException {
154         return loadByWhere(where, (int[])null, maxRows, (String JavaDoc)null);
155     }
156     public OwxUserRoles[] loadByWhere(String JavaDoc where, String JavaDoc orderby, int maxRows) throws SQLException {
157         return loadByWhere(where, (int[])null, maxRows, orderby);
158     }
159     public OwxUserRoles[] loadByWhere(String JavaDoc where, int[] fieldList) throws SQLException {
160         return loadByWhere(where, fieldList, 0, (String JavaDoc)null);
161     }
162     public OwxUserRoles[] loadByWhere(String JavaDoc where, int[] fieldList, int maxRows, String JavaDoc orderby) throws SQLException {
163         Connection conn = null;
164         try {
165             conn = openConnection();
166             OwxUserRoles _list[] = loadByWhere(where, conn, fieldList, maxRows, orderby);
167             closeConnection(conn);
168             return _list;
169         }
170         catch(SQLException e) {
171             if(conn!=null) try { closeConnection(conn);} catch(Exception JavaDoc e2) {}
172             throw e;
173         }
174     }
175
176     public OwxUserRoles[] loadByWhere(String JavaDoc where, Connection conn) throws SQLException {
177         return loadByWhere(where, conn, (int[])null, 0, (String JavaDoc)null);
178     }
179
180     public OwxUserRoles[] loadByWhere(String JavaDoc where, Connection conn, int[] fieldList) throws SQLException {
181         return loadByWhere(where, conn, fieldList, 0, (String JavaDoc)null);
182     }
183
184     public OwxUserRoles[] loadByWhere(String JavaDoc where, Connection conn, int[] fieldList, int maxRows, String JavaDoc orderby)
185                 throws SQLException {
186         String JavaDoc sql = null;
187         if(fieldList == null)
188         {
189             sql = "select " + ALL_FIELDS + " from " + tableID + " where (" + where + ")";
190             if(orderby != null) sql += " order by " + orderby;
191         }
192         else
193         {
194             StringBuffer JavaDoc buff = new StringBuffer JavaDoc(128);
195             buff.append("select ");
196             for(int i = 0; i < fieldList.length; i++)
197             {
198                 if(i != 0) buff.append(",");
199                     buff.append(S2J_FIELD_NAMES[fieldList[i]]);
200             }
201             buff.append(" from " + tableID + " ");
202             buff.append(" where (" + where + ")");
203             if(orderby != null)
204                 buff.append(" order by " + orderby);
205             sql = buff.toString();
206         }
207         Statement stmt = null;
208         ResultSet rs = null;
209         try {
210             stmt = conn.createStatement();
211             rs = stmt.executeQuery(sql);
212             java.util.Vector JavaDoc v = new java.util.Vector JavaDoc();
213             while(rs.next() && (maxRows == 0 || v.size() < maxRows)) {
214                 if(fieldList == null) v.addElement(decodeRow(rs));
215                 else v.addElement(decodeRow(rs, fieldList));
216             }
217             rs.close();
218             stmt.close();
219
220             OwxUserRoles _list[] = new OwxUserRoles[v.size()];
221             v.copyInto(_list);
222             return _list;
223         }
224         catch(SQLException e) {
225             if(rs!=null) try { rs.close();} catch(Exception JavaDoc e2) {}
226             if(stmt!=null) try { stmt.close();} catch(Exception JavaDoc e2) {}
227             throw e;
228         }
229     }
230
231     public int deleteByKey(int _userId) throws SQLException {
232         Connection _conn = null;
233         try {
234             _conn = openConnection();
235             int _rows = deleteByKey(_userId, _conn);
236             closeConnection(_conn);
237             return _rows;
238         }
239         catch(SQLException e) {
240             if(_conn!=null) try { closeConnection(_conn);} catch(Exception JavaDoc e2) {}
241             throw e;
242         }
243     }
244
245     public int deleteByKey(int _userId, Connection _conn) throws SQLException {
246         PreparedStatement _pstmt = null;
247         try {
248             _pstmt = _conn.prepareStatement("DELETE from " + tableID + " WHERE \"user_id\"=?");
249             _pstmt.setLong(1, _userId);
250             int _rows = _pstmt.executeUpdate();
251             _pstmt.close();
252             return _rows;
253         }
254         catch(SQLException e) {
255             if(_pstmt!=null) try { _pstmt.close();} catch(Exception JavaDoc e2) {}
256             throw e;
257         }
258     }
259
260     public void save(OwxUserRoles obj) throws SQLException {
261         if(!obj.isModifiedS2J()) return;
262         Connection _conn = null;
263         try {
264             _conn = openConnection();
265             save(obj, _conn);
266             closeConnection(_conn);
267         }
268         catch(SQLException e) {
269             if(_conn!=null) try { closeConnection(_conn);} catch(Exception JavaDoc e2) {}
270             throw e;
271         }
272     }
273
274     public void save(OwxUserRoles obj, Connection _conn) throws SQLException {
275         PreparedStatement _pstmt = null;
276         try {
277             if(obj.isNew()) {
278                 int _dirtyCount = 0;
279                 StringBuffer JavaDoc _sql = new StringBuffer JavaDoc("INSERT into " + tableID + " (");
280                 if(obj.userNameIsModifiedS2j()) { _sql.append("\"user_name\"").append(","); _dirtyCount++; }
281                 if(obj.roleNameIsModifiedS2j()) { _sql.append("\"role_name\"").append(","); _dirtyCount++; }
282                 if(obj.userIdIsModifiedS2j()) { _sql.append("\"user_id\"").append(","); _dirtyCount++; }
283                 _sql.setLength(_sql.length() - 1);
284                 _sql.append(") values (");
285                 for(int i = 0; i < _dirtyCount; i++) _sql.append("?,");
286                 _sql.setLength(_sql.length() - 1);
287                 _sql.append(")");
288
289                 _pstmt = _conn.prepareStatement(_sql.toString());
290                 _dirtyCount = 0;
291                 if(obj.userNameIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getUserName()); }
292                 if(obj.roleNameIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getRoleName()); }
293                 if(obj.userIdIsModifiedS2j()) { _pstmt.setLong(++_dirtyCount, obj.getUserId()); }
294                 _pstmt.executeUpdate();
295                 obj.setIsNew(false);
296                 obj.resetIsModifiedS2J();
297             }
298             else {
299                 StringBuffer JavaDoc _sql = new StringBuffer JavaDoc("UPDATE " + tableID + " SET ");
300                 if(obj.userNameIsModifiedS2j()) { _sql.append("\"user_name\"").append("=?,"); }
301                 if(obj.roleNameIsModifiedS2j()) { _sql.append("\"role_name\"").append("=?,"); }
302                 if(obj.userIdIsModifiedS2j()) { _sql.append("\"user_id\"").append("=?,"); }
303                 _sql.setLength(_sql.length() - 1);
304                 _sql.append(" WHERE ");
305                 _sql.append("\"user_name\"=?");
306                 _pstmt = _conn.prepareStatement(_sql.toString());
307                 int _dirtyCount = 0;
308                 if(obj.userNameIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getUserName()); }
309                 if(obj.roleNameIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getRoleName()); }
310                 if(obj.userIdIsModifiedS2j()) { _pstmt.setLong(++_dirtyCount, obj.getUserId()); }
311                 _pstmt.setLong(++_dirtyCount, obj.getUserId());
312                 _pstmt.executeUpdate();
313                 obj.resetIsModifiedS2J();
314             }
315             _pstmt.close();
316         }
317         catch(SQLException e) {
318             if(_pstmt!=null) try { _pstmt.close();} catch(Exception JavaDoc e2) {}
319             throw e;
320         }
321     }
322
323     private OwxUserRoles decodeRow(ResultSet rs) throws SQLException {
324         OwxUserRoles obj = new OwxUserRoles();
325         obj.setUserName(rs.getString(1));
326         obj.setRoleName(rs.getString(2));
327         obj.setUserId(rs.getInt(3));
328         obj.setIsNew(false);
329         obj.resetIsModifiedS2J();
330
331         return obj;
332     }
333
334     private OwxUserRoles decodeRow(ResultSet rs, int[] fieldList) throws SQLException {
335         OwxUserRoles obj = new OwxUserRoles();
336         int pos = 0;
337         for(int i = 0; i < fieldList.length; i++) {
338             switch(fieldList[i]) {
339                 case USER_NAME: obj.setUserName(rs.getString(++pos));
340                     break;
341                 case ROLE_NAME: obj.setRoleName(rs.getString(++pos));
342                     break;
343                 case USER_ID: obj.setUserId(rs.getInt(++pos));
344                     break;
345             }
346         }
347         obj.setIsNew(false);
348         obj.resetIsModifiedS2J();
349
350         return obj;
351     }
352
353 }
354
Popular Tags