1 package sellwin.db; 2 3 import sellwin.domain.*; 4 import sellwin.utils.*; 5 6 import java.util.ArrayList ; 7 import java.sql.*; 8 9 13 18 public class QuoteLineDB extends DBType implements DBInterface { 19 20 private Connection con; 21 private final static String deleteQuery = 22 "DELETE FROM quote_line WHERE pk = "; 23 24 private final static String deleteByOppQuery = 25 "DELETE FROM quote_line WHERE quote_pk in "+ 26 "(select quote_pk from opportunity,quote "+ 27 "where opportunity.pk = quote.opp_pk AND opportunity.pk="; 28 29 private final static String deleteByQuoteQuery = 30 "DELETE FROM quote_line WHERE quote_pk="; 31 32 private final static String insertQuery = 33 "INSERT INTO quote_line (" + 34 " PK, QUOTE_PK, PRODUCT_PK, PRODUCT_NAME, "+ 35 " PRODUCT_MODEL_NO, QUANTITY, COST, UNIT_PRICE, "+ 36 " TOTAL_PRICE, PRODUCT_DESC, MODIFIED_BY, MODIFIED_DATE " + 37 ") VALUES ("; 38 39 private final static String updateQuery = 40 "UPDATE quote_line " + 41 "SET "; 42 43 private final static String selectQuery = 44 "SELECT quote_pk, product_pk,"+ 45 "quantity, "+ 46 "total_price, "+ 47 "modified_by, modified_date " + 48 "FROM quote_line "+ 49 "WHERE pk = "; 50 51 private final static String selectByQuoteQuery = 52 "SELECT pk, product_pk,"+ 53 "quantity, "+ 54 "total_price, "+ 55 "modified_by, modified_date " + 56 "FROM quote_line "+ 57 "WHERE quote_pk = "; 58 59 63 public QuoteLineDB() { 64 } 65 66 70 public QuoteLineDB(int dbType) { 71 DB_TYPE = dbType; 72 } 73 74 80 public QuoteLineDB(Connection con) { 81 this.con = con; 82 } 83 84 89 public Connection getConnection() { 90 return this.con; 91 } 92 93 98 public final void setConnection(Connection con) 99 throws SQLException { 100 101 this.con = con; 102 } 103 104 112 public final Object selectRow(Object pk) 113 throws SQLException { 114 115 QuoteLine line = new QuoteLine(); 116 line.setPK(((Long )pk).longValue()); 117 118 Statement stmt = null; 119 ResultSet rs = null; 120 String query = selectQuery + line.getPK(); 121 122 try { 123 stmt = con.createStatement(); 124 if (Prefs.DEBUG) LogWrite.write(query); 125 rs = stmt.executeQuery(query); 126 127 int i; 128 129 ProductDB prodDB = new ProductDB(DB_TYPE); 130 prodDB.setConnection(getConnection()); 131 132 while (rs.next()) { 133 i=1; 134 line.setQuotePK(rs.getLong(i)); i++; 135 line.getProduct().setPK(rs.getLong(i)); i++; 136 line.setProduct((Product)prodDB.selectRow(new Long (line.getProduct().getPK()))); 137 line.setQuantity(new Integer (rs.getInt(i))); i++; 138 line.setTotalPrice(new Double (rs.getDouble(i))); i++; 139 line.setModifiedBy(rs.getString(i)); i++; 140 line.setModifiedDate(rs.getDate(i)); 141 } 142 143 } catch (SQLException e) { 144 throw e; 145 } finally { 146 try { 147 if (rs != null) rs.close(); 148 } catch (SQLException x) { throw x; } 149 try { 150 if (stmt != null) stmt.close(); 151 } catch (SQLException x) { throw x; } 152 } 153 154 return line; 155 } 156 157 158 167 public final void updateRow(Object obj) 168 throws SQLException { 169 170 QuoteLine quote_line = (QuoteLine)obj; 171 172 StringBuffer query = new StringBuffer (updateQuery); 173 Statement stmt = con.createStatement(); 174 175 query.append("quantity="); 176 query.append(quote_line.getQuantity().intValue()).append(","); 177 query.append("modified_by="); 178 query.append(JDBC.quoteMore(quote_line.getModifiedBy())); 179 query.append("modified_date="); 180 if (DB_TYPE == Prefs.MYSQL) 181 query.append("CURRENT_DATE"); 182 else 183 query.append("SYSDATE"); 184 query.append(" WHERE pk="); 185 query.append(quote_line.getPK()); 186 187 if (Prefs.DEBUG) LogWrite.write(query.toString()); 188 int updatedRows = stmt.executeUpdate(query.toString()); 189 } 190 191 200 public final long insertRow(Object obj, boolean load) 201 throws SQLException { 202 203 QuoteLine quote_line = (QuoteLine)obj; 204 205 if (!load) 206 quote_line.setPK(DBUtils.generatePK()); 207 208 StringBuffer query = new StringBuffer (insertQuery); 209 Statement stmt = con.createStatement(); 210 211 query.append(quote_line.getPK()).append(","); 212 query.append(quote_line.getQuotePK()).append(","); 213 query.append(quote_line.getProduct().getPK()).append(","); 214 query.append(JDBC.quoteMore(quote_line.getProduct().getName())); 215 query.append(JDBC.quoteMore(quote_line.getProduct().getModelNo())); 216 query.append(quote_line.getQuantity().intValue()).append(","); 217 query.append(quote_line.getProduct().getCost().doubleValue()).append(","); 218 query.append(quote_line.getProduct().getPrice().doubleValue()).append(","); 219 query.append(quote_line.getTotalPrice().doubleValue()).append(","); 220 query.append(JDBC.quoteMore(quote_line.getProduct().getDesc())); 221 query.append(JDBC.quoteMore(quote_line.getModifiedBy())); 222 if (DB_TYPE == Prefs.MYSQL) 223 query.append("CURRENT_DATE"); 224 else 225 query.append("SYSDATE"); 226 query.append(")"); 227 228 if (Prefs.DEBUG) LogWrite.write(query.toString()); 229 int rc = stmt.executeUpdate(query.toString()); 230 231 return quote_line.getPK(); 232 } 233 234 241 public final void deleteRow(Object obj) 242 throws SQLException { 243 244 long pkValue = ((Long )obj).longValue(); 245 246 String query = deleteQuery + pkValue; 247 248 Statement stmt = null; 249 250 try { 251 stmt = con.createStatement(); 252 if (Prefs.DEBUG) LogWrite.write(query); 253 stmt.executeUpdate(query); 254 } catch (SQLException e) { 255 throw e; 256 } finally { 257 try { 258 if (stmt != null) stmt.close(); 259 } catch (SQLException x) { } 260 } 261 } 262 263 271 public final void selectByQuoteRow(long quote_pk, ArrayList lines) 272 throws SQLException { 273 274 QuoteLine ql = null; 275 276 Statement stmt = null; 277 ResultSet rs = null; 278 String query = selectByQuoteQuery + quote_pk; 279 280 try { 281 stmt = con.createStatement(); 282 if (Prefs.DEBUG) LogWrite.write(query); 283 rs = stmt.executeQuery(query); 284 285 int i; 286 ProductDB prodDB = new ProductDB(DB_TYPE); 287 prodDB.setConnection(getConnection()); 288 long productPK; 289 290 while (rs.next()) { 291 i=1; 292 ql = new QuoteLine(); 293 ql.setPK(rs.getLong(i)); i++; 294 ql.setQuotePK(quote_pk); 295 productPK = rs.getLong(i); i++; 296 ql.setProduct((Product)prodDB.selectRow(new Long (productPK))); 297 ql.getProduct().setPK(productPK); 298 ql.setQuantity(new Integer (rs.getInt(i))); i++; 299 ql.setTotalPrice(new Double (rs.getDouble(i))); i++; 300 ql.setModifiedBy(rs.getString(i)); i++; 301 ql.setModifiedDate(rs.getDate(i)); 302 lines.add(ql); 303 } 304 305 } catch (SQLException e) { 306 throw e; 307 } finally { 308 try { 309 if (rs != null) rs.close(); 310 } catch (SQLException x) { throw x; } 311 try { 312 if (stmt != null) stmt.close(); 313 } catch (SQLException x) { throw x; } 314 } 315 } 316 317 324 public final void deleteQuoteRows(long quote_pk) 325 throws SQLException { 326 327 String query = deleteByQuoteQuery + quote_pk; 328 329 Statement stmt = null; 330 331 try { 332 stmt = con.createStatement(); 333 if (Prefs.DEBUG) LogWrite.write(query); 334 stmt.executeUpdate(query); 335 } catch (SQLException e) { 336 LogWrite.write(e); 337 throw e; 338 } finally { 339 try { 340 if (stmt != null) stmt.close(); 341 } catch (SQLException x) { } 342 } 343 } 344 345 352 public final void deleteQuoteRowsByOpp(long opp_pk) 353 throws SQLException { 354 355 String query = deleteByOppQuery + opp_pk + ")"; 356 357 Statement stmt = null; 358 359 try { 360 stmt = con.createStatement(); 361 if (Prefs.DEBUG) LogWrite.write(query); 362 stmt.executeUpdate(query); 363 } catch (SQLException e) { 364 LogWrite.write(e); 365 throw e; 366 } finally { 367 try { 368 if (stmt != null) stmt.close(); 369 } catch (SQLException x) { } 370 } 371 } 372 377 public final void truncate() 378 throws SQLException { 379 380 String query = "truncate table quote_line"; 381 382 Statement stmt = null; 383 try { 384 stmt = con.createStatement(); 385 if (Prefs.DEBUG) LogWrite.write(query); 386 stmt.executeUpdate(query); 387 } catch (SQLException e) { 388 throw e; 389 } finally { 390 try { if (stmt != null) stmt.close(); 391 } catch (SQLException x) { } 392 } 393 } 394 395 } 396 | Popular Tags |