2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shale.usecases.view;
19  
20  import java.util.ArrayList;
21  import java.util.Arrays;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.List;
25  import java.util.Locale;
26  import java.util.Map;
27  import javax.faces.FactoryFinder;
28  import javax.faces.application.Application;
29  import javax.faces.application.ApplicationFactory;
30  import javax.faces.context.FacesContext;
31  import javax.faces.model.SelectItem;
32  import org.apache.shale.util.Messages;
33  
34  /***
35   * <p>Utility class to return locale-specific domains (lists of selection items)
36   * based on the current <code>Locale</code> of this request.  An instance of
37   * this class will typically be defined as an application scope managed bean,
38   * so that it is instantiated on demand.</p>
39   *
40   * $Id: Domains.java 464373 2006-10-16 04:21:54Z rahul $
41   */
42  public class Domains {
43  
44     // -------------------------------------------------------- Constants
45      /***
46       * <p>The key for the first item in the zip code pull-down menu</p>
47       */
48      private static final String PICK_KEY = "ajax.pick";
49  	
50       /***
51        * <p>Select items for the zip code pull-down menu</p>
52        */
53      private static final SelectItem[] zipCodeItems = {
54        new SelectItem(PICK_KEY),
55        new SelectItem("14214"),
56        new SelectItem("80632"),
57        new SelectItem("97402"),     
58      };
59  
60      /***
61       * <p>Select items representing legal zip codes</p>
62       */
63      private static final SelectItem[] legalZipCodeItems = {
64         // zipCodeItems[0] is not a legal zip code
65         zipCodeItems[1], // Buffalo
66         zipCodeItems[2], // Greeley
67         zipCodeItems[3]  // Euguene
68      };
69  
70      /***
71       * <p>Select items representing City/State pairs corresponding,
72       *    in order, to the items in <code>zipCodeItems</code></p>
73       */
74      private static final SelectItem[][] cityAndStateItems = {
75         { new SelectItem("Buffalo"),  new SelectItem("New York") },
76         { new SelectItem("Greeley"),  new SelectItem("Colorado") },
77         { new SelectItem("Eugene"),   new SelectItem("Oregon")   }
78      }; 
79  
80     /***
81      * <p>Select items for a blank city and state. This array is
82      *    used if the user selects an item that's not in the
83      *    <code>legalZipCodeItems</code> array.</p>
84      */
85      private static final SelectItem[] blankCityAndStateItems = { 
86         new SelectItem(""),
87         new SelectItem("")
88      };
89  
90  
91     // -------------------------------------------------------- Static Variables
92  
93      /***
94       * <p>Localized messages for this class.</p>
95       */
96      private static Messages messages =
97       new Messages("org.apache.shale.usecases.view.Bundle");
98        
99  
100     // ------------------------------------------------------ Instance Variables
101 
102 
103     /***
104      * <p><code>Map</code> containing arrays of <code>SelectItem</code>s
105      * representing the supported message categories for this application,
106      * keyed by the Locale in which the descriptions have been localized.</p>
107      */
108     private Map categories = new HashMap();
109 
110 
111     /***
112      * <p><code>Map</code> containing arrays of <code>SelectItem</code>s
113      * representing the locales supported by this application, keyed by
114      * the Locale in which the descriptions have been localized.</p>
115      */
116     private Map locales = new HashMap();
117 
118 
119     /***
120      * <p>Array of <code>SelectItem</code> representing the abbreviations
121      * and descriptions of all valid US states.</p>
122      */
123     private SelectItem[] states = {
124         new SelectItem("AL", "Alabama"),
125         new SelectItem("AK", "Alaska"),
126         new SelectItem("AZ", "Arizona"),
127         new SelectItem("AR", "Arkansas"),
128         new SelectItem("CA", "California"),
129         new SelectItem("CO", "Colorado"),
130         new SelectItem("CT", "Connecticut"),
131         new SelectItem("DE", "Delaware"),
132         new SelectItem("DC", "District of Columbia"),
133         new SelectItem("FL", "Florida"),
134         new SelectItem("GA", "Georgia"),
135         new SelectItem("HI", "Hawaii"),
136         new SelectItem("ID", "Idaho"),
137         new SelectItem("IL", "Illinois"),
138         new SelectItem("IN", "Indiana"),
139         new SelectItem("IA", "Iowa"),
140         new SelectItem("KS", "Kansas"),
141         new SelectItem("KY", "Kentucky"),
142         new SelectItem("LA", "Louisiana"),
143         new SelectItem("ME", "Maine"),
144         new SelectItem("MD", "Maryland"),
145         new SelectItem("MA", "Massachusetts"),
146         new SelectItem("MI", "Michigan"),
147         new SelectItem("MN", "Minnesota"),
148         new SelectItem("MS", "Mississippi"),
149         new SelectItem("MO", "Missouri"),
150         new SelectItem("MT", "Montana"),
151         new SelectItem("NE", "Nebraska"),
152         new SelectItem("NV", "Nevada"),
153         new SelectItem("NH", "New Hampshire"),
154         new SelectItem("NJ", "New Jersey"),
155         new SelectItem("NM", "New Mexico"),
156         new SelectItem("NY", "New York"),
157         new SelectItem("NC", "North Carolina"),
158         new SelectItem("ND", "North Dakota"),
159         new SelectItem("OH", "Ohio"),
160         new SelectItem("OK", "Oklahoma"),
161         new SelectItem("OR", "Oregon"),
162         new SelectItem("PA", "Pennyslvania"),
163         new SelectItem("RI", "Rhode Island"),
164         new SelectItem("SC", "South Carolina"),
165         new SelectItem("SD", "South Dakota"),
166         new SelectItem("TN", "Tennessee"),
167         new SelectItem("TX", "Texas"),
168         new SelectItem("UT", "Utah"),
169         new SelectItem("VT", "Vermont"),
170         new SelectItem("VA", "Virginia"),
171         new SelectItem("WA", "Washington"),
172         new SelectItem("WV", "West Virginia"),
173         new SelectItem("WI", "Wisconsin"),
174         new SelectItem("WY", "Wyoming")
175     };
176 
177 
178     /***
179      * <p>Array of state names in alphabetical order (lazily instantiated).</p>
180      */
181     private String[] stateNames = null;
182 
183 
184     // -------------------------------------------------------------- Properties
185 
186 
187     /***
188      * <p>Return an array of selection items representing valid US state
189      * abbreviations and descriptions.</p>
190      */
191     public SelectItem[] getStates() {
192 
193         return this.states;
194 
195     }
196 
197 
198     /***
199      * <p>Return an array of state names in alphabetical order.</p>
200      */
201     public String[] getStateNames() {
202 
203         if (stateNames == null) {
204             stateNames = new String[states.length];
205             for (int i = 0; i < stateNames.length; i++) {
206                 stateNames[i] = states[i].getLabel();
207             }
208             Arrays.sort(stateNames);
209    }
210         return stateNames;
211 
212     }
213 
214 
215     /***
216      * <p>Return an array of selection items representing the message categories
217      * supported by this application, with the labels localized based on the
218      * <code>Locale</code> of the current request.</p>
219      */
220     public SelectItem[] getSupportedCategories() {
221 
222         // Return any previously cached array for this request locale
223         Locale locale =
224           FacesContext.getCurrentInstance().getViewRoot().getLocale();
225         return getSupportedCategories(locale);
226 
227     }
228 
229 
230     /***
231      * <p>Return an array of selection items representing the message categories
232      * supported by this application, with the labels localized based on the
233      * specified <code>Locale</code>.</p>
234      */
235     public SelectItem[] getSupportedCategories(Locale locale) {
236 
237         // Return any previously cached array for the specfiied locale
238         SelectItem items[] = null;
239         synchronized (categories) {
240             items = (SelectItem[]) categories.get(locale);
241             if (items != null) {
242                 return items;
243             }
244         }
245 
246         // Construct and cache a new array, before returning it
247         SelectItem item = null;
248         List list = new ArrayList();
249         int id = 0;
250         String label = null;
251         while (true) {
252             label = messages.getMessage("category." + id, locale);
253             if (label == null) {
254                 break;
255             }
256             list.add(new SelectItem(new Integer(id), label));
257             id++;
258         }
259         items = (SelectItem[]) list.toArray(new SelectItem[list.size()]);
260         synchronized(categories) {
261             categories.put(locale, items);
262         }
263         return items;
264 
265     }
266 
267 
268     /***
269      * <p>Return an array of selection items representing the locales supported
270      * by this application, with the labels localized based on the
271      * <code>Locale</code> of the current request.</p>
272      */
273     public SelectItem[] getSupportedLocales() {
274 
275         Locale locale =
276           FacesContext.getCurrentInstance().getViewRoot().getLocale();
277         return getSupportedLocales(locale);
278 
279     }
280 
281     
282     /***
283      * <p>Return an array of selection items representing the locales supported
284      * by this application, with the labels localized based on the specified
285      * <code>Locale</code>.</p>
286      *
287      * @param locale <code>Locale</code> used to localize the labels
288      */
289     public SelectItem[] getSupportedLocales(Locale locale) {
290 
291         // Return any previously cached array for the specified locale
292         SelectItem items[] = null;
293         synchronized (locales) {
294             items = (SelectItem[]) locales.get(locale);
295             if (items != null) {
296                 return items;
297             }
298         }
299 
300         // Construct and cache a new array, before returning it
301         SelectItem item = null;
302         List list = new ArrayList();
303         ApplicationFactory afactory = (ApplicationFactory)
304           FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
305         Application application = afactory.getApplication();
306         Iterator supporteds = application.getSupportedLocales();
307         while (supporteds.hasNext()) {
308             Locale supported = (Locale) supporteds.next();
309             item = new SelectItem(supported.toString(),
310                                   messages.getMessage("locale." + supported.toString(),
311                                                       locale));
312             list.add(item);
313         }
314         items = (SelectItem[]) list.toArray(new SelectItem[list.size()]);
315         synchronized(locales) {
316             locales.put(locale, items);
317         }
318         return items;
319 
320 
321     }
322 
323 
324    /***
325     * <p>Return an array of selection items representing the zip codes
326     * shown in the drop-down menu in <code>zipCode.jsp</code>.</p>
327     *
328     */
329    public SelectItem[] getZipCodeItems() {
330 	
331 	   SelectItem pickItem = zipCodeItems[0];
332 	   if (PICK_KEY.equals(pickItem.getLabel()))
333          pickItem.setLabel(messages.getMessage(PICK_KEY,
334 		                                         FacesContext.getCurrentInstance()
335 		                                                     .getViewRoot()
336 		                                                     .getLocale()));
337       return zipCodeItems;
338 
339    }
340 
341 
342    /***
343     * <p>Return an array of selection items representing the city/state
344     *    pair, given a zip code.</p>
345     *
346     * @param zip <code>String</code> the zip code
347     */
348    public SelectItem[] getCityAndStateItems(String zip) {
349 	
350       for(int i=0; i < legalZipCodeItems.length; ++i) {
351          if(legalZipCodeItems[i].getLabel().equals(zip))
352             return cityAndStateItems[i];
353       }
354       return blankCityAndStateItems;
355 
356    }
357 
358 
359    /***
360     * <p>Return an array of selection items with two select items. Both
361     *    items have empty strings for labels. Those items are
362     *    used when an illegal value is entered for a zip code.</p>
363     *
364     */
365    public SelectItem[] getBlankCityAndStateItems() {
366 	
367       return blankCityAndStateItems;
368 
369    }
370 
371 }