1 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 32 public class PriceList implements CacheInterface 33 { 34 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 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 if (retValue == null) 60 { 61 retValue = new PriceList (AD_Client_ID, M_PriceList_ID); 62 s_cache.add(retValue); 63 } 64 return retValue; 65 } 67 68 private static ArrayList s_cache = new ArrayList(); 69 70 public static long s_refresh; 71 72 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 } 85 86 public static final String NAME = "priceList"; 87 88 private Logger log = Logger.getLogger(getClass()); 89 90 private int[] m_parameter; 91 private String m_name = "Not found"; 92 private String m_description; 93 private String m_currency; 94 private String m_curSymbol; 95 private String m_AD_Language; 96 private boolean m_taxIncluded; 97 private int m_PriceList_ID = 0; 98 private int m_PriceList_Version_ID = 0; 99 100 private ArrayList m_prices = new ArrayList(); 101 102 106 public int reset () 107 { 108 int no = s_cache.size(); 109 s_cache.clear(); 110 return no; 111 } 113 117 private int[] getParameter() 118 { 119 return m_parameter; 120 } 122 127 private void initialize (int AD_Client_ID, int M_PriceList_ID) 128 { 129 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 getM_PriceList_Version_ID(m_PriceList_ID, new Timestamp(System.currentTimeMillis())); 138 loadProducts (null, 0); 139 } 141 142 148 private int getM_PriceList_ID (int AD_Client_ID, int M_PriceList_ID) 149 { 150 String sql = "SELECT M_PriceList_ID, pl.Name, pl.Description, pl.IsTaxIncluded," + " c.ISO_Code, c.CurSymbol, cc.AD_Language " + "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=?"; if (M_PriceList_ID != 0) 158 sql += " AND pl.M_PriceList_ID=?"; 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 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 e) 196 {} 197 pstmt = null; 198 } 199 return m_PriceList_ID; 200 } 202 208 private int getM_PriceList_Version_ID (int M_PriceList_ID, Timestamp day) 209 { 210 String sql = "SELECT plv.M_PriceList_Version_ID, plv.Name, plv.Description, plv.ValidFrom " + "FROM M_PriceList_Version plv " 212 + "WHERE plv.M_PriceList_ID=?" + " AND plv.ValidFrom <=? " + "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 } 230 rs.close(); 231 pstmt.close(); 232 pstmt = null; 233 } 234 catch (Exception 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 e) 246 {} 247 pstmt = null; 248 } 249 250 return m_PriceList_Version_ID; 251 } 253 258 private void loadProducts (String searchString, int M_Product_Category_ID) 259 { 260 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 String sql = "SELECT p.M_Product_ID, p.Value, p.Name, p.Description, " + "p.Help, p.DocumentNote, p.ImageURL, p.DescriptionURL, " + "pp.PriceStd, uom.Name, uom.UOMSymbol " + "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=?" + " AND pp.PriceStd > 0 "; 277 if (searchString != null) 278 sql += " AND p.Value||p.Name||p.Description LIKE ? "; if (M_Product_Category_ID != 0) 280 sql += " AND p.M_Product_Category_ID=? "; 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 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 e) 317 {} 318 pstmt = null; 319 } 320 log.debug("load #" + m_prices.size()); 321 } 323 324 328 public String toString() 329 { 330 StringBuffer sb = new StringBuffer ("PriceList["); 331 sb.append(m_prices.size()) 332 .append("]"); 333 return sb.toString(); 334 } 336 340 public int getPriceCount() 341 { 342 return m_prices.size(); 343 } 345 349 public ArrayList getPrices() 350 { 351 return m_prices; 352 } 354 355 356 360 public String getName() 361 { 362 return m_name; 363 } 364 public String getDescription() 365 { 366 return m_description; 367 } 368 public String getCurrency() 369 { 370 return m_currency; 371 } 372 public String getCurSymbol() 373 { 374 return m_curSymbol; 375 } 376 public String 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 } | Popular Tags |