KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > Product


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.hipergate;
34
35 import java.math.BigDecimal JavaDoc;
36
37 import java.sql.Date JavaDoc;
38 import java.sql.Connection JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.sql.CallableStatement JavaDoc;
41 import java.sql.PreparedStatement JavaDoc;
42 import java.sql.Statement JavaDoc;
43 import java.sql.ResultSet JavaDoc;
44 import java.sql.ResultSetMetaData JavaDoc;
45 import java.sql.Types JavaDoc;
46
47 import com.knowgate.debug.DebugFile;
48
49 import com.knowgate.jdc.JDCConnection;
50 import com.knowgate.dataobjs.DB;
51 import com.knowgate.dataobjs.DBPersist;
52 import com.knowgate.dataobjs.DBSubset;
53
54 import com.knowgate.misc.Gadgets;
55
56 /**
57  * Read and write products from k_products table
58  * @author Sergio Montoro Ten
59  * @version 3.0
60  */

61 public class Product extends DBPersist {
62
63   /**
64    * Create empty Product
65    */

66   public Product() {
67     super(DB.k_products, "Product");
68   }
69
70   /**
71    * Create empty Product and set gu_product
72    * @param sIdProduct GUID for Product
73    */

74   public Product(String JavaDoc sIdProduct) {
75     super(DB.k_products, "Product");
76
77     put (DB.gu_product, sIdProduct);
78   }
79
80   /**
81    * Load Product from database
82    * @param oConn Database Connection
83    * @param sIdProduct GUID of Product to be loaded
84    * @throws SQLException
85    */

86   public Product(JDCConnection oConn, String JavaDoc sIdProduct) throws SQLException JavaDoc {
87     super(DB.k_products, "Product");
88
89     Object JavaDoc aProd[] = { sIdProduct };
90
91     load(oConn, aProd);
92   } // Product
93

94   // ----------------------------------------------------------
95

96   /**
97    * Get product GUID given its Name (nm_product) and WorkArea (gu_owner)
98    * @param oConn Connection
99    * @param sProductNm String Product Name (k_products.nm_product column)
100    * @param sWorkAreaId String WorkArea GUID (k_products.gu_owner column)
101    * @return String GUID of product or <b>null</b> if no product with such name
102    * was found at given WorkArea
103    * @throws SQLException
104    * @since 3.0
105    */

106   public static String JavaDoc getIdFromName(Connection JavaDoc oConn, String JavaDoc sProductNm, String JavaDoc sWorkAreaId)
107     throws SQLException JavaDoc {
108
109     String JavaDoc sProdId;
110
111     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT "+DB.gu_product+" FROM "+DB.k_products+" WHERE "+DB.nm_product+"=? AND "+DB.gu_owner+"=?",
112                                                      ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
113     oStmt.setString(1, sProductNm);
114     oStmt.setString(2, sWorkAreaId);
115     ResultSet JavaDoc oRSet = oStmt.executeQuery();
116     if (oRSet.next())
117       sProdId = oRSet.getString(1);
118     else
119       sProdId = null;
120     oRSet.close();
121     oStmt.close();
122
123     return sProdId;
124   }
125
126   // ----------------------------------------------------------
127

128   /**
129    * Get product GUID given its Reference (id_ref) and WorkArea (gu_owner)
130    * @param oConn Connection
131    * @param sProductId String Product Reference (k_products.id_ref column)
132    * @param sWorkAreaId String WorkArea GUID (k_products.gu_owner column)
133    * @return String GUID of product or <b>null</b> if no product with such reference
134    * was found at given WorkArea
135    * @throws SQLException
136    * @since 3.0
137    */

138   public static String JavaDoc getIdFromReference(Connection JavaDoc oConn, String JavaDoc sProductId, String JavaDoc sWorkAreaId)
139     throws SQLException JavaDoc {
140
141     String JavaDoc sProdGuid;
142
143     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT "+DB.gu_product+" FROM "+DB.k_products+" WHERE "+DB.id_ref+"=? AND "+DB.gu_owner+"=?",
144                                                      ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
145     oStmt.setString(1, sProductId);
146     oStmt.setString(2, sWorkAreaId);
147     ResultSet JavaDoc oRSet = oStmt.executeQuery();
148     if (oRSet.next())
149       sProdGuid = oRSet.getString(1);
150     else
151       sProdGuid = null;
152     oRSet.close();
153     oStmt.close();
154
155     return sProdGuid;
156   }
157
158   // ----------------------------------------------------------
159

160   /**
161    * Get first fare found valid for a given date
162    * @param oConn JDCConnection
163    * @param dtWhen Date
164    * @return BigDecimal
165    * @throws SQLException
166    */

167   public BigDecimal JavaDoc getFareForDate(JDCConnection oConn, Date JavaDoc dtWhen) throws SQLException JavaDoc {
168     BigDecimal JavaDoc oFare = null;
169
170     if (DebugFile.trace) {
171       DebugFile.writeln("Begin Product.getFare([Connection],"+dtWhen.toString()+")" );
172       DebugFile.incIdent();
173     }
174
175     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) {
176       if (DebugFile.trace) {
177         DebugFile.writeln("Connection.prepareStatement(SELECT "+DB.pr_sale+" FROM "+DB.k_prod_fares+" WHERE "+DB.gu_product+"='"+getStringNull(DB.gu_product,null)+"' AND "+DB.dt_start+"<=? AND "+DB.dt_end+">=?)");
178       }
179       PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT "+DB.pr_sale+" FROM "+DB.k_prod_fares+" WHERE "+DB.gu_product+"=? AND "+DB.dt_start+"<=? AND "+DB.dt_end+">=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
180       oStmt.setString(1, getStringNull(DB.gu_product,null));
181       oStmt.setDate(2, dtWhen);
182       oStmt.setDate(3, dtWhen);
183       ResultSet JavaDoc oRSet = oStmt.executeQuery();
184       if (oRSet.next())
185         oFare = oRSet.getBigDecimal(1);
186       oRSet.close();
187       oStmt.close();
188     } else {
189       if (DebugFile.trace) {
190         DebugFile.writeln("Connection.prepareCall({ call k_sp_get_date_fare('"+getStringNull(DB.gu_product,null)+"','"+dtWhen.toString()+"',?) }");
191       }
192       CallableStatement JavaDoc oCall = oConn.prepareCall("{ call k_sp_get_date_fare(?,?,?) }");
193       oCall.setString(1, getStringNull(DB.gu_product,null));
194       oCall.setDate(2, dtWhen);
195       oCall.registerOutParameter(3, java.sql.Types.DECIMAL);
196       oCall.execute();
197       oFare = oCall.getBigDecimal(3);
198       oCall.close();
199     }
200
201     if (DebugFile.trace) {
202       DebugFile.decIdent();
203       if (null==oFare)
204         DebugFile.writeln("End Product.getFare() : null");
205       else
206         DebugFile.writeln("End Product.getFare() : "+oFare.toString());
207     }
208     return oFare;
209   }
210
211   // ----------------------------------------------------------
212

