KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
17 import java.sql.*;
18 import java.io.Serializable JavaDoc;
19
20 import org.compiere.util.*;
21
22 /**
23  * Localtion Region Model (Value Object)
24  *
25  * @author Jorg Janke
26  * @version $Id: MRegion.java,v 1.6 2003/10/04 03:55:45 jjanke Exp $
27  */

28 public final class MRegion extends X_C_Region
29     implements Comparator, Serializable JavaDoc
30 {
31     /**
32      * Create empty Region
33      * @param ctx context
34      * @param C_Region_ID id
35      */

36     public MRegion (Properties ctx, int C_Region_ID)
37     {
38         super (ctx, C_Region_ID);
39         if (C_Region_ID == 0)
40         {
41         }
42     } // MRegion
43

44     /**
45      * Create Region from current row in ResultSet
46      * @param ctx context
47      * @param rs result set
48      */

49     public MRegion (Properties ctx, ResultSet rs)
50     {
51         super (ctx, rs);
52     } // MRegion
53

54     /** Static Logger */
55     private static Logger s_log = Logger.getCLogger (MRegion.class);
56
57     /**
58      * Return Name
59      * @return Name
60      */

61     public String JavaDoc toString()
62     {
63         return getName();
64     } // toString
65

66     /**
67      * Compare
68      * @param o1 object 1
69      * @param o2 object 2
70      * @return -1,0, 1
71      */

72     public int compare(Object JavaDoc o1, Object JavaDoc o2)
73     {
74         String JavaDoc s1 = o1.toString();
75         if (s1 == null)
76             s1 = "";
77         String JavaDoc s2 = o2.toString();
78         if (s2 == null)
79             s2 = "";
80         return s1.compareTo(s2);
81     } // compare
82

83     /*************************************************************************/
84
85     /** Region Cache */
86     private static CCache s_regions = null;
87     private static MRegion s_default = null;
88
89     /**
90      * Load Regions (cached)
91      * @param ctx context
92      */

93     private static void loadAllRegions (Properties ctx)
94     {
95         s_regions = new CCache("regions", 100);
96         String JavaDoc sql = "SELECT * FROM C_Region WHERE IsActive='Y'";
97         try
98         {
99             Statement stmt = DB.createStatement();
100             ResultSet rs = stmt.executeQuery(sql);
101             while(rs.next())
102             {
103                 MRegion r = new MRegion (ctx, rs);
104                 s_regions.put(String.valueOf(r.getC_Region_ID()), r);
105                 if (r.isDefault())
106                     s_default = r;
107             }
108             rs.close();
109             stmt.close();
110         }
111         catch (SQLException e)
112         {
113             s_log.error("loadAllRegions", e);
114         }
115         s_log.debug("loadAllRegions=" + s_regions.size() + " - default=" + s_default);
116     } // get
117

118     /**
119      * Get Country (cached)
120      * @param ctx context
121      * @param C_Region_ID ID
122      * @return Country
123      */

124     public static MRegion get (Properties ctx, int C_Region_ID)
125     {
126         if (s_regions == null || s_regions.size() == 0)
127             loadAllRegions(ctx);
128         String JavaDoc key = String.valueOf(C_Region_ID);
129         MRegion r = (MRegion)s_regions.get(key);
130         if (r != null)
131             return r;
132         r = new MRegion (ctx, C_Region_ID);
133         if (r.getC_Region_ID() == C_Region_ID)
134         {
135             s_regions.put(key, r);
136             return r;
137         }
138         return null;
139     } // get
140

141     /**
142      * Get Default Country
143      * @param ctx context
144      * @return Country
145      */

146     public static MRegion getDefault (Properties ctx)
147     {
148         if (s_regions == null || s_regions.size() == 0)
149             loadAllRegions(ctx);
150         return s_default;
151     } // get
152

153     /**
154      * Return Countries as Array
155      * @param ctx context
156      * @return MCountry Array
157      */

158     public static MRegion[] getRegions(Properties ctx)
159     {
160         if (s_regions == null || s_regions.size() == 0)
161             loadAllRegions(ctx);
162         MRegion[] retValue = new MRegion[s_regions.size()];
163         s_regions.values().toArray(retValue);
164         Arrays.sort(retValue, new MRegion(ctx, 0));
165         return retValue;
166     } // getRegions
167

168     /**
169      * Return Array of Regions of Country
170      * @param ctx context
171      * @param C_Country_ID country
172      * @return MRegion Array
173      */

174     public static MRegion[] getRegions (Properties ctx, int C_Country_ID)
175     {
176         if (s_regions == null || s_regions.size() == 0)
177             loadAllRegions(ctx);
178         ArrayList list = new ArrayList();
179         Iterator it = s_regions.values().iterator();
180         while (it.hasNext())
181         {
182             MRegion r = (MRegion)it.next();
183             if (r.getC_Country_ID() == C_Country_ID)
184                 list.add(r);
185         }
186         // Sort it
187
MRegion[] retValue = new MRegion[list.size()];
188         list.toArray(retValue);
189         Arrays.sort(retValue, new MRegion(ctx, 0));
190         return retValue;
191     } // getRegions
192

193 } // MRegion
194
Popular Tags