KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > applications > faces > address > MatchAddressDB


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

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 JavaDoc;
39 import java.io.BufferedInputStream JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.TreeMap JavaDoc;
43 import java.util.zip.GZIPInputStream JavaDoc;
44
45 /**
46  * MatchAddressDB is the bridge between the database and the MatchBean objects.
47  */

48 public class MatchAddressDB extends MatchBean {
49
50     private static Log log = LogFactory.getLog(MatchAddressDB.class);
51
52     //database entry mapping
53
private TreeMap JavaDoc stateMap;
54     private TreeMap JavaDoc zipMap;
55
56     /**
57      * Initializes the TreeMaps and decodes the XML file to map objects to
58      * them.
59      */

60     public MatchAddressDB() {
61
62         //maps for the address match objects
63
stateMap = new TreeMap JavaDoc();
64         zipMap = new TreeMap JavaDoc();
65
66         //read the database from the XML file and map it
67
loadXDB();
68     }
69
70     /**
71      * Decodes the stored XML wrapper object representation of the address
72      * database and maps the entries as MatchCity, MatchState, and MatchZip
73      * objects.
74      *
75      * @see XAddressDataWrapper
76      */

77     private void loadXDB() {
78
79         //each of these contains a city, state, and zip value
80
XAddressDataWrapper xData = null;
81
82         //for decoding the wrapper objects
83
XMLDecoder JavaDoc xDecode = null;
84
85         try {
86             //load andunzip the xml file
87
GZIPInputStream JavaDoc in = new GZIPInputStream JavaDoc(
88                     MatchAddressDB.class.getResourceAsStream("address.xml.gz"));
89
90             //xml decoding mechanism
91
xDecode = new XMLDecoder JavaDoc(new BufferedInputStream JavaDoc(in));
92             xData = (XAddressDataWrapper) xDecode.readObject();
93
94         } catch (Exception JavaDoc e) {
95             if (log.isDebugEnabled())
96                 log.debug("Database not found.");
97         }
98
99         //loop through every entry in the xml file
100
MatchBean city;
101         MatchState state;
102         MatchZip zip;
103
104         while (xData != null && xDecode != null) {
105
106             //create zip, city and state objects
107
zip = (MatchZip) zipMap.get((xData.getZip()));
108
109             //new zip
110
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                 //new state
120
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             //get the next encoded object
129
try {
130                 xData = (XAddressDataWrapper) xDecode.readObject();
131             }
132             //end of file
133
catch (ArrayIndexOutOfBoundsException JavaDoc e) {
134                 if (log.isDebugEnabled())
135                     log.debug("Reached end of XML file.");
136                 return;
137             }
138
139         }
140         //close the XML decoder
141
xDecode.close();
142     }
143
144     /**
145      * Determines whether a given state is in the state map
146      *
147      * @param checkState the state to check
148      * @return state object with modified match flag
149      */

150     public MatchState getState(String JavaDoc checkState) {
151         MatchState state = (MatchState) stateMap.get(checkState);
152         if (state != null) {
153             state.setMatch(true);
154         }
155         return state;
156     }
157
158     /**
159      * Finds the closest matching state to the given string
160      *
161      * @param checkState the state to check
162      * @return the closest state that can be found
163      */

164     public MatchState getClosestState(String JavaDoc checkState) {
165         checkState = checkState.trim().toUpperCase();
166         return (MatchState) getClosestMatch(checkState, stateMap);
167     }
168
169     /**
170      * Retrieves all of the state values from the state map.
171      *
172      * @return array of Strings of states
173      */

174     public String JavaDoc[] getAllStates() {
175
176         String JavaDoc states[] = new String JavaDoc[stateMap.size()];
177         Iterator JavaDoc itor = stateMap.keySet().iterator();
178         int i = 0;
179
180         while (itor.hasNext()) {
181             states[i++] = (String JavaDoc) itor.next();
182         }
183         return states;
184     }
185
186     /**
187      * Determines whether a given zip is in the zip map
188      *
189      * @param checkZip the zip to check
190      * @return zip object with modified match flag
191      */

192     public MatchZip getZip(String JavaDoc checkZip) {
193
194         MatchZip zip = (MatchZip) zipMap.get(checkZip);
195         if (zip != null) {
196             zip.setMatch(true);
197         }
198         return zip;
199     }
200
201     /**
202      * Finds the closest matching zip to the given string
203      *
204      * @param checkZip the zip to check
205      * @return the closest zip that can be found
206      */

207     public MatchZip getClosestZip(String JavaDoc checkZip) {
208
209         checkZip = checkZip.trim();
210         return (MatchZip) getClosestMatch(checkZip, zipMap);
211     }
212
213     /**
214      * Finds all cities that match the provided string exactly.
215      *
216      * @param checkCity the city to check
217      * @return list of cities
218      */

219     public ArrayList JavaDoc getCity(String JavaDoc checkCity) {
220
221         Iterator JavaDoc states = stateMap.values().iterator();
222         ArrayList JavaDoc cities = new ArrayList JavaDoc();
223
224         //check every object in the state map
225
while (states.hasNext()) {
226             MatchState state = (MatchState) states.next();
227             MatchCity city = state.getCity(checkCity);
228
229             //add each matching city to the list
230
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     /**
243      * Finds the closest cities to the provided city.
244      *
245      * @param checkCity the city to check
246      * @return list of cities
247      */

248     public ArrayList JavaDoc getClosestCity(String JavaDoc checkCity) {
249
250         String JavaDoc myCheckCity = AddressFormProcessor.fixCapitalization(checkCity);
251         ArrayList JavaDoc cities = getCity(myCheckCity);
252
253         if (cities != null) {
254             //at least one exact match, so exit
255
return cities;
256         }
257
258         //get closest city in each state
259
Iterator JavaDoc itor = stateMap.values().iterator();
260         TreeMap JavaDoc closest = new TreeMap JavaDoc();
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         //get closest of the closest cities
270
city = (MatchCity) getClosestMatch(myCheckCity, closest);
271
272         //must return all cities with this name
273
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