213   /**
214    * Get a given fare price for a product
215    * @param oConn JDCConnection
216    * @param sIdFare String Identifier of fare to wich price is going to be retrived
217    * @return BigDecimal Product price for the fare or <b>null</b> if fare does not exist.
218    * @throws SQLException
219    * @since v2.2
220    */

221   public BigDecimal JavaDoc getFare(JDCConnection oConn, String JavaDoc sIdFare) throws SQLException JavaDoc {
222     BigDecimal JavaDoc oFare = null;
223
224     if (DebugFile.trace) {
225       DebugFile.writeln("Begin Product.getFare([Connection],"+sIdFare+")" );
226       DebugFile.incIdent();
227     }
228
229     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) {
230       if (DebugFile.trace) {
231         DebugFile.writeln("Connection.prepareStatement(SELECT "+DB.pr_sale+" FROM "+DB.k_prod_fares+" WHERE "+DB.gu_product+"='"+getStringNull(DB.gu_product,null)+"' AND "+DB.id_fare+"='"+sIdFare+"')");
232       }
233       PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT "+DB.pr_sale+" FROM "+DB.k_prod_fares+" WHERE "+DB.gu_product+"=? AND "+DB.id_fare+"=?",ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
234       oStmt.setString(1, getStringNull(DB.gu_product,null));
235       oStmt.setString(2, sIdFare);
236       ResultSet JavaDoc oRSet = oStmt.executeQuery();
237       if (oRSet.next())
238         oFare = oRSet.getBigDecimal(1);
239       oRSet.close();
240       oStmt.close();
241     } else {
242       if (DebugFile.trace) {
243         DebugFile.writeln("Connection.prepareCall({ call k_sp_get_prod_fare('"+getStringNull(DB.gu_product,null)+"','"+sIdFare+"',?) }");
244       }
245       CallableStatement JavaDoc oCall = oConn.prepareCall("{ call k_sp_get_prod_fare(?,?,?) }");
246       oCall.setString(1, getStringNull(DB.gu_product,null));
247       oCall.setString(2, sIdFare);
248       oCall.registerOutParameter(3, java.sql.Types.DECIMAL);
249       oCall.execute();
250       oFare = oCall.getBigDecimal(3);
251       oCall.close();
252     }
253
254     if (DebugFile.trace) {
255       DebugFile.decIdent();
256       if (null==oFare)
257         DebugFile.writeln("End Product.getFare() : null");
258       else
259         DebugFile.writeln("End Product.getFare() : "+oFare.toString());
260     }
261
262     return oFare;
263   }
264
265   // ----------------------------------------------------------
266

