| 1 package com.raptus.owxv3.modules.base; 2 3 import java.sql.*; 4 5 import com.raptus.owxv3.*; 6 7 8 9 10 11 12 public class NwslistManager extends SQLDatabaseManager 13 14 15 16 17 { 18 19 20 21 22 23 24 protected static final String tableNamebase = "nwslist"; 25 26 public NwslistManager() 27 { 28 tableID = tableNamebase; 29 initialize(); 30 } 31 32 public NwslistManager(String tableNamespace) 33 { 34 this.tableNamespace = tableNamespace; 35 if(tableNamespace != null && tableNamespace.length() > 0) 36 tableID = tableNamebase + tableNamespace; 37 else 38 tableID = tableNamebase; 39 initialize(); 40 } 41 42 public static final int ROWID = 0; 43 public static final int FLAGVISIBLE = 1; 44 public static final int FLAGENTRYDTE = 2; 45 public static final int FLAGTIMEDPUB = 3; 46 public static final int FLAGFILES = 4; 47 public static final int FLAGPICTURES = 5; 48 public static final int FLAGLINKS = 6; 49 public static final int ENTRYDTE = 7; 50 public static final int SHOWFROM = 8; 51 public static final int SHOWUNTIL = 9; 52 53 protected void initialize() 54 { 55 ALL_FIELDS = "\"rowid\"" 56 + ",\"flagvisible\"" 57 + ",\"flagentrydte\"" 58 + ",\"flagtimedpub\"" 59 + ",\"flagfiles\"" 60 + ",\"flagpictures\"" 61 + ",\"flaglinks\"" 62 + ",\"entrydte\"" 63 + ",\"showfrom\"" 64 + ",\"showuntil\""; 65 S2J_FIELD_NAMES = new String [] 66 { 67 tableID + ".\"rowid\"", 68 tableID + ".\"flagvisible\"", 69 tableID + ".\"flagentrydte\"", 70 tableID + ".\"flagtimedpub\"", 71 tableID + ".\"flagfiles\"", 72 tableID + ".\"flagpictures\"", 73 tableID + ".\"flaglinks\"", 74 tableID + ".\"entrydte\"", 75 tableID + ".\"showfrom\"", 76 tableID + ".\"showuntil\"" 77 }; 78 } 79 80 public Nwslist loadByKey(int _rowid) throws SQLException { 81 Connection _conn = null; 82 try { 83 _conn = openConnection(); 84 Nwslist _val = loadByKey(_rowid, _conn); 85 closeConnection(_conn); 86 return _val; 87 } 88 catch(SQLException e) { 89 if(_conn != null) try { closeConnection(_conn); } catch(SQLException e2) { } 90 throw e; 91 } 92 } 93 94 public Nwslist loadByKey(int _rowid, Connection _conn) throws SQLException { 95 PreparedStatement _pstmt = null; 96 try { 97 _pstmt = _conn.prepareStatement("SELECT " + ALL_FIELDS + " FROM " + tableID + " WHERE \"rowid\"=?"); 98 _pstmt.setLong(1, _rowid); 99 Nwslist _list[] = loadByPreparedStatement(_pstmt); 100 _pstmt.close(); 101 if(_list.length < 1) return null; 102 else return _list[0]; 103 } 104 catch(SQLException e) { 105 if(_pstmt != null) try { _pstmt.close(); } catch(SQLException e2) { } 106 throw e; 107 } 108 } 109 110 public Nwslist[] loadAll() throws SQLException { 111 Connection _conn = null; 112 try { 113 _conn = openConnection(); 114 Nwslist _list[] = loadAll(_conn); 115 closeConnection(_conn); 116 return _list; 117 } 118 catch(SQLException e) { 119 if(_conn != null) try { closeConnection(_conn); } catch(SQLException e2) { } 120 throw e; 121 } 122 } 123 124 public Nwslist[] loadAll(Connection _conn) throws SQLException { 125 PreparedStatement _pstmt = null; 126 try { 127 _pstmt = _conn.prepareStatement("SELECT " + ALL_FIELDS + " FROM " + tableID ); 128 Nwslist _list[] = loadByPreparedStatement(_pstmt); 129 _pstmt.close(); 130 return _list; 131 } 132 catch(SQLException e) { 133 if(_pstmt != null) try { _pstmt.close(); } catch(SQLException e2) { } 134 throw e; 135 } 136 } 137 138 public Nwslist[] loadByPreparedStatement(PreparedStatement pstmt) throws SQLException { 139 return loadByPreparedStatement(pstmt, null, 0); 140 } 141 142 public Nwslist[] loadByPreparedStatement(PreparedStatement pstmt, int maxRows) throws SQLException { 143 return loadByPreparedStatement(pstmt, null, maxRows); 144 } 145 146 public Nwslist[] loadByPreparedStatement(PreparedStatement pstmt, int[] fieldList) throws SQLException { 147 return loadByPreparedStatement(pstmt, fieldList, 0); 148 } 149 150 public Nwslist[] loadByPreparedStatement(PreparedStatement pstmt, int[] fieldList, int maxRows) throws SQLException { 151 ResultSet rs = null; 152 try { 153 rs = pstmt.executeQuery(); 154 java.util.Vector v = new java.util.Vector (); 155 while(rs.next() && (maxRows == 0 || v.size() < maxRows)) { 156 if(fieldList == null) v.addElement(decodeRow(rs)); 157 else v.addElement(decodeRow(rs, fieldList)); 158 } 159 rs.close(); 160 Nwslist list[] = new Nwslist[v.size()]; 161 v.copyInto(list); 162 return list; 163 } 164 catch(SQLException e) { 165 if(rs!=null) try { rs.close();} catch(Exception e2) {} 166 throw e; 167 } 168 } 169 170 public Nwslist[] loadByWhere(String where) throws SQLException { 171 return loadByWhere(where, (int[])null, 0, (String )null); 172 } 173 public Nwslist[] loadByWhere(String where, int maxRows) throws SQLException { 174 return loadByWhere(where, (int[])null, maxRows, (String )null); 175 } 176 public Nwslist[] loadByWhere(String where, String orderby, int maxRows) throws SQLException { 177 return loadByWhere(where, (int[])null, maxRows, orderby); 178 } 179 public Nwslist[] loadByWhere(String where, int[] fieldList) throws SQLException { 180 return loadByWhere(where, fieldList, 0, (String )null); 181 } 182 public Nwslist[] loadByWhere(String where, int[] fieldList, int maxRows, String orderby) throws SQLException { 183 Connection conn = null; 184 try { 185 conn = openConnection(); 186 Nwslist _list[] = loadByWhere(where, conn, fieldList, maxRows, orderby); 187 closeConnection(conn); 188 return _list; 189 } 190 catch(SQLException e) { 191 if(conn!=null) try { closeConnection(conn);} catch(Exception e2) {} 192 throw e; 193 } 194 } 195 196 public Nwslist[] loadByWhere(String where, Connection conn) throws SQLException { 197 return loadByWhere(where, conn, (int[])null, 0, (String )null); 198 } 199 200 public Nwslist[] loadByWhere(String where, Connection conn, int[] fieldList) throws SQLException { 201 return loadByWhere(where, conn, fieldList, 0, (String )null); 202 } 203 204 public Nwslist[] loadByWhere(String where, Connection conn, int[] fieldList, int maxRows, String orderby) 205 throws SQLException { 206 String sql = null; 207 if(fieldList == null) 208 { 209 sql = "select " + ALL_FIELDS + " from " + tableID + " where (" + where + ")"; 210 if(orderby != null) sql += " order by " + orderby; 211 } 212 else 213 { 214 StringBuffer buff = new StringBuffer (128); 215 buff.append("select "); 216 for(int i = 0; i < fieldList.length; i++) 217 { 218 if(i != 0) buff.append(","); 219 buff.append(S2J_FIELD_NAMES[fieldList[i]]); 220 } 221 buff.append(" from " + tableID + " "); 222 buff.append(" where (" + where + ")"); 223 if(orderby != null) 224 buff.append(" order by " + orderby); 225 sql = buff.toString(); 226 } 227 Statement stmt = null; 228 ResultSet rs = null; 229 try { 230 stmt = conn.createStatement(); 231 rs = stmt.executeQuery(sql); 232 java.util.Vector v = new java.util.Vector (); 233 while(rs.next() && (maxRows == 0 || v.size() < maxRows)) { 234 if(fieldList == null) v.addElement(decodeRow(rs)); 235 else v.addElement(decodeRow(rs, fieldList)); 236 } 237 rs.close(); 238 stmt.close(); 239 240 Nwslist _list[] = new Nwslist[v.size()]; 241 v.copyInto(_list); 242 return _list; 243 } 244 catch(SQLException e) { 245 if(rs!=null) try { rs.close();} catch(Exception e2) {} 246 if(stmt!=null) try { stmt.close();} catch(Exception e2) {} 247 throw e; 248 } 249 } 250 251 public int deleteByKey(int _rowid) throws SQLException { 252 Connection _conn = null; 253 try { 254 _conn = openConnection(); 255 int _rows = deleteByKey(_rowid, _conn); 256 closeConnection(_conn); 257 return _rows; 258 } 259 catch(SQLException e) { 260 if(_conn!=null) try { closeConnection(_conn);} catch(Exception e2) {} 261 throw e; 262 } 263 } 264 265 public int deleteByKey(int _rowid, Connection _conn) throws SQLException { 266 PreparedStatement _pstmt = null; 267 try { 268 _pstmt = _conn.prepareStatement("DELETE from " + tableID + " WHERE \"rowid\"=?"); 269 _pstmt.setLong(1, _rowid); 270 int _rows = _pstmt.executeUpdate(); 271 _pstmt.close(); 272 return _rows; 273 } 274 catch(SQLException e) { 275 if(_pstmt!=null) try { _pstmt.close();} catch(Exception e2) {} 276 throw e; 277 } 278 } 279 280 public void save(Nwslist obj) throws SQLException { 281 if(!obj.isModifiedS2J()) return; 282 Connection _conn = null; 283 try { 284 _conn = openConnection(); 285 save(obj, _conn); 286 closeConnection(_conn); 287 } 288 catch(SQLException e) { 289 if(_conn!=null) try { closeConnection(_conn);} catch(Exception e2) {} 290 throw e; 291 } 292 } 293 294 public void save(Nwslist obj, Connection _conn) throws SQLException { 295 PreparedStatement _pstmt = null; 296 try { 297 if(obj.isNew()) { 298 int _dirtyCount = 0; 299 StringBuffer _sql = new StringBuffer ("INSERT into " + tableID + " ("); 300 if(obj.rowidIsModifiedS2j()) { _sql.append("\"rowid\"").append(","); _dirtyCount++; } 301 if(obj.flagvisibleIsModifiedS2j()) { _sql.append("\"flagvisible\"").append(","); _dirtyCount++; } 302 if(obj.flagentrydteIsModifiedS2j()) { _sql.append("\"flagentrydte\"").append(","); _dirtyCount++; } 303 if(obj.flagtimedpubIsModifiedS2j()) { _sql.append("\"flagtimedpub\"").append(","); _dirtyCount++; } 304 if(obj.flagfilesIsModifiedS2j()) { _sql.append("\"flagfiles\"").append(","); _dirtyCount++; } 305 if(obj.flagpicturesIsModifiedS2j()) { _sql.append("\"flagpictures\"").append(","); _dirtyCount++; } 306 if(obj.flaglinksIsModifiedS2j()) { _sql.append("\"flaglinks\"").append(","); _dirtyCount++; } 307 if(obj.entrydteIsModifiedS2j()) { _sql.append("\"entrydte\"").append(","); _dirtyCount++; } 308 if(obj.showfromIsModifiedS2j()) { _sql.append("\"showfrom\"").append(","); _dirtyCount++; } 309 if(obj.showuntilIsModifiedS2j()) { _sql.append("\"showuntil\"").append(","); _dirtyCount++; } 310 _sql.setLength(_sql.length() - 1); 311 _sql.append(") values ("); 312 for(int i = 0; i < _dirtyCount; i++) _sql.append("?,"); 313 _sql.setLength(_sql.length() - 1); 314 _sql.append(")"); 315 316 _pstmt = _conn.prepareStatement(_sql.toString()); 317 _dirtyCount = 0; 318 if(obj.rowidIsModifiedS2j()) { _pstmt.setLong(++_dirtyCount, obj.getRowid()); } 319 if(obj.flagvisibleIsModifiedS2j()) { _pstmt.setBoolean(++_dirtyCount, obj.getFlagvisible()); } 320 if(obj.flagentrydteIsModifiedS2j()) { _pstmt.setBoolean(++_dirtyCount, obj.getFlagentrydte()); } 321 if(obj.flagtimedpubIsModifiedS2j()) { _pstmt.setBoolean(++_dirtyCount, obj.getFlagtimedpub()); } 322 if(obj.flagfilesIsModifiedS2j()) { _pstmt.setBoolean(++_dirtyCount, obj.getFlagfiles()); } 323 if(obj.flagpicturesIsModifiedS2j()) { _pstmt.setBoolean(++_dirtyCount, obj.getFlagpictures()); } 324 if(obj.flaglinksIsModifiedS2j()) { _pstmt.setBoolean(++_dirtyCount, obj.getFlaglinks()); } 325 if(obj.entrydteIsModifiedS2j()) { if(obj.getEntrydte() == null) { _pstmt.setNull(++_dirtyCount, 93); } else { _pstmt.setTimestamp(++_dirtyCount, new java.sql.Timestamp (obj.getEntrydte().getTime())); } } 326 if(obj.showfromIsModifiedS2j()) { if(obj.getShowfrom() == null) { _pstmt.setNull(++_dirtyCount, 93); } else { _pstmt.setTimestamp(++_dirtyCount, new java.sql.Timestamp (obj.getShowfrom().getTime())); } } 327 if(obj.showuntilIsModifiedS2j()) { if(obj.getShowuntil() == null) { _pstmt.setNull(++_dirtyCount, 93); } else { _pstmt.setTimestamp(++_dirtyCount, new java.sql.Timestamp (obj.getShowuntil().getTime())); } } 328 _pstmt.executeUpdate(); 329 _pstmt.close(); 330 _pstmt = _conn.prepareStatement("SELECT currval('" + tableID + "_ROWID_seq')"); 331 ResultSet _rs = _pstmt.executeQuery(); 332 if(_rs.next()) obj.setRowid(_rs.getInt(1)); 333 _rs.close(); 334 obj.setIsNew(false); 335 obj.resetIsModifiedS2J(); 336 } 337 else { 338 StringBuffer _sql = new StringBuffer ("UPDATE " + tableID + " SET "); 339 if(obj.rowidIsModifiedS2j()) { _sql.append("\"rowid\"").append("=?,"); } 340 if(obj.flagvisibleIsModifiedS2j()) { _sql.append("\"flagvisible\"").append("=?,"); } 341 if(obj.flagentrydteIsModifiedS2j()) { _sql.append("\"flagentrydte\"").append("=?,"); } 342 if(obj.flagtimedpubIsModifiedS2j()) { _sql.append("\"flagtimedpub\"").append("=?,"); } 343 if(obj.flagfilesIsModifiedS2j()) { _sql.append("\"flagfiles\"").append("=?,"); } 344 if(obj.flagpicturesIsModifiedS2j()) { _sql.append("\"flagpictures\"").append("=?,"); } 345 if(obj.flaglinksIsModifiedS2j()) { _sql.append("\"flaglinks\"").append("=?,"); } 346 if(obj.entrydteIsModifiedS2j()) { _sql.append("\"entrydte\"").append("=?,"); } 347 if(obj.showfromIsModifiedS2j()) { _sql.append("\"showfrom\"").append("=?,"); } 348 if(obj.showuntilIsModifiedS2j()) { _sql.append("\"showuntil\"").append("=?,"); } 349 _sql.setLength(_sql.length() - 1); 350 _sql.append(" WHERE "); 351 _sql.append("\"rowid\"=?"); 352 _pstmt = _conn.prepareStatement(_sql.toString()); 353 int _dirtyCount = 0; 354 if(obj.rowidIsModifiedS2j()) { _pstmt.setLong(++_dirtyCount, obj.getRowid()); } 355 if(obj.flagvisibleIsModifiedS2j()) { _pstmt.setBoolean(++_dirtyCount, obj.getFlagvisible()); } 356 if(obj.flagentrydteIsModifiedS2j()) { _pstmt.setBoolean(++_dirtyCount, obj.getFlagentrydte()); } 357 if(obj.flagtimedpubIsModifiedS2j()) { _pstmt.setBoolean(++_dirtyCount, obj.getFlagtimedpub()); } 358 if(obj.flagfilesIsModifiedS2j()) { _pstmt.setBoolean(++_dirtyCount, obj.getFlagfiles()); } 359 if(obj.flagpicturesIsModifiedS2j()) { _pstmt.setBoolean(++_dirtyCount, obj.getFlagpictures()); } 360 if(obj.flaglinksIsModifiedS2j()) { _pstmt.setBoolean(++_dirtyCount, obj.getFlaglinks()); } 361 if(obj.entrydteIsModifiedS2j()) { if(obj.getEntrydte() == null) { _pstmt.setNull(++_dirtyCount, 93); } else { _pstmt.setTimestamp(++_dirtyCount, new java.sql.Timestamp (obj.getEntrydte().getTime())); } } 362 if(obj.showfromIsModifiedS2j()) { if(obj.getShowfrom() == null) { _pstmt.setNull(++_dirtyCount, 93); } else { _pstmt.setTimestamp(++_dirtyCount, new java.sql.Timestamp (obj.getShowfrom().getTime())); } } 363 if(obj.showuntilIsModifiedS2j()) { if(obj.getShowuntil() == null) { _pstmt.setNull(++_dirtyCount, 93); } else { _pstmt.setTimestamp(++_dirtyCount, new java.sql.Timestamp (obj.getShowuntil().getTime())); } } 364 _pstmt.setLong(++_dirtyCount, obj.getRowid()); 365 _pstmt.executeUpdate(); 366 obj.resetIsModifiedS2J(); 367 } 368 _pstmt.close(); 369 } 370 catch(SQLException e) { 371 if(_pstmt!=null) try { _pstmt.close();} catch(Exception e2) {} 372 throw e; 373 } 374 } 375 376 private Nwslist decodeRow(ResultSet rs) throws SQLException { 377 Nwslist obj = new Nwslist(); 378 obj.setRowid(rs.getInt(1)); 379 obj.setFlagvisible(rs.getBoolean(2)); 380 obj.setFlagentrydte(rs.getBoolean(3)); 381 obj.setFlagtimedpub(rs.getBoolean(4)); 382 obj.setFlagfiles(rs.getBoolean(5)); 383 obj.setFlagpictures(rs.getBoolean(6)); 384 obj.setFlaglinks(rs.getBoolean(7)); 385 obj.setEntrydte(rs.getTimestamp(8)); 386 obj.setShowfrom(rs.getTimestamp(9)); 387 obj.setShowuntil(rs.getTimestamp(10)); 388 obj.setIsNew(false); 389 obj.resetIsModifiedS2J(); 390 391 return obj; 392 } 393 394 private Nwslist decodeRow(ResultSet rs, int[] fieldList) throws SQLException { 395 Nwslist obj = new Nwslist(); 396 int pos = 0; 397 for(int i = 0; i < fieldList.length; i++) { 398 switch(fieldList[i]) { 399 case ROWID: obj.setRowid(rs.getInt(++pos)); 400 break; 401 case FLAGVISIBLE: obj.setFlagvisible(rs.getBoolean(++pos)); 402 break; 403 case FLAGENTRYDTE: obj.setFlagentrydte(rs.getBoolean(++pos)); 404 break; 405 case FLAGTIMEDPUB: obj.setFlagtimedpub(rs.getBoolean(++pos)); 406 break; 407 case FLAGFILES: obj.setFlagfiles(rs.getBoolean(++pos)); 408 break; 409 case FLAGPICTURES: obj.setFlagpictures(rs.getBoolean(++pos)); 410 break; 411 case FLAGLINKS: obj.setFlaglinks(rs.getBoolean(++pos)); 412 break; 413 case ENTRYDTE: obj.setEntrydte(rs.getTimestamp(++pos)); 414 break; 415 case SHOWFROM: obj.setShowfrom(rs.getTimestamp(++pos)); 416 break; 417 case SHOWUNTIL: obj.setShowuntil(rs.getTimestamp(++pos)); 418 break; 419 } 420 } 421 obj.setIsNew(false); 422 obj.resetIsModifiedS2J(); 423 424 return obj; 425 } 426 427 } 428 | Popular Tags |