View Javadoc

1   /*
2    * Copyright 1999-2002,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  
18  package org.apache.struts.webapp.example2;
19  
20  
21  import java.io.IOException;
22  import javax.faces.FacesException;
23  import javax.faces.context.FacesContext;
24  
25  
26  /***
27   * <p>Backing bean for the <code>registration.jsp</code> page.</p>
28   */
29  
30  public class RegistrationBacking {
31  
32  
33      // -------------------------------------------------------------- Properties
34  
35  
36      // These methods exist to work around a bug in the PFD version of the
37      // rendering for <h:data_table> that disallows constant values on
38      // per-row command and output components
39      public String getDeleteLabel() { return ("Delete"); }
40      public String getEditLabel() { return ("Edit"); }
41  
42  
43      // ----------------------------------------------------------------- Actions
44  
45  
46      /***
47       * <p>Create a new subscription.</p>
48       */
49      public String create() {
50  
51          FacesContext context = FacesContext.getCurrentInstance();
52          StringBuffer url = base(context);
53          url.append("?action=Create");
54          url.append("&username=");
55          User user = (User)
56              context.getExternalContext().getSessionMap().get("user");
57          url.append(user.getUsername());
58          forward(context, url.toString());
59          return (null);
60  
61      }
62  
63  
64      /***
65       * <p>Delete an existing subscription.</p>
66       */
67      public String delete() {
68  
69          FacesContext context = FacesContext.getCurrentInstance();
70          StringBuffer url = base(context);
71          url.append("?action=Delete");
72          url.append("&username=");
73          User user = (User)
74              context.getExternalContext().getSessionMap().get("user");
75          url.append(user.getUsername());
76          url.append("&host=");
77          Subscription subscription = (Subscription)
78              context.getExternalContext().getRequestMap().get("subscription");
79          url.append(subscription.getHost());
80          forward(context, url.toString());
81          return (null);
82  
83      }
84  
85  
86      /***
87       * <p>Edit an existing subscription.</p>
88       */
89      public String edit() {
90  
91          FacesContext context = FacesContext.getCurrentInstance();
92          StringBuffer url = base(context);
93          url.append("?action=Edit");
94          url.append("&username=");
95          User user = (User)
96              context.getExternalContext().getSessionMap().get("user");
97          url.append(user.getUsername());
98          url.append("&host=");
99          Subscription subscription = (Subscription)
100             context.getExternalContext().getRequestMap().get("subscription");
101         url.append(subscription.getHost());
102         forward(context, url.toString());
103         return (null);
104 
105     }
106 
107 
108     // --------------------------------------------------------- Private Methods
109 
110 
111     /***
112      * <p>Return the context relative base URL for the "edit subscriptions"
113      * action.</p>
114      *
115      * @param context <code>FacesContext</code> for the current request
116      */
117     private StringBuffer base(FacesContext context) {
118 
119         // FIXME - assumes extension mapping for Struts
120         return (new StringBuffer("/editSubscription.do"));
121 
122     }
123 
124 
125     /***
126      * <p>Forward to the specified URL and mark this response as having
127      * been completed.</p>
128      *
129      * @param context <code>FacesContext</code> for the current request
130      * @param url Context-relative URL to forward to
131      *
132      * @exception FacesException if any error occurs
133      */
134     private void forward(FacesContext context, String url) {
135 
136         try {
137             context.getExternalContext().dispatch(url);
138         } catch (IOException e) {
139             throw new FacesException(e);
140         } finally {
141             context.responseComplete();
142         }
143 
144     }
145 
146 
147 }