267   /**
268    * Get product fares
269    * @param oConn JDCConnection
270    * @return DBSubset with the following columns<br>
271    * <table><tr><td>id_fare</td><td>pr_sale</td><td>id_currency</td>pct_tax_rate</td><td>is_tax_included</td><td>dt_start</td><td>dt_end</td><td>tp_fare</td></tr></table>
272    * @throws SQLException
273    */

274   public DBSubset getFares(JDCConnection oConn) throws SQLException JavaDoc {
275     DBSubset oFares = new DBSubset(DB.k_prod_fares,DB.id_fare+","+DB.pr_sale+","+
276                                    DB.id_currency+","+DB.pct_tax_rate+","+
277                                    DB.is_tax_included+","+DB.dt_start+","+DB.dt_end+","+
278                                    DB.tp_fare,DB.gu_product+"=?", 10);
279     oFares.load(oConn, new Object JavaDoc[]{getStringNull(DB.gu_product,null)});
280
281     return oFares;
282   }
283
284   // ----------------------------------------------------------
285

286   /**
287    * Get product fares of a given type
288    * @param oConn JDCConnection
289    * @return DBSubset with the following columns<br>
290    * <table><tr><td>id_fare</td><td>pr_sale</td><td>id_currency</td>pct_tax_rate</td><td>is_tax_included</td><td>dt_start</td><td>dt_end</td><td>tp_fare</td></tr></table>
291    * @throws SQLException
292    */

293   public DBSubset getFaresOfType(JDCConnection oConn, String JavaDoc sType) throws SQLException JavaDoc {
294     DBSubset oFares = new DBSubset(DB.k_prod_fares,DB.id_fare+","+DB.pr_sale+","+
295                                    DB.id_currency+","+DB.pct_tax_rate+","+
296                                    DB.is_tax_included+","+DB.dt_start+","+DB.dt_end+","+
297                                    DB.tp_fare,DB.gu_product+"=? AND "+DB.tp_fare+"=?", 10);
298     oFares.load(oConn, new Object JavaDoc[]{getStringNull(DB.gu_product,null),sType});
299     return oFares;
300   }
301
302   // ----------------------------------------------------------
303

304   /**
305    * Get product fares of a given type valid for the specified date
306    * @param oConn JDCConnection
307    * @return DBSubset with the following columns<br>
308    * <table><tr><td>id_fare</td><td>pr_sale</td><td>id_currency</td>pct_tax_rate</td><td>is_tax_included</td><td>dt_start</td><td>dt_end</td><td>tp_fare</td></tr></table>
309    * @throws SQLException
310    */

311   public DBSubset getFaresOfType(JDCConnection oConn, String JavaDoc sType, Date JavaDoc dtWhen) throws SQLException JavaDoc {
312     DBSubset oFares = new DBSubset(DB.k_prod_fares,DB.id_fare+","+DB.pr_sale+","+
313                                    DB.id_currency+","+DB.pct_tax_rate+","+
314                                    DB.is_tax_included+","+DB.dt_start+","+DB.dt_end+","+
315                                    DB.tp_fare,DB.gu_product+"=? AND "+DB.tp_fare+"=? AND "+
316                                    "("+DB.dt_start+" IS NULL OR "+DB.dt_start+"<=?) AND "+
317                                    "("+DB.dt_end+" IS NULL OR "+DB.dt_end+">=?)", 10);
318     oFares.load(oConn, new Object JavaDoc[]{getStringNull(DB.gu_product,null),sType,dtWhen,dtWhen});
319     return oFares;
320   }
321
322   // ----------------------------------------------------------
323

