1 14 package org.compiere.model; 15 16 import java.util.*; 17 import java.sql.*; 18 import java.io.Serializable ; 19 20 import org.compiere.util.*; 21 22 28 public final class MRegion extends X_C_Region 29 implements Comparator, Serializable 30 { 31 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 } 44 49 public MRegion (Properties ctx, ResultSet rs) 50 { 51 super (ctx, rs); 52 } 54 55 private static Logger s_log = Logger.getCLogger (MRegion.class); 56 57 61 public String toString() 62 { 63 return getName(); 64 } 66 72 public int compare(Object o1, Object o2) 73 { 74 String s1 = o1.toString(); 75 if (s1 == null) 76 s1 = ""; 77 String s2 = o2.toString(); 78 if (s2 == null) 79 s2 = ""; 80 return s1.compareTo(s2); 81 } 83 84 85 86 private static CCache s_regions = null; 87 private static MRegion s_default = null; 88 89 93 private static void loadAllRegions (Properties ctx) 94 { 95 s_regions = new CCache("regions", 100); 96 String 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 } 118 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 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 } 141 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 } 153 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 } 168 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 MRegion[] retValue = new MRegion[list.size()]; 188 list.toArray(retValue); 189 Arrays.sort(retValue, new MRegion(ctx, 0)); 190 return retValue; 191 } 193 } | Popular Tags |