KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.icesoft.faces.component.ext.HtmlCommandButton;
36 import com.icesoft.faces.component.ext.HtmlInputText;
37 import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
38
39 import javax.faces.application.FacesMessage;
40 import javax.faces.component.UIComponent;
41 import javax.faces.component.UIForm;
42 import javax.faces.component.UISelectOne;
43 import javax.faces.context.FacesContext;
44 import javax.faces.event.ValueChangeEvent;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.TreeMap JavaDoc;
48
49 /**
50  * Handles all of the inter-field form validation and ValueChangeEvents. Stores
51  * calculated values for city, state, and zip, and injects them when PhaseSync
52  * calls the inject() method
53  *
54  * @see PhaseSync, FormElementContainer
55  */

56 public class AddressFormProcessor {
57
58     //access to the address database and associated methods (static)
59
static {
60         dataBean = new MatchAddressDB();
61     }
62
63     private static final String JavaDoc NOT_IN_LIST = "";
64
65     //form elements - title, first name, last name, city, state, zip
66
private ArrayList JavaDoc componentList;
67
68     //actual city, state, and zip values
69
private LinkedFormElement city, state, zip;
70
71     //injector values
72
private String JavaDoc newCity, newState, newZip;
73
74     private SubmitButton submit;
75     protected static MatchAddressDB dataBean;
76
77     // component bindings
78
private HtmlInputText firstNameText = null;
79     private HtmlInputText lastNameText = null;
80     private HtmlInputText cityText = null;
81     private HtmlInputText stateText = null;
82     private HtmlInputText zipText = null;
83
84     private UISelectOne selectTitle = null;
85     private UISelectOne selectCity = null;
86     private UISelectOne selectState = null;
87     private UISelectOne selectZip = null;
88
89     private HtmlCommandButton submitButton = null;
90
91     private boolean addressComplete = false;
92
93
94     public HtmlCommandButton getSubmitButton() {
95         return submitButton;
96     }
97
98     public void setSubmitButton(HtmlCommandButton submitButton) {
99         this.submitButton = submitButton;
100     }
101
102     /**
103      * Makes references to the FormElement objects.
104      *
105      * @see FormElementContainer
106      */

107     public AddressFormProcessor(FormElementContainer elementContainer) {
108
109         //this allows the use of an iterator over all of the form elements
110
componentList = elementContainer.getComponentList();
111
112         //the submit button element
113
submit = elementContainer.getSubmit();
114
115         //city, state, and zip elements
116
city = elementContainer.getCity();
117         state = elementContainer.getState();
118         zip = elementContainer.getZip();
119
120         //temporary storage for injector values
121
newCity = city.getValue();
122         newState = state.getValue();
123         newZip = zip.getValue();
124
125     }
126
127     /**
128      * Clears the temporary values.
129      */

130     public void reset() {
131         newCity = newState = newZip = "";
132         submit.setStatus(false);
133         addressComplete = false;
134         city.reset();
135         state.reset();
136         zip.reset();
137         selectState.setValue("");
138         selectCity.setValue("");
139         selectZip.setValue("");
140         selectTitle.setValue("");
141     }
142
143     /**
144      * Called by PhaseSync to inject the calculated temporary values (newCity,
145      * newState, newZip) into their respective FormElementContainer objects each
146      * cycle.
147      *
148      * @see PhaseSync
149      */

150     public void inject() {
151
152         //assign the temporary values to the actual city, state, and zip values
153
city.setValue(newCity);
154         state.setValue(newState);
155         zip.setValue(newZip);
156     }
157
158     /**
159      * Determines whether or not to activate the submit button. This method is
160      * called by PhaseSync to determine the state of the submit button each JSF
161      * cycle.
162      *
163      * @see PhaseSync
164      */

165     public void updateSubmitButton() {
166
167         boolean status = true;
168
169         Iterator JavaDoc i = componentList.iterator();
170         FormElement current;
171
172         //one un-set element will make status false
173
while (i.hasNext()) {
174
175             current = (FormElement) i.next();
176             if (!current.getIsSet()) {
177                 status = false;
178             }
179         }
180         //all the elements are set, determine if the address is valid
181
if (!isAddressValid()) {
182             status = false;
183
184             // clear all of the warning flags if the address is valid
185
} else {
186
187             // reset the iterator
188
i = componentList.iterator();
189             while (i.hasNext()) {
190
191                 current = (FormElement) i.next();
192                 current.setImage(FormElement.IMAGE_BLANK);
193             }
194         }
195         submit.setStatus(status);
196         if ((status) && !addressComplete) {
197             addressComplete = true;
198             submitButton.requestFocus();
199         }
200     }
201
202     // recursive function keeps recursing unto a form is found
203
private UIForm getForm(UIComponent uiComponent) {
204         UIComponent form = uiComponent.getParent();
205         if (form instanceof UIForm) {
206             return (UIForm) form;
207         } else {
208             return getForm(form);
209         }
210     }
211
212     /**
213      * Calculates form values based on the new city value.
214      *
215      * @param event the event caused by the form change
216      */

217     public void cityChanged(ValueChangeEvent event) {
218
219         FacesContext context = FacesContext.getCurrentInstance();
220         FacesMessage msg;
221         UIComponent component = event.getComponent();
222
223         String JavaDoc newValue = (String JavaDoc) event.getNewValue();
224
225         city.setImage(LinkedFormElement.IMAGE_PROGRESS);
226
227         if (newValue == null || newValue.equals("")) {
228             newValue = "";
229             newCity = newValue;
230             return;
231         }
232
233         newCity = newValue;
234
235         city.setImage(LinkedFormElement.IMAGE_PROGRESS);
236
237         boolean stateAutoFilled = false;
238         newCity = fixCapitalization(newCity);
239
240         //process city against zip
241
MatchZip zipDb;
242
243         if ((null != newZip) && (newZip.length() > 0)) {
244
245             //see if we get a valid zip
246
zipDb = dataBean.getZip(newZip);
247
248             if (zipDb != null) {
249
250                 //city must match zip to be valid
251
if (!zipDb.getCity().equals(newCity)) {
252                     msg = new FacesMessage("City not found with Zip "
253                                            + newZip + ". Our best guess is "
254                                            + zipDb.getCity() + ", " +
255                                            zipDb.getState());
256
257                     city.setImage(LinkedFormElement.IMAGE_ALERT);
258                     context.addMessage(component.getClientId(context), msg);
259                     return;
260                 } else {
261                     //zip and city match
262
if (newCity.length() == 0) {
263                         //we can autofill the state
264
newCity = zipDb.getState();
265                         city.setImage(LinkedFormElement.IMAGE_BLANK);
266                         stateAutoFilled = true;
267                     }
268                 }
269             }
270         }
271
272         //process city against state
273
if (!stateAutoFilled && (null != newState)
274             && newState.length() > 0) {
275             //see if we get a valid state
276
MatchState stateDb = dataBean.getState(newState);
277
278             if (stateDb != null) {
279                 MatchCity bestGuessCity = stateDb.getClosestCity(newCity);
280                 if (bestGuessCity.isMatch()) {
281
282                     if (newZip.length() == 0) {
283                         //autofill zip if there is only one
284
autoFillZip(bestGuessCity);
285                     }
286                 } else {
287                     //city and state don't match
288
msg = new FacesMessage("City not found in "
289                                            + newState + ". Our best guess is "
290                                            + bestGuessCity.getCity() + ", "
291                                            + bestGuessCity.getState());
292                     city.setImage(LinkedFormElement.IMAGE_ALERT);
293                     context.addMessage(component.getClientId(context), msg);
294                     return;
295                 }
296             }
297         }
298
299         if ((newZip == null) || (newZip.length() == 0)
300                                 && (newState == null) || newState.length() == 0)
301         {
302             //no zip or state, so check for best match
303
ArrayList JavaDoc possibleCities = dataBean.getClosestCity(newCity);
304             MatchCity possibleCity = (MatchCity) possibleCities.get(0);
305
306             if (possibleCity.isMatch()) {
307                 //exact city match
308
newCity = possibleCity.getCity();
309
310                 if (possibleCities.size() == 1) {
311                     //only one match, so autofill state and possibly zip
312
newState = possibleCity.getState();
313
314                     //autofill zips
315
autoFillZip(possibleCity);
316                 } else {
317                     //multiple states
318
ArrayList JavaDoc stateSelect =
319                             new ArrayList JavaDoc(possibleCities.size() + 1);
320                     stateSelect.add(NOT_IN_LIST);
321                     for (int i = 0; i < possibleCities.size(); i++) {
322                         stateSelect.add((
323                                 (MatchCity) possibleCities.get(i)) .getState());
324                     }
325                     //drop-down selection
326
state.setSelect(stateSelect);
327                     state.setSelectRendered(true);
328                     ((HtmlSelectOneMenu) selectState).requestFocus();
329                 }
330             } else {
331                 //don't have exact match, so suggest our best guess
332
msg = new FacesMessage("City is not valid. Our best guess is "
333                                        + possibleCity.getCity() + ", "
334                                        + possibleCity.getState());
335                 city.setImage(LinkedFormElement.IMAGE_ALERT);
336                 context.addMessage(component.getClientId(context), msg);
337             }
338         }
339     }
340
341     public void titleSelectChanged(ValueChangeEvent event) {
342         firstNameText.requestFocus();
343     }
344
345     /**
346      * Calculates form values based on the new selected city value.
347      *
348      * @param event the event caused by the form change
349      */

350     public void citySelectChanged(ValueChangeEvent event) {
351
352         FacesContext context = FacesContext.getCurrentInstance();
353         FacesMessage msg;
354         UIComponent component = event.getComponent();
355         String JavaDoc newValue = (String JavaDoc) event.getNewValue();
356
357         city.setImage(LinkedFormElement.IMAGE_PROGRESS);
358
359         if ((newValue == null) || "".equals(newValue)) {
360             return;
361         }
362
363         if (newValue.equals(NOT_IN_LIST)) {
364             //revert to blank text input field
365
newCity = "";
366         } else {
367             newCity = newValue;
368         }
369
370         //disable the drop-down list
371
city.setSelectRendered(false);
372
373         //process city against zip
374
MatchZip zipDb;
375
376         if ((null != newZip) && (newZip.length() > 0)) {
377             //see if we get a valid zip
378
zipDb = dataBean.getZip(newZip);
379
380             if (zipDb != null) {
381                 //city must match zip to be valid
382
if (!zipDb.getCity().equals(newCity)) {
383                     msg = new FacesMessage("City not found with Zip "
384                                            + newZip + ". Our best guess is "
385                                            + zipDb.getCity());
386                     city.setImage(LinkedFormElement.IMAGE_ALERT);
387                     context.addMessage(component.getClientId(context), msg);
388                     return;
389                 }
390             }
391         } else {
392             //have a city and state, so see if we can autofill zip
393
MatchState stateDb = dataBean.getState(newState);
394             if (stateDb != null) {
395                 MatchCity cityDb = stateDb.getCity(newCity);
396                 if (cityDb != null) {
397                     autoFillZip(cityDb);
398                 }
399             }
400         }
401         city.setImage(LinkedFormElement.IMAGE_BLANK);
402         city.reset();
403     }
404
405
406     /**
407      * Calculates form values based on the new state value.
408      *
409      * @param event the event caused by the form change
410      */

411     public void stateChanged(ValueChangeEvent event) {
412
413         FacesContext context = FacesContext.getCurrentInstance();
414         FacesMessage msg;
415         UIComponent component = event.getComponent();
416         String JavaDoc newValue = (String JavaDoc) event.getNewValue();
417
418         state.setImage(LinkedFormElement.IMAGE_PROGRESS);
419
420         //check for null pointers
421
if (newValue == null) {
422             if ((newState == null) || ("".equals(newState))) {
423                 return;
424             } else {
425                 newState = "";
426                 return;
427             }
428         } else {
429             if (newValue.equals(newState)) {
430                 return;
431             }
432         }
433
434         //set proper capitalization
435
newState = newValue.toUpperCase();
436
437         //process state against zip
438
boolean cityAutoFilled = false;
439         MatchZip zipDb;
440
441         if ((null != newZip) && (newZip.length() > 0)) {
442             //see if we get a valid zip
443
zipDb = dataBean.getZip(newZip);
444
445             if (zipDb != null) {
446
447                 //state must match zip to be valid
448
if (!zipDb.getState().equals(newState)) {
449                     msg = new FacesMessage("State not found with Zip "
450                                            + newZip + ". Our best guess is "
451                                            + zipDb.getState());
452                     state.setImage(LinkedFormElement.IMAGE_ALERT);
453                     context.addMessage(component.getClientId(context), msg);
454                     return;
455                 } else {
456                     if (newCity.length() == 0) {
457                         //we can autofill the city
458
newCity = zipDb.getCity();
459                         cityAutoFilled = true;
460                     }
461                 }
462             }
463         }
464
465         //process state against city
466
if (!cityAutoFilled && newCity.length() > 0) {
467             //see if we get a valid city
468
ArrayList JavaDoc cities = dataBean.getCity(newCity);
469
470             if (cities != null) {
471
472                 //check each city for closest matching state
473
TreeMap JavaDoc states = new TreeMap JavaDoc();
474                 Iterator JavaDoc itor = cities.iterator();
475
476                 while (itor.hasNext()) {
477                     MatchCity thisCity = (MatchCity) itor.next();
478                     states.put(thisCity.getState(), thisCity);
479                 }
480                 MatchCity bestGuessCity =
481                         (MatchCity) dataBean.getClosestMatch(newState, states);
482
483                 //exact match
484
if (bestGuessCity.isMatch()) {
485
486                     if (newZip.length() == 0) {
487                         //autofill zips if there's only one of them
488
autoFillZip(bestGuessCity);
489                     }
490                 } else {
491                     //state and city don't match
492
msg = new FacesMessage("State not valid for "
493                                            + newCity + ". Our best guess is "
494                                            + bestGuessCity.getState());
495                     state.setImage(LinkedFormElement.IMAGE_ALERT);
496                     context.addMessage(component.getClientId(context), msg);
497                 }
498             }
499         }
500
501         if (((newZip == null) || (newZip.length() == 0))
502             && ((newCity == null) || (newCity.length() == 0))) {
503             //no zip or city, so check for best match
504
MatchState possibleState = dataBean.getClosestState(newState);
505
506             //exact match
507
if (possibleState.isMatch()) {
508                 newState = possibleState.getState();
509
510                 //build a city drop-down selection list
511
String JavaDoc cities[] = possibleState.getCitiesAsStrings();
512                 ArrayList JavaDoc citySelect = new ArrayList JavaDoc(cities.length + 1);
513                 citySelect.add(NOT_IN_LIST);
514                 for (int i = 0; i < cities.length; i++) {
515                     citySelect.add(cities[i]);
516                 }
517                 //activate the city drop-down selection
518
city.setSelect(citySelect);
519                 city.setSelectRendered(true);
520                 ((HtmlSelectOneMenu) selectCity).requestFocus();
521             } else {
522                 // Don't have exact match so fill in our best guess;
523
msg = new FacesMessage("State is not valid. Our best guess is "
524                                        + possibleState.getState());
525                 state.setImage(LinkedFormElement.IMAGE_ALERT);
526                 context.addMessage(component.getClientId(context), msg);
527             }
528         }
529     }
530
531     /**
532      * Calculates form values based on the new selected state value.
533      *
534      * @param event the event caused by the form change
535      */

536     public void stateSelectChanged(ValueChangeEvent event) {
537
538         FacesContext context = FacesContext.getCurrentInstance();
539         FacesMessage msg;
540         UIComponent component = event.getComponent();
541         String JavaDoc newValue = (String JavaDoc) event.getNewValue();
542
543         state.setImage(LinkedFormElement.IMAGE_PROGRESS);
544
545         if ((newValue == null) || "".equals(newValue)) {
546             return;
547         }
548
549         if (newValue.equals(NOT_IN_LIST)) {
550             //revert to blank text input field
551
newState = "";
552         } else {
553             newState = newValue;
554         }
555         state.setSelectRendered(false);
556
557         //process state against zip
558
MatchZip zipDb;
559
560         if ((null != newZip) && (newZip.length() > 0)) {
561             //see if we get a valid zip
562
zipDb = dataBean.getZip(newZip);
563
564             if (zipDb != null) {
565
566                 //state must match zip to be valid
567
if (!zipDb.getState().equals(newState)) {
568                     msg = new FacesMessage("State not found with Zip "
569                                            + newZip + ". Our best guess is "
570                                            + zipDb.getState());
571                     state.setImage(LinkedFormElement.IMAGE_ALERT);
572                     context.addMessage(component.getClientId(context), msg);
573                 }
574             }
575         } else {
576             //have city and state, so see if we can autofill zip
577
MatchState stateDb = dataBean.getState(newState);
578             if (stateDb != null) {
579                 MatchCity cityDb = stateDb.getCity(newCity);
580                 if (cityDb != null) {
581                     autoFillZip(cityDb);
582                 }
583             }
584         }
585
586         state.reset();
587     }
588
589     /**
590      * Calculates form values based on the new zip value.
591      *
592      * @param event the event caused by the form change
593      */

594     public void zipChanged(ValueChangeEvent event) {
595
596         FacesContext context = FacesContext.getCurrentInstance();
597         FacesMessage msg;
598         UIComponent component = event.getComponent();
599         String JavaDoc newValue = (String JavaDoc) event.getNewValue();
600
601         zip.setImage(LinkedFormElement.IMAGE_PROGRESS);
602
603         if (newValue == null) {
604             if ((newZip == null) || ("".equals(newZip))) {
605                 return;
606             } else {
607                 newZip = "";
608                 return;
609             }
610         } else {
611             if (newValue.equals(newZip)) {
612                 return;
613             }
614             if (newValue.length() < 1) {
615                 //if zip is blank do nothing
616
newZip = "";
617                 return;
618             }
619         }
620
621         newValue = newValue.toUpperCase();
622         newZip = newValue;
623
624         //process zip against state
625
boolean cityAutoFilled = false;
626         MatchState stateDb;
627
628         if (newState.length() > 0) {
629             //see if we get a valid state
630
stateDb = dataBean.getState(newState);
631
632             if (stateDb != null) {
633                 //zip must match state to be valid
634
MatchZip possibleZip = stateDb.getClosestZip(newValue);
635
636                 if (!possibleZip.isMatch()) {
637                     //zip doesn't match state
638
msg = new FacesMessage("Zip not valid in "
639                                            + newState + ". Our best guess is "
640                                            + possibleZip.getZip() + " ("
641                                            + possibleZip.getCity() + ")");
642                     zip.setImage(LinkedFormElement.IMAGE_ALERT);
643                     context.addMessage(component.getClientId(context), msg);
644                     return;
645                 } else {
646                     //state and zip match
647
if (newCity.length() == 0) {
648                         //we can auto fill the city;
649
newCity = possibleZip.getCity();
650                         cityAutoFilled = true;
651                     }
652                 }
653             }
654         }
655
656         //process zip against city
657
if (!cityAutoFilled && newCity.length() > 0) {
658             //see if we get a valid city
659
ArrayList JavaDoc cities = dataBean.getCity(newCity);
660             if (cities != null) {
661
662                 //check each city for closest matching zip
663
TreeMap JavaDoc zips = new TreeMap JavaDoc();
664
665                 Iterator JavaDoc itor = cities.iterator();
666
667                 while (itor.hasNext()) {
668                     MatchCity thisCity = (MatchCity) itor.next();
669                     MatchZip thisZip = thisCity.getClosestZip(newValue);
670                     zips.put(thisZip.getZip(), thisZip);
671                 }
672
673                 MatchZip bestGuessZip =
674                         (MatchZip) dataBean.getClosestMatch(newValue, zips);
675
676                 if (bestGuessZip.isMatch()) {
677                     //zip and city match
678
newZip = newValue;
679
680                     if (newState.length() == 0) {
681                         //autofill states if it is blank;
682
newState = bestGuessZip.getState();
683                     }
684                 } else {
685                     //zip and city don't match
686
msg = new FacesMessage("Zip not valid for "
687                                            + newCity + ". Our best guess is "
688                                            + bestGuessZip.getZip());
689
690                     zip.setImage(LinkedFormElement.IMAGE_ALERT);
691                     context.addMessage(component.getClientId(context), msg);
692                     return;
693                 }
694             }
695         }
696
697         if (newState.length() == 0 && newCity.length() == 0) {
698             //no state or city, so check for best match
699
MatchZip possibleZip = dataBean.getClosestZip(newValue);
700
701             if (possibleZip.isMatch()) {
702                 //exact zip match, so set zip, state, and city
703
newZip = possibleZip.getZip();
704                 newState = possibleZip.getState();
705                 newCity = possibleZip.getCity();
706
707             } else {
708                 //don't have exact match so fill in our best guess
709
msg = new FacesMessage(
710                         "Zip is not valid. Our best guess is "
711                         + possibleZip.getZip() + " ("
712                         + possibleZip.getCity() + ", "
713                         + possibleZip.getState() + ")");
714
715                 zip.setImage(LinkedFormElement.IMAGE_ALERT);
716                 context.addMessage(component.getClientId(context), msg);
717             }
718         }
719     }
720
721
722     /**
723      * Calculates form values based on the new selected zip value.
724      *
725      * @param event the event caused by the form change
726      */

727     public void zipSelectChanged(ValueChangeEvent event) {
728
729         String JavaDoc newValue = (String JavaDoc) event.getNewValue();
730         zip.setImage(LinkedFormElement.IMAGE_PROGRESS);
731
732         if ((newValue == null) || "".equals(newValue)) {
733             newZip = "";
734             return;
735         }
736
737         if (newValue.equals(NOT_IN_LIST)) {
738             //revert to blank text input
739
newZip = "";
740         } else {
741             newZip = newValue;
742         }
743         if (!NOT_IN_LIST.equals(newValue)) {
744             zip.setSelectRendered(false);
745         }
746
747         zip.setImage(LinkedFormElement.IMAGE_BLANK);
748         zip.reset();
749     }
750
751     /**
752      * Generates a list of zip codes for the provided city and either fills in
753      * the zip field (one zip) or populates and enables the zip drop-down menu
754      * (multiple zips).
755      *
756      * @param city the city of which to check for zips
757      */

758     private void autoFillZip(MatchCity city) {
759
760         String JavaDoc myZips[] = city.getZipsAsStrings();
761
762
763         if (myZips.length == 1) {
764             //only one zip
765
newZip = myZips[0];
766
767             //disable drop-down menu
768
zip.setSelectRendered(false);
769         } else {
770             //multiple zips
771
ArrayList JavaDoc zipSelect = new ArrayList JavaDoc(myZips.length + 1);
772             zipSelect.add(NOT_IN_LIST);
773             for (int i = 0; i < myZips.length; i++) {
774                 zipSelect.add(myZips[i]);
775
776             }
777             //enable drop-down menu
778
zip.setSelect(zipSelect);
779             zip.setSelectRendered(true);
780             ((HtmlSelectOneMenu) selectZip).requestFocus();
781         }
782     }
783
784     /**
785      * Determines whether the city, state, and zip values describe an entry in
786      * the database.
787      *
788      * @return whether or not the address is valid
789      * @see SubmitButton, PhaseSync
790      */

791     public boolean isAddressValid() {
792
793         MatchZip zipDb = dataBean.getZip(zip.getValue());
794
795         return (zipDb != null && zipDb.getCity().equals(city.getValue()) &&
796                 zipDb.getState().equals(state.getValue()));
797     }
798
799     /**
800      * Removes extra whitespace and capitalizes the first letter of each word in
801      * the provided string.
802      *
803      * @param inString
804      * @return the string with proper capitalization
805      */

806     public static String JavaDoc fixCapitalization(String JavaDoc inString) {
807
808         StringBuffer JavaDoc str = new StringBuffer JavaDoc(inString.trim().toLowerCase());
809
810         //empty string
811
if (str.length() == 0) {
812             return str.toString();
813         }
814         Character JavaDoc nextChar;
815         int i = 0;
816         nextChar = new Character JavaDoc(str.charAt(i));
817
818         while (i < str.length()) {
819             //capitalize the first character
820
str.setCharAt(i++, Character.toUpperCase(nextChar.charValue()));
821
822             if (i == str.length()) {
823                 return str.toString();
824             }
825
826             //look for whitespace
827
nextChar = new Character JavaDoc(str.charAt(i));
828             while (i < str.length() - 2
829                    && !Character.isWhitespace(nextChar.charValue())) {
830                 nextChar = new Character JavaDoc(str.charAt(++i));
831             }
832
833             if (!Character.isWhitespace(nextChar.charValue())) {
834                 //not whitespace, we must be at end of string
835
return str.toString();
836             }
837
838             //remove all but first whitespace
839
nextChar = new Character JavaDoc(str.charAt(++i));
840             while (i < str.length()
841                    && Character.isWhitespace(nextChar.charValue())) {
842                 str.deleteCharAt(i);
843                 nextChar = new Character JavaDoc(str.charAt(i));
844             }
845         }
846         return str.toString();
847     }
848
849
850     public UISelectOne getSelectState() {
851         return selectState;
852     }
853
854
855     public void setSelectState(UISelectOne selectState) {
856         this.selectState = selectState;
857     }
858
859     public HtmlInputText getCityText() {
860         return cityText;
861     }
862
863     public void setCityText(HtmlInputText cityText) {
864         this.cityText = cityText;
865     }
866
867     public HtmlInputText getFirstNameText() {
868         return firstNameText;
869     }
870
871     public void setFirstNameText(HtmlInputText firstNameText) {
872         this.firstNameText = firstNameText;
873     }
874
875     public HtmlInputText getLastNameText() {
876         return lastNameText;
877     }
878
879     public void setLastNameText(HtmlInputText lastNameText) {
880         this.lastNameText = lastNameText;
881     }
882
883     public UISelectOne getSelectCity() {
884         return selectCity;
885     }
886
887     public void setSelectCity(UISelectOne selectCity) {
888         this.selectCity = selectCity;
889     }
890
891     public UISelectOne getSelectTitle() {
892         return selectTitle;
893     }
894
895     public void setSelectTitle(UISelectOne selectTitle) {
896         this.selectTitle = selectTitle;
897     }
898
899     public UISelectOne getSelectZip() {
900         return selectZip;
901     }
902
903     public void setSelectZip(UISelectOne selectZip) {
904         this.selectZip = selectZip;
905     }
906
907     public HtmlInputText getStateText() {
908         return stateText;
909     }
910
911     public void setStateText(HtmlInputText stateText) {
912         this.stateText = stateText;
913     }
914
915     public HtmlInputText getZipText() {
916         return zipText;
917     }
918
919     public void setZipText(HtmlInputText zipText) {
920         this.zipText = zipText;
921     }
922
923 }
Popular Tags