324   /**
325    * Get Images associated to this Product
326    * @param oConn Database Connection
327    * @return A DBSubset with all columns from k_images table for images with
328    * gu_product field is equal to this Product GUID.
329    * @throws SQLException
330    * @see com.knowgate.hipergate.Image
331    */

332   public DBSubset getImages(JDCConnection oConn) throws SQLException JavaDoc {
333     if (DebugFile.trace) {
334       DebugFile.writeln("Begin Product.getImages([Connection])" );
335       DebugFile.incIdent();
336     }
337
338     int iLoca;
339     Image oImg = new Image();
340     Object JavaDoc aProd[] = { get(DB.gu_product) };
341
342     oLocations = new DBSubset (DB.k_images, oImg.getTable(oConn).getColumnsStr(), DB.gu_product + "=?", 10);
343     iLoca = oLocations.load (oConn, aProd);
344
345     oImg = null;
346
347     if (DebugFile.trace) {
348       DebugFile.decIdent();
349       DebugFile.writeln("End Product.getImages()");
350     }
351
352     return oLocations;
353   } // getImages
354

355   // ----------------------------------------------------------
356

357   /**
358    * <p>Get Product Locations</p>
359    * Location semantics depend upon what Product is used for.<br>
360    * <ul>
361    * <li>For Downloadable Products, ProductLocations represent mirror download URLs.
362    * <li>For Versioned Products, ProductLocations represent different versions of the same File.
363    * <li>For Compound Products, ProductLocations represent parts of the Product each one being a File.
364    * <li>For Physical Products, ProductLocations represent stock of Product at different warehouses.
365    * </ul>
366    * @param oConn Database Connection
367    * @return A DBSubset with all columns from k_prod_locats for ProductLocations
368    * with gu_product is equal to this Product GUID.
369    * @throws SQLException
370    */

371   public DBSubset getLocations(JDCConnection oConn) throws SQLException JavaDoc {
372     if (DebugFile.trace) {
373       DebugFile.writeln("Begin Product.getLocations([Connection])" );
374       DebugFile.incIdent();
375     }
376     int iLoca;
377     ProductLocation oLoca = new ProductLocation();
378     Object JavaDoc aProd[] = { get(DB.gu_product) };
379
380     oLocations = new DBSubset (DB.k_prod_locats, oLoca.getTable(oConn).getColumnsStr(), DB.gu_product + "=?", 10);
381     iLoca = oLocations.load (oConn, aProd);
382
383     oLoca = null;
384
385     if (DebugFile.trace) {
386       DebugFile.decIdent();
387       DebugFile.writeln("End Product.getLocations() : " + String.valueOf(iLoca));
388     }
389
390     return oLocations;
391   } // getLocations
392

393   // ----------------------------------------------------------
394

395   /**
396    * <p>Get First ProductLocation for this Product.</p>
397    * First ProductLocation is that one find in the first place when querying
398    * to the database. Thus there is no particular criteria for what is a first
399    * ProductLocation. This method is particularly usefull when retrieving
400    * the ProductLocation for products that always have a single Productlocation.
401    * @param oConn Database Connection
402    * @return ProductLocation or <b>null</b> if no ProductLocation is found at
403    * k_prod_locats with gu_product equal to this Product GUID.
404    * @throws SQLException
405    */

