View Javadoc
1   /*
2    * $Id$
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  
23  package org.apache.struts.webapp.example;
24  
25  
26  import org.apache.struts.apps.mailreader.dao.Subscription;
27  import org.apache.struts.apps.mailreader.dao.User;
28  import org.apache.struts.apps.mailreader.dao.UserDatabase;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import jakarta.enterprise.context.RequestScoped;
33  import jakarta.faces.component.UIData;
34  import jakarta.faces.context.FacesContext;
35  import jakarta.inject.Named;
36  
37  
38  /**
39   * <p>Backing bean for the <code>registration.jsp</code> page.</p>
40   */
41  
42  @Named
43  @RequestScoped
44  public class RegistrationBacking extends AbstractBacking {
45  
46  
47      // -------------------------------------------------------- Static Variables
48  
49  
50      /**
51       * The {@code Log} instance for this class.
52       */
53      private final static Logger LOG =
54          LoggerFactory.getLogger(RegistrationBacking.class);
55  
56  
57      // -------------------------------------------------------------- Properties
58  
59  
60      private UIData table = null;
61  
62  
63      /**
64       * <p>Return the <code>UIData</code> instance we are bound to.</p>
65       */
66      public UIData getTable() {
67  
68          return (this.table);
69  
70      }
71  
72  
73      /**
74       * <p>Set the <code>UIData</code> instance we are bound to.</p>
75       *
76       * @param table The <code>UIData</code> instance
77       */
78      public void setTable(UIData table) {
79  
80          this.table = table;
81  
82      }
83  
84  
85  
86      // ----------------------------------------------------------------- Actions
87  
88  
89      /**
90       * <p>Create a new subscription.</p>
91       */
92      public String create() {
93  
94          LOG.debug("create()");
95          FacesContext context = FacesContext.getCurrentInstance();
96          StringBuilder url = subscription(context);
97          url.append("?action=Create");
98          url.append("&username=");
99          User user = (User)
100             context.getExternalContext().getSessionMap().get("user");
101         url.append(user.getUsername());
102         forward(context, url.toString());
103         return (null);
104 
105     }
106 
107 
108     /**
109      * <p>Delete an existing subscription.</p>
110      */
111     public String delete() {
112 
113         LOG.debug("delete()");
114         FacesContext context = FacesContext.getCurrentInstance();
115         StringBuilder url = subscription(context);
116         url.append("?action=Delete");
117         url.append("&username=");
118         User user = (User)
119             context.getExternalContext().getSessionMap().get("user");
120         url.append(user.getUsername());
121         url.append("&host=");
122         Subscription subscription = (Subscription)
123             context.getExternalContext().getRequestMap().get("subscription");
124         url.append(subscription.getHost());
125         forward(context, url.toString());
126         return (null);
127 
128     }
129 
130 
131     /**
132      * <p>Edit an existing subscription.</p>
133      */
134     public String edit() {
135 
136         LOG.debug("edit()");
137         FacesContext context = FacesContext.getCurrentInstance();
138         StringBuilder url = subscription(context);
139         url.append("?action=Edit");
140         url.append("&username=");
141         User user = (User)
142             context.getExternalContext().getSessionMap().get("user");
143         url.append(user.getUsername());
144         url.append("&host=");
145         Subscription subscription = (Subscription)
146             context.getExternalContext().getRequestMap().get("subscription");
147         url.append(subscription.getHost());
148         forward(context, url.toString());
149         return (null);
150 
151     }
152 
153 
154     /**
155      * <p>Update the subscriptions to reflect any revisions to the
156      * <code>type</code> and <code>autoConnect</code> properties.</p>
157      */
158     public String update() {
159 
160         LOG.debug("update()");
161 
162         FacesContext context = FacesContext.getCurrentInstance();
163 
164         // Updates went directly to the underlying rows, so all we need to do
165         // is save the database
166         try {
167             UserDatabase database = (UserDatabase)
168                 context.getExternalContext().getApplicationMap().
169                 get(Constants.DATABASE_KEY);
170             database.save();
171         } catch (Exception e) {
172             LOG.error("Database save", e);
173         }
174 
175         // Forward back to the edit registration page
176         StringBuilder sb = registration(context);
177         sb.append("?action=Edit");
178         forward(context, sb.toString());
179         return (null);
180 
181     }
182 }