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.remoting;
19  
20  import java.io.IOException;
21  import java.util.Map;
22  
23  import javax.faces.context.FacesContext;
24  import javax.faces.context.ResponseWriter;
25  import javax.faces.model.SelectItem;
26  import org.apache.shale.remoting.faces.ResponseFactory;
27  import org.apache.shale.usecases.view.Domains;
28  import org.apache.shale.view.AbstractFacesBean;
29  
30  /***
31   * <p>Remotely executable business logic methods that complete the entire
32   * response.</p>
33   *
34   * $Id: Business.java 476661 2006-11-18 23:44:11Z craigmcc $
35   */
36  public class Business extends AbstractFacesBean {
37  
38      // ---------------------------------------------------------- Public Methods
39  
40     /***
41      * <p>Generate an XML response from an array of JSF SelectItems. Those
42      *    items represent a city/state pair that matches a zip code. The
43      *    zip code is stored in the <i>zip</i> request parameter. That
44      *    parameter was put in request scope by an Ajax call--see zipCode.jsp
45      *    for more details. When the XML response is complete, the 
46      *    <code>protected</code> method <code>selectItems</code> invokes
47      *    <code>responseComplete()</code> on the Faces context, which ends
48      *    the response.</p>
49      */
50     public void cityAndStateForZip() throws IOException {
51  
52         FacesContext context = getFacesContext();
53         Map requestParams = getRequestParameterMap();
54         String zip = (String)requestParams.get("zip");
55         Domains domains = (Domains) getBean("domains");
56  
57         if (zip != null)
58           selectItems(context, domains.getCityAndStateItems(zip));
59         else
60           selectItems(context, domains.getBlankCityAndStateItems());
61  
62     }
63  
64      /***
65       * <p>Return the set of reported categories.</p>
66       */
67      public void listCategories() throws IOException {
68  
69          FacesContext context = getFacesContext();
70          selectItems(context, supportedCategories(context));
71  
72      }
73  
74  
75      /***
76       * <p>Return the set of reported locales.</p>
77       */
78      public void listLocales() throws IOException {
79  
80          FacesContext context = getFacesContext();
81          selectItems(context, supportedLocales(context));
82  
83      }
84  
85  
86  
87      // ------------------------------------------------------- Protected Methods
88  
89  
90      /***
91       * <p>Render an XML document containing the specified selection items
92       * as the response to this request.</p>
93       *
94       * @param context <code>FacesContext</code> for the current request
95       * @param items Selection items to be rendered
96       */
97      protected void selectItems(FacesContext context, SelectItem items[]) throws IOException {
98  
99          ResponseWriter writer =
100                 (new ResponseFactory()).getResponseWriter(context, "text/xml; charset=UTF-8");
101 
102         // Generate the response content
103         writer.startDocument();
104         writer.startElement("items", null);
105         writer.write("\n");
106         for (int i = 0; i < items.length; i++) {
107             writer.startElement("item", null);
108             writer.write("\n");
109             Object value = items[i].getValue();
110             if (value != null) {
111                 writer.startElement("value", null);
112                 writer.writeText(value, null);
113                 writer.endElement("value");
114                 writer.write("\n");
115             }
116             String label = items[i].getLabel();
117             if (label != null) {
118                 writer.startElement("label", null);
119                 writer.writeText(label, null);
120                 writer.endElement("label");
121                 writer.write("\n");
122             }
123             String description = items[i].getLabel();
124             if (description != null) {
125                 writer.startElement("description", null);
126                 writer.writeText(description, null);
127                 writer.endElement("description");
128                 writer.write("\n");
129             }
130             writer.endElement("item");
131             writer.write("\n");
132         }
133         writer.endElement("items");
134         writer.write("\n");
135 
136         context.responseComplete();
137 
138     }
139 
140 
141     /***
142      * <p>Return the set of legal supported categories.</p>
143      *
144      * @param context <code>FacesContext</code> for the current request
145      */
146     protected SelectItem[] supportedCategories(FacesContext context) {
147 
148         Domains domains = (Domains) getBean("domains");
149         return domains.getSupportedCategories(context.getViewRoot().getLocale());
150 
151     }
152 
153 
154     /***
155      * <p>Return the set of legal supported locales.</p>
156      *
157      * @param context <code>FacesContext</code> for the current request
158      */
159     protected SelectItem[] supportedLocales(FacesContext context) {
160 
161         Domains domains = (Domains) getBean("domains");
162         return domains.getSupportedLocales(context.getViewRoot().getLocale());
163 
164     }
165 
166 
167 }