406   public ProductLocation getFirstLocation(JDCConnection oConn) throws SQLException JavaDoc {
407     ResultSet JavaDoc oRSet;
408     ResultSetMetaData JavaDoc oMeta;
409     PreparedStatement JavaDoc oStmt;
410     ProductLocation oLoca;
411     Object JavaDoc oVal;
412     int iColCount;
413
414     if (DebugFile.trace) {
415       DebugFile.writeln("Begin Product.getFirstLocation()" );
416       DebugFile.incIdent();
417     }
418
419     oStmt = oConn.prepareStatement("SELECT l.* FROM " + DB.k_prod_locats + " l, " + DB.k_products + " p WHERE l." + DB.gu_product + "=p." + DB.gu_product + " AND p." + DB.gu_product + "=?", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
420
421     oStmt.setString(1,getString(DB.gu_product));
422     oRSet = oStmt.executeQuery();
423
424     if (oRSet.next()) {
425       oLoca = new ProductLocation();
426       oMeta = oRSet.getMetaData();
427       iColCount = oMeta.getColumnCount();
428
429       for (int iCol=1; iCol<=iColCount; iCol++) {
430         oVal = oRSet.getObject(iCol);
431         if (null!=oVal) oLoca.put(oMeta.getColumnName(iCol).toLowerCase(), oVal);
432       }
433     }
434     else
435       oLoca = null;
436
437     if (DebugFile.trace) {
438       DebugFile.decIdent();
439       DebugFile.writeln("End Product.getFirstLocation()");
440     }
441
442     return oLoca;
443   } // getFirstLocation
444

445   // ----------------------------------------------------------
446

447   /**
448    * <p>Store Product</p>
449    * If no gu_product is provided, this method will automatically add a new row to k_prod_attr table for th enewly created product
450    * If gu_product is null then a new GUID is automatically assigned.<br>
451    * If dt_modified is null then it is assigned to current system date.<br>
452    * If dt_uploaded is null then it is assigned to current system date.<br>
453    * If is_compound is null then it is assigned to 0.<br>
454    * If id_status is null then it is assigned to 1.<br>
455    * @param oConn Database Connection
456    * @throws SQLException
457    */

458   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
459     java.sql.Timestamp JavaDoc dtSQL = new java.sql.Timestamp JavaDoc(new java.util.Date JavaDoc().
460         getTime());
461
462     if (DebugFile.trace) {
463       DebugFile.writeln("Begin Product.store()");
464       DebugFile.incIdent();
465     }
466
467     boolean bNew = !AllVals.containsKey(DB.gu_product);
468
469     if (bNew)
470
471       put(DB.gu_product, Gadgets.generateUUID());
472
473     else if (!AllVals.containsKey(DB.dt_modified) && exists(oConn))
474
475       put (DB.dt_modified, dtSQL);
476
477     if (!AllVals.containsKey(DB.dt_uploaded))
478       put (DB.dt_uploaded, dtSQL);
479
480     if (!AllVals.containsKey(DB.is_compound))
481       put (DB.is_compound, new Short JavaDoc((short)0));
482
483     if (!AllVals.containsKey(DB.id_status))
484       put (DB.id_status, new Short JavaDoc((short)1));
485
486     boolean bRetVal = super.store(oConn);
487
488     if (bNew) {
489       DebugFile.writeln("new ProductAttribute("+getStringNull(DB.gu_product,"null")+")");
490       new ProductAttribute(getString(DB.gu_product)).store(oConn);
491     }
492
493     if (DebugFile.trace) {
494       DebugFile.decIdent();
495       DebugFile.writeln("End Product.store() : " + getString(DB.gu_product) );
496     }
497
498     return bRetVal;
499   } // store
500

501   // ----------------------------------------------------------
502

503   /**
504    * <p>Delete Product</p>
505    * Images and Productlocations are deleted first, including disk files.
506    * Then k_sp_del_product storedprocedure is called.
507    * @param oConn Database Connection
508    * @throws SQLException
509    */

510   public boolean delete(JDCConnection oConn) throws SQLException JavaDoc {
511     CallableStatement JavaDoc oStmt;
512
513     if (DebugFile.trace) {
514       DebugFile.writeln("Begin Product.delete(Connection)" );
515       DebugFile.incIdent();
516     }
517
518     try {
519       eraseImages(oConn);
520     } catch (SQLException JavaDoc sqle) { if (DebugFile.trace) DebugFile.writeln("SQLException: " + sqle.getMessage());}
521
522     // Begin SQLException
523
eraseLocations(oConn);
524
525       if (DebugFile.trace) DebugFile.writeln("Connection.prepareCall({call k_sp_del_product ('" + getStringNull(DB.gu_product,"null") + "')}");
526
527       oStmt = oConn.prepareCall("{call k_sp_del_product ('" + getString(DB.gu_product) + "')}");
528       oStmt.execute();
529       oStmt.close();
530     // End SQLException
531

532     if (DebugFile.trace) {
533       DebugFile.decIdent();
534       DebugFile.writeln("End Product.delete() : " + getString(DB.gu_product) );
535     }
536
537     return true;
538   } // delete
539

