KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MLocatorLookup


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 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-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.sql.*;
17 import java.util.*;
18 import java.math.*;
19 import java.io.Serializable JavaDoc;
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 /**
28  * Warehouse Locator Lookup Model.
29  * (Lookup Model is model.Lookup.java)
30  *
31  * @author Jorg Janke
32  * @version $Id: MLocatorLookup.java,v 1.3 2003/11/02 07:49:19 jjanke Exp $
33  */

34 public final class MLocatorLookup extends Lookup implements Serializable JavaDoc
35 {
36     /**
37      * Constructor
38      * @param ctx context
39      * @param WindowNo window no
40      */

41     public MLocatorLookup(Properties ctx, int WindowNo)
42     {
43         m_ctx = ctx;
44         m_WindowNo = WindowNo;
45         //
46
m_loader = new Loader();
47         m_loader.start();
48     } // MLocator
49

50     private Properties m_ctx;
51     private int m_WindowNo;
52     protected int C_Locator_ID;
53     private Loader m_loader;
54
55     /** Only Warehouse */
56     private int m_only_Warehouse_ID = 0;
57
58     /** Storage of data MLookups */
59     private volatile LinkedHashMap m_lookup = new LinkedHashMap();
60     private static int s_maxRows = 100; // how many rows to read
61

62     /**
63      * Dispose
64      */

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         //
78
super.dispose();
79     } // dispose
80

81     /**
82      * Set Warehouse restriction
83      * @param only_Warehouse_ID wahrehouse
84      */

85     public void setOnly_Warehouse_ID (int only_Warehouse_ID)
86     {
87         m_only_Warehouse_ID = only_Warehouse_ID;
88     } // setOnly_Warehouse_ID
89

90     /**
91      * Get Only Wahrehouse
92      * @return warehouse
93      */

94     public int getOnly_Warehouse_ID()
95     {
96         return m_only_Warehouse_ID;
97     } // getOnly_Warehouse_ID
98

99     /**
100      * Wait until async Load Complete
101      */

102     public void loadComplete()
103     {
104         if (m_loader != null)
105         {
106             try
107             {
108                 m_loader.join();
109             }
110             catch (InterruptedException JavaDoc ie)
111             {
112                 log.error("loadComplete - join interrupted", ie);
113             }
114         }
115     } // loadComplete
116

117     /**
118      * Get value
119      * @param key key
120      * @return value value
121      */

122     public NamePair get (Object JavaDoc key)
123     {
124         if (key == null)
125             return null;
126
127         // try cache
128
MLocator loc = (MLocator) m_lookup.get(key);
129         if (loc != null)
130             return new KeyNamePair (loc.getM_Locator_ID(), loc.toString());
131
132         // Not found and waiting for loader
133
if (m_loader.isAlive())
134         {
135             log.debug("get - waiting for Loader");
136             loadComplete();
137             // is most current
138
loc = (MLocator) m_lookup.get(key);
139         }
140         if (loc != null)
141             return new KeyNamePair (loc.getM_Locator_ID(), loc.toString());
142
143         // Try to get it directly
144
return getDirect(key, true);
145     } // get
146

147     /**
148      * Get Display value
149      * @param value value
150      * @return String to display
151      */

152     public String JavaDoc getDisplay (Object JavaDoc value)
153     {
154         if (value == null)
155             return "";
156         //
157
NamePair display = get (value);
158         if (display == null)
159             return "<" + value.toString() + ">";
160         return display.toString();
161     } // getDisplay
162

163     /**
164      * The Lookup contains the key
165      * @param key key
166      * @return true, if lookup contains key
167      */

168     public boolean containsKey (Object JavaDoc key)
169     {
170         return m_lookup.containsKey(key);
171     } // containsKey
172

173     /**
174      * Get Data Direct from Table
175      * @param keyValue integer key value
176      * @param saveInCache save in cache
177      * @return Object directly loaded
178      */

179     public NamePair getDirect (Object JavaDoc keyValue, boolean saveInCache)
180     {
181         MLocator loc = getMLocator (keyValue);
182         if (loc == null)
183             return null;
184         //
185
int key = loc.getM_Locator_ID();
186         if (saveInCache)
187             m_lookup.put(new Integer JavaDoc(key), loc);
188         NamePair retValue = new KeyNamePair(key, loc.toString());
189         return retValue;
190     } // getDirect
191

