1 33 package com.icesoft.applications.faces.address; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 38 import java.beans.XMLDecoder ; 39 import java.io.BufferedInputStream ; 40 import java.util.ArrayList ; 41 import java.util.Iterator ; 42 import java.util.TreeMap ; 43 import java.util.zip.GZIPInputStream ; 44 45 48 public class MatchAddressDB extends MatchBean { 49 50 private static Log log = LogFactory.getLog(MatchAddressDB.class); 51 52 private TreeMap stateMap; 54 private TreeMap zipMap; 55 56 60 public MatchAddressDB() { 61 62 stateMap = new TreeMap (); 64 zipMap = new TreeMap (); 65 66 loadXDB(); 68 } 69 70 77 private void loadXDB() { 78 79 XAddressDataWrapper xData = null; 81 82 XMLDecoder xDecode = null; 84 85 try { 86 GZIPInputStream in = new GZIPInputStream ( 88 MatchAddressDB.class.getResourceAsStream("address.xml.gz")); 89 90 xDecode = new XMLDecoder (new BufferedInputStream (in)); 92 xData = (XAddressDataWrapper) xDecode.readObject(); 93 94 } catch (Exception e) { 95 if (log.isDebugEnabled()) 96 log.debug("Database not found."); 97 } 98 99 MatchBean city; 101 MatchState state; 102 MatchZip zip; 103 104 while (xData != null && xDecode != null) { 105 106 zip = (MatchZip) zipMap.get((xData.getZip())); 108 109 if (zip == null) { 111 zip = new MatchZip(xData.getZip(), xData.getCity(), 112 xData.getState()); 113 zipMap.put((xData.getZip()), zip); 114 115 city = new MatchCity(xData.getCity(), xData.getState()); 116 117 state = (MatchState) stateMap.get((xData.getState())); 118 119 if (state == null) { 121 state = new MatchState(xData.getState()); 122 stateMap.put((xData.getState()), state); 123 124 } 125 city = state.addCity((MatchCity) city); 126 ((MatchCity) city).addZip(zip); 127 } 128 try { 130 xData = (XAddressDataWrapper) xDecode.readObject(); 131 } 132 catch (ArrayIndexOutOfBoundsException e) { 134 if (log.isDebugEnabled()) 135 log.debug("Reached end of XML file."); 136 return; 137 } 138 139 } 140 xDecode.close(); 142 } 143 144 150 public MatchState getState(String checkState) { 151 MatchState state = (MatchState) stateMap.get(checkState); 152 if (state != null) { 153 state.setMatch(true); 154 } 155 return state; 156 } 157 158 164 public MatchState getClosestState(String checkState) { 165 checkState = checkState.trim().toUpperCase(); 166 return (MatchState) getClosestMatch(checkState, stateMap); 167 } 168 169 174 public String [] getAllStates() { 175 176 String states[] = new String [stateMap.size()]; 177 Iterator itor = stateMap.keySet().iterator(); 178 int i = 0; 179 180 while (itor.hasNext()) { 181 states[i++] = (String ) itor.next(); 182 } 183 return states; 184 } 185 186 192 public MatchZip getZip(String checkZip) { 193 194 MatchZip zip = (MatchZip) zipMap.get(checkZip); 195 if (zip != null) { 196 zip.setMatch(true); 197 } 198 return zip; 199 } 200 201 207 public MatchZip getClosestZip(String checkZip) { 208 209 checkZip = checkZip.trim(); 210 return (MatchZip) getClosestMatch(checkZip, zipMap); 211 } 212 213 219 public ArrayList getCity(String checkCity) { 220 221 Iterator states = stateMap.values().iterator(); 222 ArrayList cities = new ArrayList (); 223 224 while (states.hasNext()) { 226 MatchState state = (MatchState) states.next(); 227 MatchCity city = state.getCity(checkCity); 228 229 if (city != null) { 231 city.setMatch(true); 232 cities.add(city); 233 } 234 } 235 if (cities.size() == 0) { 236 return null; 237 } else { 238 return cities; 239 } 240 } 241 242 248 public ArrayList getClosestCity(String checkCity) { 249 250 String myCheckCity = AddressFormProcessor.fixCapitalization(checkCity); 251 ArrayList cities = getCity(myCheckCity); 252 253 if (cities != null) { 254 return cities; 256 } 257 258 Iterator itor = stateMap.values().iterator(); 260 TreeMap closest = new TreeMap (); 261 MatchCity city; 262 263 while (itor.hasNext()) { 264 MatchState state = (MatchState) itor.next(); 265 city = state.getClosestCity(myCheckCity); 266 closest.put((city.getCity()), city); 267 } 268 269 city = (MatchCity) getClosestMatch(myCheckCity, closest); 271 272 cities = getCity(city.getCity()); 274 itor = cities.iterator(); 275 276 while (itor.hasNext()) { 277 ((MatchCity) itor.next()).setMatch(false); 278 } 279 return cities; 280 } 281 } | Popular Tags |