1 14 package org.compiere.model; 15 16 import java.sql.*; 17 import java.io.Serializable ; 18 import java.util.*; 19 20 import org.compiere.util.*; 21 22 28 public final class MCountry extends X_C_Country 29 implements Comparator, Serializable 30 { 31 36 public MCountry (Properties ctx, int C_Country_ID) 37 { 38 super (ctx, C_Country_ID); 39 if (C_Country_ID == 0) 40 { 41 setHasRegion(false); 42 setHasPostal_Add(false); 43 } 44 } 46 51 public MCountry (Properties ctx, ResultSet rs) 52 { 53 super (ctx, rs); 54 } 56 57 private static Logger s_log = Logger.getCLogger (MCountry.class); 58 59 63 public String toString() 64 { 65 return getName(); 66 } 68 74 public int compare(Object o1, Object o2) 75 { 76 String s1 = o1.toString(); 77 if (s1 == null) 78 s1 = ""; 79 String s2 = o2.toString(); 80 if (s2 == null) 81 s2 = ""; 82 return s1.compareTo(s2); 83 } 85 86 87 88 private static CCache s_countries = null; 89 private static MCountry s_default = null; 90 91 95 private static void loadAllCountries (Properties ctx) 96 { 97 s_countries = new CCache("countries", 250); 98 String sql = "SELECT * FROM C_Country WHERE IsActive='Y'"; 99 try 100 { 101 Statement stmt = DB.createStatement(); 102 ResultSet rs = stmt.executeQuery(sql); 103 while(rs.next()) 104 { 105 MCountry c = new MCountry (ctx, rs); 106 s_countries.put(String.valueOf(c.getC_Country_ID()), c); 107 if (c.isDefault()) 108 s_default = c; 109 } 110 rs.close(); 111 stmt.close(); 112 } 113 catch (SQLException e) 114 { 115 s_log.error("loadAllCountries", e); 116 } 117 s_log.debug("loadAllCountries=" + s_countries.size()); 118 } 120 126 public static MCountry get (Properties ctx, int C_Country_ID) 127 { 128 if (s_countries == null || s_countries.size() == 0) 129 loadAllCountries(ctx); 130 String key = String.valueOf(C_Country_ID); 131 MCountry c = (MCountry)s_countries.get(key); 132 if (c != null) 133 return c; 134 c = new MCountry (ctx, C_Country_ID); 135 if (c.getC_Country_ID() == C_Country_ID) 136 { 137 s_countries.put(key, c); 138 return c; 139 } 140 return null; 141 } 143 148 public static MCountry getDefault (Properties ctx) 149 { 150 if (s_countries == null || s_countries.size() == 0) 151 loadAllCountries(ctx); 152 return s_default; 153 } 155 160 public static MCountry[] getCountries(Properties ctx) 161 { 162 if (s_countries == null || s_countries.size() == 0) 163 loadAllCountries(ctx); 164 MCountry[] retValue = new MCountry[s_countries.size()]; 165 s_countries.values().toArray(retValue); 166 Arrays.sort(retValue, new MCountry(ctx, 0)); 167 return retValue; 168 } 170 171 172 173 177 public static void main (String [] args) 178 { 179 229 } 231 } | Popular Tags |