540   // ----------------------------------------------------------
541

542   /**
543    * <p>Delete Associated images</p>
544    * @param oConn Database Connection
545    * @throws SQLException
546    * @see com.knowgate.hipergate.Image#delete(JDCConnection)
547    */

548   private void eraseImages(JDCConnection oConn) throws SQLException JavaDoc {
549     if (DebugFile.trace) {
550       DebugFile.writeln("Begin Product.eraseImages(Connection)" );
551       DebugFile.incIdent();
552     }
553
554     DBSubset oImages = new DBSubset(DB.k_images, DB.gu_image + "," + DB.path_image, DB.gu_product + "=?", 10);
555     int iImgCount = oImages.load(oConn, new Object JavaDoc[]{getString(DB.gu_product)});
556     Image oImg = new Image();
557
558     for (int i=0; i<iImgCount; i++) {
559       oImg.replace(DB.gu_image, oImages.get(0,i));
560       oImg.replace(DB.path_image, oImages.get(1,i));
561       oImg.delete(oConn);
562     } // next
563

564     oImg = null;
565     oImages = null;
566
567     if (DebugFile.trace) {
568       DebugFile.decIdent();
569       DebugFile.writeln("End Product.eraseImages()");
570     }
571   } // eraseImages
572

573   // ----------------------------------------------------------
574

575   /**
576    * <p>Delete ProductLocations including disk files.</p>
577    * @param oConn Database Connection
578    * @throws SQLException
579    * @see com.knowgate.hipergate.ProductLocation#delete(JDCConnection)
580    */

581   public int eraseLocations(JDCConnection oConn) throws SQLException JavaDoc {
582
583     if (DebugFile.trace) {
584       DebugFile.writeln("Begin Product.eraseLocations(Connection)" );
585       DebugFile.incIdent();
586     }
587
588     DBSubset oLocs = getLocations(oConn);
589     int iLocs = oLocs.getRowCount();
590     ProductLocation oLoca = new ProductLocation();
591
592     for (int f=0; f<iLocs; f++) {
593       oLoca = new ProductLocation(oConn, oLocs.getString(0,f));
594       oLoca.delete(oConn);
595     } // next (f)
596

597     if (DebugFile.trace) {
598       DebugFile.decIdent();
599       DebugFile.writeln("End Product.eraseLocations() : " + oLocs.getRowCount() );
600     }
601
602     return oLocs.getRowCount();
603   } // eraseLocations()
604

605   // ----------------------------------------------------------
606

607   /**
608    * <p>Add Product to Category</p>
609    * Insert Product GUID into table k_x_cat_objs.<br>
610    * If Product already belongs to Category no error is raised.
611    * @param oConn Database Connection
612    * @param idCategory GUID of Category
613    * @param iOdPosition Position of Product into Category.
614    * Positions of products inside categories do not need to be unique,
615    * this value is only used for ordering product when displaying them.
616    * @throws SQLException
617    */

618   public int addToCategory(JDCConnection oConn, String JavaDoc idCategory, int iOdPosition) throws SQLException JavaDoc {
619     boolean bAlreadyExists;
620     int iRetVal;
621     PreparedStatement JavaDoc oStmt;
622     ResultSet JavaDoc oRSet;
623
624     oStmt = oConn.prepareStatement("SELECT NULL FROM " + DB.k_x_cat_objs + " WHERE " + DB.gu_category + "=? AND " + DB.gu_object + "=?");
625     oStmt.setString(1, idCategory);
626     oStmt.setString(2, getString(DB.gu_product));
627     oRSet = oStmt.executeQuery();
628     bAlreadyExists = oRSet.next();
629     oRSet.close();
630     oStmt.close();
631
632     if (!bAlreadyExists) {
633       oStmt = oConn.prepareStatement("INSERT INTO " + DB.k_x_cat_objs + " (" + DB.gu_category + "," + DB.gu_object + "," + DB.id_class + "," + DB.od_position + ") VALUES (?,?,?,?)");
634       oStmt.setString(1, idCategory);
635       oStmt.setString(2, getString(DB.gu_product));
636       oStmt.setInt (3, ClassId);
637       oStmt.setInt (4, iOdPosition);
638       iRetVal = oStmt.executeUpdate();
639       oStmt.close();
640     }
641     else
642       iRetVal = 0;
643
644     return iRetVal;
645   }
646
647   // ----------------------------------------------------------
648

