1 14 package org.compiere.model; 15 16 import java.sql.*; 17 import java.util.*; 18 import java.math.*; 19 import java.io.Serializable ; 20 21 import org.compiere.util.NamePair; 22 import org.compiere.util.KeyNamePair; 23 import org.compiere.util.CCache; 24 import org.compiere.util.DB; 25 import org.compiere.util.Access; 26 27 34 public final class MLocatorLookup extends Lookup implements Serializable  35 { 36 41 public MLocatorLookup(Properties ctx, int WindowNo) 42 { 43 m_ctx = ctx; 44 m_WindowNo = WindowNo; 45 m_loader = new Loader(); 47 m_loader.start(); 48 } 50 private Properties m_ctx; 51 private int m_WindowNo; 52 protected int C_Locator_ID; 53 private Loader m_loader; 54 55 56 private int m_only_Warehouse_ID = 0; 57 58 59 private volatile LinkedHashMap m_lookup = new LinkedHashMap(); 60 private static int s_maxRows = 100; 62 65 public void dispose() 66 { 67 log.debug("dispose " + C_Locator_ID); 68 if (m_loader != null) 69 { 70 while (m_loader.isAlive()) 71 m_loader.interrupt(); 72 } 73 m_loader = null; 74 if (m_lookup != null) 75 m_lookup.clear(); 76 m_lookup = null; 77 super.dispose(); 79 } 81 85 public void setOnly_Warehouse_ID (int only_Warehouse_ID) 86 { 87 m_only_Warehouse_ID = only_Warehouse_ID; 88 } 90 94 public int getOnly_Warehouse_ID() 95 { 96 return m_only_Warehouse_ID; 97 } 99 102 public void loadComplete() 103 { 104 if (m_loader != null) 105 { 106 try 107 { 108 m_loader.join(); 109 } 110 catch (InterruptedException ie) 111 { 112 log.error("loadComplete - join interrupted", ie); 113 } 114 } 115 } 117 122 public NamePair get (Object key) 123 { 124 if (key == null) 125 return null; 126 127 MLocator loc = (MLocator) m_lookup.get(key); 129 if (loc != null) 130 return new KeyNamePair (loc.getM_Locator_ID(), loc.toString()); 131 132 if (m_loader.isAlive()) 134 { 135 log.debug("get - waiting for Loader"); 136 loadComplete(); 137 loc = (MLocator) m_lookup.get(key); 139 } 140 if (loc != null) 141 return new KeyNamePair (loc.getM_Locator_ID(), loc.toString()); 142 143 return getDirect(key, true); 145 } 147 152 public String getDisplay (Object value) 153 { 154 if (value == null) 155 return ""; 156 NamePair display = get (value); 158 if (display == null) 159 return "<" + value.toString() + ">"; 160 return display.toString(); 161 } 163 168 public boolean containsKey (Object key) 169 { 170 return m_lookup.containsKey(key); 171 } 173 179 public NamePair getDirect (Object keyValue, boolean saveInCache) 180 { 181 MLocator loc = getMLocator (keyValue); 182 if (loc == null) 183 return null; 184 int key = loc.getM_Locator_ID(); 186 if (saveInCache) 187 m_lookup.put(new Integer (key), loc); 188 NamePair retValue = new KeyNamePair(key, loc.toString()); 189 return retValue; 190 } 192 197 public MLocator getMLocator (Object keyValue) 198 { 199 int M_Locator_ID = -1; 201 try 202 { 203 M_Locator_ID = Integer.parseInt(keyValue.toString()); 204 } 205 catch (Exception e) 206 {} 207 if (M_Locator_ID == -1) 208 { 209 log.error("getMLocator - invalid key=" + keyValue); 210 return null; 211 } 212 return new MLocator (m_ctx, M_Locator_ID); 214 } 216 219 public String toString() 220 { 221 return "MLocatorLookup[Size=" + m_lookup.size() + "]"; 222 } 224 225 230 public boolean isValid (Object key) 231 { 232 if (key == null) 233 return true; 234 MLocator loc = (MLocator) m_lookup.get(key); 236 if (loc == null) 237 loc = getMLocator(key); 238 return isValid(loc); 239 } 241 246 public boolean isValid (MLocator locator) 247 { 248 if (locator == null || m_only_Warehouse_ID == 0) 249 return true; 250 return m_only_Warehouse_ID == locator.getM_Warehouse_ID(); 251 } 253 254 257 class Loader extends Thread implements Serializable  258 { 259 public Loader() 260 { 261 super("MLocatorLookup"); 262 } 264 267 public void run() 268 { 269 StringBuffer sql = new StringBuffer ("SELECT * FROM M_Locator ") 272 .append(" WHERE IsActive='Y'"); 273 if (m_only_Warehouse_ID != 0) 274 sql.append("AND M_Warehouse_ID=").append(m_only_Warehouse_ID); 275 String finalSql = MRole.getDefault(m_ctx, false).addAccessSQL( 276 sql.toString(), "M_Locator", MRole.SQL_NOTQUALIFIED, MRole.SQL_RO); 277 if (isInterrupted()) 278 { 279 log.error("Loader.run interrupted"); 280 return; 281 } 282 283 m_lookup.clear(); 285 int rows = 0; 286 try 287 { 288 PreparedStatement pstmt = DB.prepareStatement(finalSql); 289 ResultSet rs = pstmt.executeQuery(); 290 291 while (rs.next() && rows++ < s_maxRows) 293 { 294 MLocator loc = new MLocator(m_ctx, rs); 295 int M_Locator_ID = loc.getM_Locator_ID(); 296 m_lookup.put(new Integer (M_Locator_ID), loc); 297 } 298 rs.close(); 299 pstmt.close(); 300 } 301 catch (SQLException e) 302 { 303 log.error("Loader\nSQL=" + sql, e); 304 } 305 log.debug("Loader.run - complete #" + m_lookup.size()); 306 } } 309 313 public Collection getData () 314 { 315 if (m_loader.isAlive()) 316 { 317 log.debug("getData - waiting for Loader"); 318 try 319 { 320 m_loader.join(); 321 } 322 catch (InterruptedException ie) 323 { 324 log.error ("getData - join interrupted - " + ie.getMessage()); 325 } 326 } 327 return m_lookup.values(); 328 } 330 338 public ArrayList getData (boolean mandatory, boolean onlyValidated, boolean onlyActive, boolean temporary) 339 { 340 Collection collection = getData(); 342 ArrayList list = new ArrayList (collection.size()); 343 Iterator it = collection.iterator(); 344 while (it.hasNext()) 345 { 346 MLocator loc = (MLocator)it.next(); 347 if (isValid(loc)) list.add(loc); 349 } 350 351 357 return list; 358 } 360 361 365 public int refresh() 366 { 367 log.debug("refresh - start"); 368 m_loader = new Loader(); 369 m_loader.start(); 370 try 371 { 372 m_loader.join(); 373 } 374 catch (InterruptedException ie) 375 { 376 } 377 log.info("refresh - #" + m_lookup.size()); 378 return m_lookup.size(); 379 } 381 385 public String getColumnName() 386 { 387 return "M_Locator.M_Locator_ID"; 388 } 390 }
| Popular Tags
|