KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.Serializable JavaDoc;
19
20 import org.compiere.util.DB;
21 import org.compiere.util.Env;
22 import org.compiere.util.CCache;
23 import org.compiere.util.NamePair;
24 import org.compiere.util.KeyNamePair;
25
26 /**
27  * Address Loaction Lookup Model - not cached.
28  * <p>
29  * MLocation maintains a static cache of countries and regions
30  *
31  * @author Jorg Janke
32  * @version $Id: MLocation.java,v 1.15 2003/10/27 15:22:39 jjanke Exp $
33  */

34 public final class MLocation extends Lookup
35     implements Serializable JavaDoc
36 {
37     /**
38      * Constructor
39      * @param ctx context
40      * @param WindowNo window no (to derive AD_Client/Org for new records)
41      */

42     public MLocation(Properties ctx, int WindowNo)
43     {
44         // Default Country & Region
45
if (s_mCountry == null)
46         {
47             // Get Country Preference
48
int pC_Country_ID = 0;
49             String JavaDoc s = Env.getPreference(ctx, 0, "C_Country_ID", true);
50             if (s.length() > 0)
51                 pC_Country_ID = Integer.parseInt(s);
52             if (pC_Country_ID != 0)
53                 s_mCountry = MCountry.get(ctx, pC_Country_ID);
54             if (s_mCountry == null)
55                 s_mCountry = MCountry.getDefault(ctx);
56             if (s_mCountry == null) // Still no default
57
s_mCountry = MCountry.get(ctx, 100); // US
58
//
59
// Regions
60
int pC_Region_ID = 0;
61             s = Env.getPreference(ctx, 0, "C_Region_ID", true);
62             if (s.length() > 0)
63                 pC_Region_ID = Integer.parseInt(s);
64             if (pC_Country_ID != 0)
65                 s_mRegion = MRegion.get(ctx, pC_Region_ID);
66             if (s_mRegion == null)
67                 s_mRegion = MRegion.getDefault(ctx);
68         }
69         //
70
m_ctx = ctx;
71         m_WindowNo = WindowNo;
72         load(0);
73     } // MLocation
74

75     private int m_WindowNo;
76     private Properties m_ctx;
77
78     private X_C_Location m_loc = null;
79     private MCountry mCountry;
80     private MRegion mRegion;
81     //
82
private static MCountry s_mCountry; // default
83
private static MRegion s_mRegion; // default
84

85     /**
86      * Get Display for Value (not cached)
87      * @param value Location_ID
88      * @return String Value
89      */

90     public String JavaDoc getDisplay (Object JavaDoc value)
91     {
92         if (!containsKey (value))
93             return "<" + value.toString() + ">";
94         return toString();
95     } // getDisplay
96

97     /**
98      * Get Object of Key Value
99      * @param value value
100      * @return Object or null
101      */

102     public NamePair get (Object JavaDoc value)
103     {
104         if (value == null)
105             return null;
106         if (!containsKey (value))
107             return null;
108         return new KeyNamePair (m_loc.getC_Location_ID(), toString());
109     } // get
110

111     /**
112      * The Lookup contains the key (not cached)
113      * @param key Location_ID
114      * @return true if key known
115      */

116     public boolean containsKey (Object JavaDoc key)
117     {
118         int intValue = 0;
119         if (key instanceof Integer JavaDoc)
120             intValue = ((Integer JavaDoc)key).intValue();
121         else if (key != null)
122             intValue = Integer.parseInt(key.toString());
123         //
124
return load (intValue);
125     } // containsKey
126

127     /*************************************************************************/
128
129     /**
130      * Get ID
131      * @return ID
132      */

133     public int getC_Location_ID()
134     {
135         return m_loc.getC_Location_ID();
136     } // getC_Location_ID
137

138     /**
139      * Get Address line 1
140      * @return address 1
141      */

142     public String JavaDoc getAddress1()
143     {
144         String JavaDoc s = m_loc.getAddress1();
145         if (s == null)
146             return "";
147         return s;
148     } // getAddress1
149
public void setAddress1 (String JavaDoc Address1)
150     {
151         m_loc.setAddress1(Address1);
152     }
153
154     /**
155      * Get Address line 2
156      * @return address 2
157      */

158     public String JavaDoc getAddress2()
159     {
160         String JavaDoc s = m_loc.getAddress2();
161         if (s == null)
162             return "";
163         return s;
164     } // getAddress2
165
public void setAddress2 (String JavaDoc Address2)
166     {
167         m_loc.setAddress2(Address2);
168     }
169
170     /**
171      * Get formatted City Region Postal line
172      * @return City, Region Postal
173      */

174     public String JavaDoc getCityRegionPostal()
175     {
176         return parseCRP (mCountry);
177     } // getCityRegionPostal
178

179     /**
180      * Get City
181      * @return city
182      */

183     public String JavaDoc getCity()
184     {
185         String JavaDoc s = m_loc.getCity();
186         if (s == null)
187             return "";
188         return s;
189     } // getCity
190
public void setCity (String JavaDoc City)
191     {
192         m_loc.setCity(City);
193     }
194     public int getC_City_ID()
195     {
196         return m_loc.getC_City_ID();
197     }
198     public void setC_City_ID (int C_City_ID)
199     {
200         m_loc.setC_City_ID(C_City_ID);
201     }
202
203     /**
204      * Get Postal
205      * @return Postal
206      */

207     public String JavaDoc getPostal()
208     {
209         String JavaDoc postal = m_loc.getPostal();
210         if (postal == null)
211             postal = "";
212         return postal;
213     } // getPostal
214
public void setPostal (String JavaDoc Postal)
215     {
216         m_loc.setPostal(Postal);
217     }
218
219     public String JavaDoc getPostal_Add()
220     {
221         String JavaDoc add = m_loc.getPostal_Add();
222         if (add == null)
223             add = "";
224         return add;
225     }
226     public void setPostal_Add (String JavaDoc Postal_Add)
227     {
228         m_loc.setPostal_Add(Postal_Add);
229     }
230
231
232     public int getC_Region_ID()
233     {
234         return m_loc.getC_Region_ID();
235     }
236     public void setC_Region_ID (int C_Region_ID)
237     {
238         if (C_Region_ID == 0)
239         {
240             m_loc.setValue("C_Region_ID", null);
241             mRegion = null;
242         }
243         else
244         {
245             m_loc.setC_Region_ID(C_Region_ID);
246             mRegion = MRegion.get(m_ctx, C_Region_ID);
247         }
248     }
249
250     public MRegion getRegion()
251     {
252         return mRegion;
253     }
254     public void setRegion(MRegion region)
255     {
256         mRegion = region;
257         if (region == null)
258         {
259             m_loc.setValue("C_Region_ID", null);
260             return;
261         }
262         m_loc.setC_Region_ID(region.getC_Region_ID());
263         if (region.getC_Country_ID() != m_loc.getC_Country_ID())
264         {
265             log.warn("setRegion - Region(" + region + ") C_Country_ID=" + region.getC_Country_ID()
266               + " overwriting Location.C_Country_ID=" + m_loc.getC_Country_ID());
267             setC_Country_ID(region.getC_Country_ID());
268         }
269     } // setRegion
270

271
272     /**
273      * Get Region Name
274      * @return region Name or ""
275      */

276     public String JavaDoc getRegionName()
277     {
278         String JavaDoc region = m_loc.getRegionName();
279         if (region == null)
280             region = "";
281         int regionID = m_loc.getC_Region_ID();
282         if (regionID == 0)
283             return region;
284         MRegion r = MRegion.get (m_ctx, regionID);
285         if (r != null)
286             return r.getName();
287         return region;
288     } // getRegionName
289

290     /**
291      * Set Region Name
292      * @param RegionName region
293      */

294     public void setRegionName(String JavaDoc RegionName)
295     {
296         m_loc.setRegionName(RegionName);
297     } // setRegionName
298

299
300     public int getC_Country_ID()
301     {
302         return m_loc.getC_Country_ID();
303     }
304     public void setC_Country_ID (int C_Country_ID)
305     {
306         m_loc.setC_Country_ID(C_Country_ID);
307         mCountry = MCountry.get (m_ctx, C_Country_ID);
308     }
309
310     public MCountry getCountry()
311     {
312         return mCountry;
313     }
314     public void setCountry(MCountry country)
315     {
316         if (country == null)
317             throw new IllegalArgumentException JavaDoc ("Country cannot be null");
318         mCountry = country;
319         m_loc.setC_Country_ID(mCountry.getC_Country_ID());
320     }
321
322     /**
323      * Get Country Name
324      * @return Country Name or ""
325      */

326     public String JavaDoc getCountryName()
327     {
328         int countryID = m_loc.getC_Country_ID();
329         if (countryID == 0)
330             return "";
331         MCountry c = MCountry.get (m_ctx, countryID);
332         if (c != null)
333             return c.getName();
334         return "";
335     } // getCountryName
336

337     /**
338      * Set Country Name - NOP
339      * @param country country
340      */

341     public void setCountryName(String JavaDoc country)
342     {
343     } // setCountryName
344

345     /**
346      * Get Country Line
347      * @param onlyForeign if true only foreign country is returned
348      * @return country or null
349      */

350     public String JavaDoc getCountry (boolean onlyForeign)
351     {
352         if (mCountry == null
353             || onlyForeign && mCountry.getC_Country_ID() == s_mCountry.getC_Country_ID())
354             return null;
355         return mCountry.getName();
356     } // getCountry
357

358     public Properties getCtx()
359     {
360         return m_ctx;
361     } // getCtx
362

363     /*************************************************************************/
364
365     /**
366      * Return String representation
367      * @return String
368      */

369     public String JavaDoc toString()
370     {
371         if (m_loc.getC_Location_ID() == 0)
372             return ""; // Msg.getMsg("NotFound");
373

374         StringBuffer JavaDoc retStr = new StringBuffer JavaDoc(getAddress1());
375         if (getAddress2().length() > 0)
376             retStr.append(" ").append(getAddress2());
377         // City, Region, Postal
378
retStr.append(", ").append(parseCRP (mCountry));
379         //
380
return retStr.toString();
381     } // toString
382

383     /**
384      * Return String representation with CR at line end
385      * @return String
386      */

387     public String JavaDoc toStringCR()
388     {
389         if (m_loc.getC_Location_ID() == 0)
390             return ""; // Msg.getMsg("NotFound");
391

392         StringBuffer JavaDoc retStr = new StringBuffer JavaDoc(getAddress1());
393         if (getAddress2().length() > 0)
394             retStr.append("\n").append(getAddress2());
395         // City, Region, Postal
396
retStr.append("\n").append(parseCRP (mCountry));
397         // Add Country would come here
398
return retStr.toString();
399     } // toString
400

401
402     /**
403      * Return detailed String representation
404      * @return String
405      */

406     public String JavaDoc toStringX()
407     {
408         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("MLocation=[");
409         sb.append("ID=").append(m_loc.getC_Location_ID())
410             .append(",").append(toString())
411             .append("]");
412         return sb.toString();
413     } // toStringX
414

415     /*************************************************************************/
416
417     /**
418      * Load Location with ID
419      * @param Location_ID C_Location_ID
420      * @return true if loaded
421      */

422     public boolean load (int Location_ID)
423     {
424         if (Location_ID == 0) // load default
425
{
426             m_loc = new X_C_Location(m_ctx, 0);
427             m_loc.setAddress1("");
428             m_loc.setAddress2("");
429             m_loc.setCity("");
430             m_loc.setPostal("");
431             m_loc.setPostal_Add("");
432             m_loc.setRegionName("");
433             mCountry = s_mCountry;
434             m_loc.setC_Country_ID(s_mCountry.getC_Country_ID());
435             mRegion = s_mRegion;
436             if (mRegion != null)
437                 m_loc.setC_Region_ID(mRegion.getC_Region_ID());
438             return true;
439         }
440         if (Location_ID == m_loc.getC_Location_ID()) // already loaded
441
return true;
442         //
443
m_loc = new X_C_Location (m_ctx, Location_ID);
444         mCountry = MCountry.get (m_ctx, m_loc.getC_Country_ID());
445         mRegion = MRegion.get (m_ctx, m_loc.getC_Region_ID());
446         return true;
447     } // load
448

449     /**
450      * Load Location with ID if Business Partner Location
451      * @param C_BPartner_Location_ID Business Partner Location
452      * @return true if loaded
453      */

454     public boolean loadBPLocation (int C_BPartner_Location_ID)
455     {
456         if (C_BPartner_Location_ID == 0) // load default
457
return load(0);
458
459         String JavaDoc sql = "SELECT * FROM C_Location l "
460             + "WHERE C_Location_ID=(SELECT C_Location_ID FROM C_BPartner_Location WHERE C_BPartner_Location_ID=?)";
461         m_loc = null;
462         try
463         {
464             PreparedStatement pstmt = DB.prepareStatement(sql);
465             pstmt.setInt(1, C_BPartner_Location_ID);
466             ResultSet rs = pstmt.executeQuery();
467             if (rs.next())
468                 m_loc = new X_C_Location (m_ctx, rs);
469             rs.close();
470             pstmt.close();
471         }
472         catch (SQLException e)
473         {
474             log.error("loadBPLocation - " + C_BPartner_Location_ID, e);
475             m_loc = null;
476         }
477         if (m_loc == null) // not found
478
{
479             load(0);
480             return false;
481         }
482         mCountry = MCountry.get (m_ctx, m_loc.getC_Country_ID());
483         mRegion = MRegion.get (m_ctx, m_loc.getC_Region_ID());
484         return true;
485     } // loadBPLocation
486

487     /**
488      * Save to database
489      * @return C_Location_ID
490      */

491     public int save()
492     {
493         m_loc.save();
494         log.debug("save - C_Location_ID=" + m_loc.getC_Location_ID());
495         return m_loc.getC_Location_ID();
496     } // save
497

498
499     /**
500      * Parse according Ctiy/Postal/Region according to displaySequence.
501      * @C@ - City @R@ - Region @P@ - Postal @A@ - PostalAdd
502      * @param c country
503      * @return parsed String
504      */

505     private String JavaDoc parseCRP (MCountry c)
506     {
507         if (c == null)
508             return "CountryNotFound";
509
510         String JavaDoc token;
511         String JavaDoc inStr = c.getDisplaySequence();
512         StringBuffer JavaDoc outStr = new StringBuffer JavaDoc();
513
514         int i = inStr.indexOf("@");
515         while (i != -1)
516         {
517             outStr.append (inStr.substring(0, i)); // up to @
518
inStr = inStr.substring(i+1, inStr.length()); // from first @
519

520             int j = inStr.indexOf("@"); // next @
521
if (j < 0)
522             {
523                 token = ""; // no second tag
524
j = i+1;
525             }
526             else
527                 token = inStr.substring(0, j);
528             //
529
if (token.equals("C"))
530             {
531                 outStr.append(getCity());
532             }
533             else if (token.equals("R"))
534             {
535                 MRegion r = MRegion.get(m_ctx, m_loc.getC_Region_ID());
536                 if (r != null)
537                     outStr.append(r.getName());
538                 else if (getRegionName() != null && getRegionName().length() > 0)
539                     outStr.append(getRegionName());
540             }
541             else if (token.equals("P"))
542             {
543                 outStr.append(m_loc.getPostal());
544             }
545             else if (token.equals("A"))
546             {
547                 String JavaDoc add = m_loc.getPostal_Add();
548                 if (add != null && add.length() > 0)
549                     outStr.append("-").append(add);
550             }
551             else
552                 outStr.append("@").append(token).append("@");
553
554             inStr = inStr.substring(j+1, inStr.length()); // from second @
555
i = inStr.indexOf("@");
556         }
557         outStr.append(inStr); // add the rest of the string
558

559         // Print Region Name if entered and not part of pattern
560
if (getRegionName() != null && getRegionName().length() > 0 && c.getDisplaySequence().indexOf("@R@") == -1)
561             outStr.append(" ").append(getRegionName());
562
563         return outStr.toString();
564     } // parseContext
565

566
567     /**
568      * Get underlying fully qualified Table.Column Name.
569      * Used for VLookup.actionButton (Zoom)
570      * @return column name
571      */

572     public String JavaDoc getColumnName()
573     {
574         return "C_Location_ID";
575     } // getColumnName
576

577     /**
578      * Return data as sorted Array - not implemented
579      * @param mandatory mandatory
580      * @param onlyValidated only validated
581      * @param onlyActive only active
582      * @param temporary force load for temporary display
583      * @return null
584      */

585     public ArrayList getData (boolean mandatory, boolean onlyValidated, boolean onlyActive, boolean temporary)
586     {
587         log.error("getData - not implemented");
588         return null;
589     } // getArray
590

591 } // MLocation
592
Popular Tags