KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > wstore > PriceList


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.wstore;
15
16 import java.util.*;
17 import java.sql.*;
18
19 import org.apache.log4j.Logger;
20
21 import org.compiere.model.*;
22 import org.compiere.util.*;
23 import org.compiere.www.*;
24
25 /**
26  * Price List.
27  * ArrayList of PriceListProduct
28  *
29  * @author Jorg Janke
30  * @version $Id: PriceList.java,v 1.10 2003/09/05 04:55:42 jjanke Exp $
31  */

32 public class PriceList implements CacheInterface
33 {
34     /**
35      * Get Price List
36      * @param AD_Client_ID client
37      * @param M_PriceList_ID price list
38      * @return Price list
39      */

40     public static PriceList get (int AD_Client_ID, int M_PriceList_ID)
41     {
42         PriceList retValue = null;
43         if (System.currentTimeMillis() > s_refresh)
44         {
45             s_cache.clear ();
46             s_refresh = System.currentTimeMillis () + JSPEnv.REFRESH_MS;
47         }
48         // search cache
49
for (int i = 0; i < s_cache.size(); i++)
50         {
51             PriceList pl = (PriceList)s_cache.get(i);
52             if (pl.getParameter()[0] == AD_Client_ID && pl.getParameter()[1] == M_PriceList_ID)
53             {
54                 retValue = pl;
55                 break;
56             }
57         }
58         // create New
59
if (retValue == null)
60         {
61             retValue = new PriceList (AD_Client_ID, M_PriceList_ID);
62             s_cache.add(retValue);
63         }
64         return retValue;
65     } // get
66

67     /** Price List Cache */
68     private static ArrayList s_cache = new ArrayList();
69     /** Next Refresh */
70     public static long s_refresh;
71
72     /**
73      * PriceList constructor
74      * @param AD_Client_ID client
75      * @param M_PriceList_ID optional price list
76      */

77     private PriceList (int AD_Client_ID, int M_PriceList_ID)
78     {
79         log.info("AD_Client_ID=" + AD_Client_ID + ", M_PriceList_ID=" + M_PriceList_ID);
80         CacheMgt.get().register(this);
81         m_parameter = new int[] {AD_Client_ID, M_PriceList_ID};
82         initialize (AD_Client_ID, M_PriceList_ID);
83     } // PriceList
84

85     /** Attribute Name - also in JSPs */
86     public static final String JavaDoc NAME = "priceList";
87     /** Logging */
88     private Logger log = Logger.getLogger(getClass());
89
90     private int[] m_parameter;
91     private String JavaDoc m_name = "Not found";
92     private String JavaDoc m_description;
93     private String JavaDoc m_currency;
94     private String JavaDoc m_curSymbol;
95     private String JavaDoc m_AD_Language;
96     private boolean m_taxIncluded;
97     private int m_PriceList_ID = 0;
98     private int m_PriceList_Version_ID = 0;
99     /** Price Lines */
100     private ArrayList m_prices = new ArrayList();
101
102     /**
103      * Clear cache
104      * @return no of cached entry
105      */

106     public int reset ()
107     {
108         int no = s_cache.size();
109         s_cache.clear();
110         return no;
111     } // clearCache
112

113     /**
114      * Get Parameter
115      * @return Parameter
116      */

117     private int[] getParameter()
118     {
119         return m_parameter;
120     } // getParameter
121

122     /**
123      * Find Price List
124      * @param AD_Client_ID client
125      * @param M_PriceList_ID optional price list
126      */

127     private void initialize (int AD_Client_ID, int M_PriceList_ID)
128     {
129         // Get Price List
130
if (getM_PriceList_ID (AD_Client_ID, M_PriceList_ID) == 0)
131             if (getM_PriceList_ID(AD_Client_ID, 0) == 0)
132                  return;
133
134         log.debug("initialize - AD_Client_ID=" + AD_Client_ID + ", M_PriceList_ID=" + m_PriceList_ID);
135
136         // Get Price List Version
137
getM_PriceList_Version_ID(m_PriceList_ID, new Timestamp(System.currentTimeMillis()));
138         loadProducts (null, 0);
139     } // initialize
140

141
142     /**
143      * Find Price List
144      * @param AD_Client_ID client
145      * @param M_PriceList_ID optional price list
146      * @return M_PriceList_ID
147      */

148     private int getM_PriceList_ID (int AD_Client_ID, int M_PriceList_ID)
149     {
150         String JavaDoc sql = "SELECT M_PriceList_ID, pl.Name, pl.Description, pl.IsTaxIncluded," // 1..4
151
+ " c.ISO_Code, c.CurSymbol, cc.AD_Language " // 5..7
152
+ "FROM M_PriceList pl"
153             + " INNER JOIN C_Currency c ON (pl.C_Currency_ID=c.C_Currency_ID)"
154             + " LEFT OUTER JOIN C_Country cc ON (c.C_Currency_ID=cc.C_Currency_ID AND ROWNUM=1) "
155             + "WHERE pl.IsActive='Y'"
156             + " AND pl.AD_Client_ID=?"; // #1
157
if (M_PriceList_ID != 0)
158             sql += " AND pl.M_PriceList_ID=?"; // #2
159
else
160             sql += " ORDER BY pl.IsDefault DESC";
161         m_PriceList_ID = 0;
162         PreparedStatement pstmt = null;
163         try
164         {
165             pstmt = DB.prepareStatement(sql);
166             pstmt.setInt(1, AD_Client_ID);
167             if (M_PriceList_ID != 0)
168                 pstmt.setInt(2, M_PriceList_ID);
169             ResultSet rs = pstmt.executeQuery();
170             if (rs.next())
171             {
172                 m_PriceList_ID = rs.getInt(1);
173                 m_name = rs.getString(2);
174                 m_description = rs.getString(3);
175                 m_taxIncluded = "Y".equals(rs.getString(4));
176                 m_currency = rs.getString(5);
177                 m_curSymbol = rs.getString(6);
178                 m_AD_Language = rs.getString(7);
179             }
180             rs.close();
181             pstmt.close();
182             pstmt = null;
183         }
184         catch (Exception JavaDoc e)
185         {
186             log.error("getM_PriceList_ID", e);
187         }
188         finally
189         {
190             try
191             {
192                 if (pstmt != null)
193                     pstmt.close ();
194             }
195             catch (Exception JavaDoc e)
196             {}
197             pstmt = null;
198         }
199         return m_PriceList_ID;
200     } // getM_PriceList_ID
201

202     /**
203      * Get PL Version
204      * @param M_PriceList_ID price list
205      * @param day valid day
206      * @return M_PriceList_Version_ID
207      */

208     private int getM_PriceList_Version_ID (int M_PriceList_ID, Timestamp day)
209     {
210         String JavaDoc sql = "SELECT plv.M_PriceList_Version_ID, plv.Name, plv.Description, plv.ValidFrom " // 1..4
211
+ "FROM M_PriceList_Version plv "
212             + "WHERE plv.M_PriceList_ID=?" // #1
213
+ " AND plv.ValidFrom <=? " // #2
214
+ "ORDER BY plv.ValidFrom DESC";
215         PreparedStatement pstmt = null;
216         m_PriceList_Version_ID = 0;
217         try
218         {
219             pstmt = DB.prepareStatement(sql);
220             pstmt.setInt(1, M_PriceList_ID);
221             pstmt.setTimestamp(2, day);
222             ResultSet rs = pstmt.executeQuery();
223             if (rs.next())
224             {
225                 m_PriceList_Version_ID = rs.getInt(1);
226                 m_name = rs.getString(2);
227                 m_description = rs.getString(3);
228             // m_validFrom = rs.getTimestamp(4);
229
}
230             rs.close();
231             pstmt.close();
232             pstmt = null;
233         }
234         catch (Exception JavaDoc e)
235         {
236             log.error("getM_PriceList_Version_ID", e);
237         }
238         finally
239         {
240             try
241             {
242                 if (pstmt != null)
243                     pstmt.close ();
244             }
245             catch (Exception JavaDoc e)
246             {}
247             pstmt = null;
248         }
249
250         return m_PriceList_Version_ID;
251     } // getM_PriceList_Version_ID
252

253     /**
254      * Load From Product Price
255      * @param searchString optional search string
256      * @param M_Product_Category_ID optional product category
257      */

258     private void loadProducts (String JavaDoc searchString, int M_Product_Category_ID)
259     {
260         // Set Search String
261
if (searchString != null && (searchString.length() == 0 || searchString.equals("%")))
262             searchString = null;
263         if (searchString != null && searchString.indexOf("%") == -1)
264             searchString += "%";
265         log.debug("loadProducts - M_PriceList_Version_ID=" + m_PriceList_Version_ID
266             + ", SearchString=" + searchString + ", M_Product_Category_ID=" + M_Product_Category_ID);
267         m_prices.clear();
268         //
269
String JavaDoc sql = "SELECT p.M_Product_ID, p.Value, p.Name, p.Description, " // 1..4
270
+ "p.Help, p.DocumentNote, p.ImageURL, p.DescriptionURL, " // 5..8
271
+ "pp.PriceStd, uom.Name, uom.UOMSymbol " // 9..11
272
+ "FROM M_ProductPrice pp "
273             + " INNER JOIN M_Product p ON (pp.M_Product_ID=p.M_Product_ID AND p.IsActive='Y' AND p.IsSold='Y')"
274             + " INNER JOIN C_UOM uom ON (p.C_UOM_ID=uom.C_UOM_ID) "
275             + "WHERE M_PriceList_Version_ID=?" // #1
276
+ " AND pp.PriceStd > 0 ";
277         if (searchString != null)
278             sql += " AND p.Value||p.Name||p.Description LIKE ? "; // #2
279
if (M_Product_Category_ID != 0)
280             sql += " AND p.M_Product_Category_ID=? "; // #3
281
sql += "ORDER BY p.M_Product_Category_ID, p.Value";
282
283         PreparedStatement pstmt = null;
284         try
285         {
286             pstmt = DB.prepareStatement(sql);
287             int index = 1;
288             pstmt.setInt(index++, m_PriceList_Version_ID);
289             if (searchString != null)
290                 pstmt.setString(index++, searchString);
291             if (M_Product_Category_ID != 0)
292                 pstmt.setInt(index++, M_Product_Category_ID);
293             ResultSet rs = pstmt.executeQuery();
294             while (rs.next())
295             {
296                 m_prices.add (new PriceListProduct(
297                         rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4),
298                         rs.getString(5), rs.getString(6), rs.getString(7), rs.getString(8),
299                         rs.getBigDecimal(9), rs.getString(10), rs.getString(11) ));
300             }
301             rs.close();
302             pstmt.close();
303             pstmt = null;
304         }
305         catch (Exception JavaDoc e)
306         {
307             log.error("load", e);
308         }
309         finally
310         {
311             try
312             {
313                 if (pstmt != null)
314                     pstmt.close ();
315             }
316             catch (Exception JavaDoc e)
317             {}
318             pstmt = null;
319         }
320         log.debug("load #" + m_prices.size());
321     } // load
322