649   /**
650    * Remove product from Category
651    * @param oConn Database Conenction
652    * @param idCategory Category GUID
653    * @throws SQLException
654    */

655   public int removeFromCategory(JDCConnection oConn, String JavaDoc idCategory) throws SQLException JavaDoc {
656     int iDeleted = 0;
657     PreparedStatement JavaDoc oStmt;
658
659     oStmt = oConn.prepareStatement("DELETE FROM " + DB.k_x_cat_objs + " WHERE " + DB.gu_category + "=? AND " + DB.gu_object + "=? AND " + DB.id_class + "=" + String.valueOf(ClassId));
660     oStmt.setString(1, idCategory);
661     oStmt.setString(2, getString(DB.gu_product));
662     iDeleted = oStmt.executeUpdate();
663     oStmt.close();
664
665     return iDeleted;
666   }
667
668   // ----------------------------------------------------------
669

670   /**
671    * get position of Product inside a Category.
672    * @param oConn Database Conenction
673    * @param sCategoryId Category GUID
674    * @return Product Position or <b>null</b> if this Product was not found inside
675    * specified Category.
676    * @throws SQLException
677    */

678   public Integer JavaDoc getPosition(JDCConnection oConn, String JavaDoc sCategoryId) throws SQLException JavaDoc {
679     Statement JavaDoc oStmt;
680     ResultSet JavaDoc oRSet;
681     CallableStatement JavaDoc oCall;
682     Object JavaDoc oPos;
683     Integer JavaDoc iPos;
684
685     if (DebugFile.trace) {
686       DebugFile.writeln("Begin Product.getPosition([Connection], " + sCategoryId + ")" );
687       DebugFile.incIdent();
688     }
689
690     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) {
691       oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
692
693       if (DebugFile.trace) DebugFile.writeln("Statement.executeQuery(SELECT k_sp_cat_obj_position('" + getStringNull(DB.gu_product, "null") + "','" + sCategoryId + "'))");
694
695       oRSet = oStmt.executeQuery("SELECT k_sp_cat_obj_position ('" + getString(DB.gu_product) + "','" + sCategoryId + "')");
696       oRSet.next();
697       oPos = new Integer JavaDoc(oRSet.getInt(1));
698       oRSet.close();
699       oStmt.close();
700     }
701     else {
702       oCall = oConn.prepareCall("{ call k_sp_cat_obj_position('" + getString(DB.gu_product) + "','" + sCategoryId + "',?)}");
703       oCall.registerOutParameter(1, Types.INTEGER);
704       oCall.execute();
705       oPos = oCall.getObject(1);
706       oCall.close();
707       oCall = null;
708     }
709
710     if (null==oPos)
711       iPos = null;
712     else
713       iPos = new Integer JavaDoc(oPos.toString());
714
715     if (DebugFile.trace) {
716       DebugFile.decIdent();
717       DebugFile.writeln("End Product.getPosition()");
718     }
719
720     return iPos;
721   } // getPosition
722

723   // ----------------------------------------------------------
724