192     /**
193      * Get Data Direct from Table
194      * @param keyValue integer key value
195      * @return Object directly loaded
196      */

197     public MLocator getMLocator (Object JavaDoc keyValue)
198     {
199     // Log.trace(Log.l6_Database, "MLocatorLookup.getDirect " + keyValue.getClass() + "=" + keyValue);
200
int M_Locator_ID = -1;
201         try
202         {
203             M_Locator_ID = Integer.parseInt(keyValue.toString());
204         }
205         catch (Exception JavaDoc e)
206         {}
207         if (M_Locator_ID == -1)
208         {
209             log.error("getMLocator - invalid key=" + keyValue);
210             return null;
211         }
212         //
213
return new MLocator (m_ctx, M_Locator_ID);
214     } // getMLocator
215

216     /**
217      * @return a string representation of the object.
218      */

219     public String JavaDoc toString()
220     {
221         return "MLocatorLookup[Size=" + m_lookup.size() + "]";
222     } // toString
223

224
225     /**
226      * Is Locator with key valid (Warehouse)
227      * @param key key
228      * @return true if valid
229      */

230     public boolean isValid (Object JavaDoc key)
231     {
232         if (key == null)
233             return true;
234         // try cache
235
MLocator loc = (MLocator) m_lookup.get(key);
236         if (loc == null)
237             loc = getMLocator(key);
238         return isValid(loc);
239     } // isValid
240

241     /**
242      * Is Locator with key valid (Warehouse)
243      * @param locator locator
244      * @return true if valid
245      */

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     } // isValid
252

253
254     /**************************************************************************
255      * Loader
256      */

257     class Loader extends Thread JavaDoc implements Serializable JavaDoc
258     {
259         public Loader()
260         {
261             super("MLocatorLookup");
262         } // Loader
263

264         /**
265          * Load Lookup
266          */

267         public void run()
268         {
269         // Log.trace(Log.l3_Util, "MLocatorLookup Loader.run " + m_AD_Column_ID);
270
// Set Info
271
StringBuffer JavaDoc sql = new StringBuffer JavaDoc("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 JavaDoc 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             // Reset
284
m_lookup.clear();
285             int rows = 0;
286             try
287             {
288                 PreparedStatement pstmt = DB.prepareStatement(finalSql);
289                 ResultSet rs = pstmt.executeQuery();
290
291                 // Get first 100 rows
292
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 JavaDoc(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         } // run
307
} // Loader
308

309     /**
310      * Return info as ArrayList containing Locator, waits for the loader to finish
311      * @return Collection of lookup values
312      */

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 JavaDoc ie)
323             {
324                 log.error ("getData - join interrupted - " + ie.getMessage());
325             }
326         }
327         return m_lookup.values();
328     } // getData
329

330     /**
331      * Return data as sorted ArrayList
332      * @param mandatory mandatory
333      * @param onlyValidated only validated
334      * @param onlyActive only active
335      * @param temporary force load for temporary display
336      * @return ArrayList of lookup values
337      */

338     public ArrayList getData (boolean mandatory, boolean onlyValidated, boolean onlyActive, boolean temporary)
339     {
340         // create list
341
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)) // only valid warehouses
348
list.add(loc);
349         }
350
351         /** Sort Data
352         MLocator l = new MLocator (m_ctx, 0);
353         if (!mandatory)
354             list.add (l);
355         Collections.sort (list, l);
356         **/

357         return list;
358     } // getArray
359

360
361     /**
362      * Refresh Values
363      * @return new size of lookup
364      */

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 JavaDoc ie)
375         {
376         }
377         log.info("refresh - #" + m_lookup.size());
378         return m_lookup.size();
379     } // refresh
380

381     /**
382      * Get underlying fully qualified Table.Column Name
383      * @return Table.ColumnName
384      */

385     public String JavaDoc getColumnName()
386     {
387         return "M_Locator.M_Locator_ID";
388     } // getColumnName
389

390 } // MLocatorLookup
391
Popular Tags