323
324     /**
325      * String Representation
326      * @return info
327      */

328     public String JavaDoc toString()
329     {
330         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("PriceList[");
331         sb.append(m_prices.size())
332             .append("]");
333         return sb.toString();
334     } // toString
335

336     /**
337      * Get Count
338      * @return size
339      */

340     public int getPriceCount()
341     {
342         return m_prices.size();
343     } // getPriceCount
344

345     /**
346      * Get Prices
347      * @return Price Array List
348      */

349     public ArrayList getPrices()
350     {
351         return m_prices;
352     } // getPrices
353

354     /*************************************************************************/
355
356     /**
357      * Get Name
358      * @return Price List Name
359      */

360     public String JavaDoc getName()
361     {
362         return m_name;
363     }
364     public String JavaDoc getDescription()
365     {
366         return m_description;
367     }
368     public String JavaDoc getCurrency()
369     {
370         return m_currency;
371     }
372     public String JavaDoc getCurSymbol()
373     {
374         return m_curSymbol;
375     }
376     public String JavaDoc getAD_Language()
377     {
378         return m_AD_Language;
379     }
380     public boolean isTaxIncluded()
381     {
382         return m_taxIncluded;
383     }
384     public int getPriceList_ID()
385     {
386         return m_PriceList_ID;
387     }
388     public int getPriceList_Version_ID()
389     {
390         return m_PriceList_Version_ID;
391     }
392
393 } // PriceList
394
Popular Tags