725   /**
726    * <p>Get Sale Price for a given date.</p>
727    * This method takes into account pr_sale, pr_list, dt_start and dt_end fields.<br>
728    * There are two possible prices, <i>list price</i> or <i>sale</i> (bargain) <i>price</i>.<br>
729    * Sale price is returned if it exists at database and given date is between dt_start and dt_end.<br>
730    * Otherwise List price is returned.<br>
731    * This method does not take into account any information from k_prod_fares table.<br>
732    * Product Price is taken from k_products table following these rules:<br>
733    * <ul>
734    * <li>if dt_start AND dt_end are NULL then pr_list price is assigned.
735    * <li>if dt_start is NULL AND dt_end is NOT NULL AND pr_sale is NULL then pr_list is assigned.
736    * <li>if dt_start is NULL AND dt_end is NOT NULL AND pr_sale is NOT NULL AND dtForDate is less than or equal to dt_end then pr_sale is assigned.
737    * <li>if dt_start is NULL AND dt_end is NOT NULL AND pr_sale is NOT NULL AND dtForDate is greater than dt_end then pr_list is assigned.
738    * <li>if dt_start is NOT NULL AND dt_end is NULL AND pr_sale is NULL then pr_list is assigned.
739    * <li>if dt_start is NOT NULL AND dt_end is NULL AND pr_sale is NOT NULL AND dtForDate is greater than or equal to dt_start then pr_sale is assigned.
740    * <li>if dt_start is NOT NULL AND dt_end is NULL AND pr_sale is NOT NULL AND dtForDate is less than dt_start then pr_list is assigned.
741    * <li>if dt_start AND dt_end are NOT NULL AND pr_sale IS NULL then pr_list price is assigned.
742    * <li>if dt_start AND dt_end are NOT NULL AND pr_sale IS NOT NULL AND dtForDate is greater than or equal to dt_start AND dtForDate is less than or equal to dt_end then pr_sale is assigned.
743    * <li>if dt_start AND dt_end are NOT NULL AND pr_sale IS NOT NULL AND dtForDate is less than dt_start OR dtForDate is greater than dt_end then pr_sale is assigned.
744    * </ul>
745    * @param dtForDate Date for testing List or Sale Price.
746    * @return Price for selling the Product at a given Date.
747    */

748   public BigDecimal JavaDoc salePrice(java.util.Date JavaDoc dtForDate) {
749     java.util.Date JavaDoc dtForDateStart;
750     java.util.Date JavaDoc dtForDateEnd;
751     BigDecimal JavaDoc dRetVal;
752
753     if (DebugFile.trace) {
754       DebugFile.writeln("Begin Product.salePrice(" + dtForDate + ")" );
755       DebugFile.incIdent();
756     }
757
758     if (isNull(DB.pr_list) && isNull(DB.pr_sale))
759       dRetVal = null;
760     else {
761       if ((isNull(DB.dt_start) && isNull(DB.dt_end)) || null==dtForDate) {
762         if (isNull(DB.pr_list))
763           dRetVal = null;
764         else
765           dRetVal = getDecimal(DB.pr_list);
766       }
767       else {
768         dtForDateStart = new java.util.Date JavaDoc(dtForDate.getTime());
769         dtForDateStart.setHours(0); dtForDateStart.setMinutes(1); dtForDateStart.setSeconds(1);
770         dtForDateEnd = new java.util.Date JavaDoc(dtForDate.getTime());
771         dtForDateEnd.setHours(0); dtForDateEnd.setMinutes(1); dtForDateEnd.setSeconds(1);
772
773         if (!isNull(DB.dt_start)) {
774           if (isNull(DB.dt_end))
775             if (dtForDateStart.compareTo(getDate(DB.dt_start))>0)
776               dRetVal = getDecimal(DB.pr_sale);
777             else
778               dRetVal = getDecimal(DB.pr_list);
779           else
780             if (dtForDateStart.compareTo(getDate(DB.dt_start))>0 && dtForDateEnd.compareTo(getDate(DB.dt_end))<0)
781               dRetVal = getDecimal(DB.pr_sale);
782             else
783               dRetVal = getDecimal(DB.pr_list);
784         }
785         else {
786           if (dtForDateEnd.compareTo(getDate(DB.dt_end))<0)
787             dRetVal = getDecimal(DB.pr_list);
788           else
789             dRetVal = getDecimal(DB.pr_sale);
790         }
791       } // fi ((isNull(DB.dt_start) && isNull(DB.dt_end)) || null==dtForDate)
792
} // fi (isNull(DB.pr_list) && isNull(DB.pr_sale))
793

794     if (DebugFile.trace) {
795       DebugFile.decIdent();
796       DebugFile.writeln("End Product.salePrice() : " + dRetVal);
797     }
798
799     return dRetVal;
800   } // salePrice
801

802   // ----------------------------------------------------------
803

804   private DBSubset oLocations;
805
806   public static final short ClassId = 15;
807 } // Product
808
Popular Tags