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