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.examples.mailreaderjpa;
19  
20  import java.util.List;
21  import java.util.Map;
22  import org.apache.mailreaderjpa.Protocol;
23  import org.apache.mailreaderjpa.Subscription;
24  import org.apache.mailreaderjpa.User;
25  import org.apache.shale.view.AbstractViewController;
26  
27  /***
28   * <p>Backing bean for the <code>/detail.jsp</code> view.</p>
29   */
30  public class Detail extends AbstractViewController {
31      
32  
33      // ------------------------------------------------------ Manifest Constants
34  
35  
36      /***
37       * <p>Cache key under which we save the current primary key.</p>
38       */
39      private static final String ID_KEY = "id";
40  
41  
42      /***
43       * <p>Cache key under which we save the current transaction mode.</p>
44       */
45      private static final String MODE_KEY = "mode";
46  
47  
48      /***
49       * <p>Cache key under which we save the <code>Subscription</code> instance
50       * we are creating or editing.</p>
51       */
52      private static final String SUBSCRIPTION_KEY = "user";
53  
54  
55      // ------------------------------------------------------- Public Properties
56  
57  
58      /***
59       * <p>The {@link Domains} for this application.  This value
60       * will be injected, based on the managed bean configuration.</p>
61       */
62      private Domains domains = null;
63  
64  
65      /***
66       * <p>Return the {@link Domains} for this application.</p>
67       */
68      public Domains getDomains() {
69          return this.domains;
70      }
71  
72  
73      /***
74       * <p>Set the {@link Domains} for this application.</p>
75       *
76       * @param domains The new domains instance
77       */
78      public void setDomains(Domains domains) {
79          this.domains = domains;
80      }
81  
82  
83      /***
84       * <p>The primary key of the row to be processed.</p>
85       */
86      private int id = 0;
87  
88  
89      /***
90       * <p>Return the primary key of the row to be processed.</p>
91       */
92      private int getId() {
93          return this.id;
94      }
95  
96  
97      /***
98       * <p>Set the primary key of the row to be processed.</p>
99       *
100      * @param id The new primary key
101      */
102     public void setId(int id) {
103         this.id = id;
104     }
105 
106 
107     /***
108      * <p>The business {@link Logic} for this application.  This value
109      * will be injected, based on the managed bean configuration.</p>
110      */
111     private Logic logic = null;
112 
113 
114     /***
115      * <p>Return the business {@link Logic} for this application.</p>
116      */
117     public Logic getLogic() {
118         return this.logic;
119     }
120 
121 
122     /***
123      * <p>Set the business {@link Logic} for this application.</p>
124      *
125      * @param logic The new business logic instance
126      */
127     public void setLogic(Logic logic) {
128         this.logic = logic;
129     }
130 
131 
132     /***
133      * <p>The transaction mode for the current request (CREATE or EDIT
134      * or DELETE or DISPLAY).  Default mode is DISPLAY.</p>
135      */
136     private String mode = Constants.DISPLAY_MODE;
137 
138 
139     /***
140      * <p>Return the transaction mode (CREATE or EDIT) for this request.</p>
141      */
142     public String getMode() {
143         return this.mode;
144     }
145 
146 
147     /***
148      * <p>Set the transaction mode (CREATE or EDIT) for this request.</p>
149      *
150      * @param mode The new transaction mode
151      */
152     public void setMode(String mode) {
153         this.mode = mode;
154     }
155 
156 
157     /***
158      * <p>Return <code>true</code> if the input fields should be rendered
159      * as read only.</p>
160      */
161     public boolean isReadOnly() {
162         return Constants.DELETE_MODE.equals(getMode())
163             || Constants.DISPLAY_MODE.equals(getMode());
164     }
165 
166 
167     /***
168      * <p>The per-user {@link State} instance we are associated with.  This
169      * value will be injected, based on the managed bean configuration.</p>
170      */
171     private State state = null;
172 
173 
174     /***
175      * <p>Return the per-user {@link State} instance we are associated with.</p>
176      */
177     public State getState() {
178         return this.state;
179     }
180 
181 
182     /***
183      * <p>Set the per-user {@link State} instance we are associated with.</p>
184      *
185      * @param state The new State instance
186      */
187     public void setState(State state) {
188         this.state = state;
189     }
190 
191 
192     /***
193      * <p>The <code>Subscription</code> instance being created or edited.</p>
194      */
195     private Subscription subscription = null;
196 
197 
198     /***
199      * <p>Return the <code>Subscription</code> instance we are creating or editing.</p>
200      */
201     public Subscription getSubscription() {
202         return this.subscription;
203     }
204 
205 
206     // -------------------------------------------------------- Lifecycle Events
207 
208 
209     /***
210      * <p>Initialize primary key and transaction mode from the request URI,
211      * if specified.</p>
212      */
213     public void init() {
214 
215         log("Detail.init(" + isPostBack() + ")");
216 
217         String id = getRequestParameter("id");
218         if (id != null) {
219             try {
220                 setId(Integer.valueOf(id));
221             } catch (NumberFormatException e) {
222                 setId(0);
223             }
224         }
225 
226         String mode = getRequestParameter("mode");
227         if ((mode != null) && (mode.length() > 0)) {
228             setMode(mode);
229         }
230 
231     }
232 
233 
234     /***
235      * <p>Restore the <code>User</code> instance we are creating or editing,
236      * creating a new one if necessary.</p>
237      */
238     public void preprocess() {
239 
240         log("Detail.preprocess(" + isPostBack() + ")");
241 
242         String mode = (String) retrieveData(MODE_KEY);
243         if (mode != null) {
244             setMode(mode);
245         }
246 
247         Subscription subscription = (Subscription) retrieveData(SUBSCRIPTION_KEY);
248         if (subscription != null) {
249             this.subscription = subscription;
250         } else {
251             setup();
252         }
253 
254     }
255 
256 
257     /***
258      * <p>Cache the <code>Subscription</code> instance we are creating or editing,
259      * as well as the transaction model</p>
260      */
261     public void prerender() {
262 
263         log("Detail.prerender(" + isPostBack() + ")");
264 
265         // Create a new user instance if necessary (i.e. we navigated
266         // to this page directly, or we are in CREATE mode)
267         if (getSubscription() == null) {
268             setup();
269         }
270 
271         // Cache the current transaction mode and user instance
272         saveData(MODE_KEY, getMode());
273         saveData(SUBSCRIPTION_KEY, getSubscription());
274 
275     }
276 
277 
278     /***
279      * <p>Release resources used during this request.</p>
280      */
281     public void destroy() {
282 
283         log("Detail.destroy(" + isPostBack() + ")");
284 
285         ; // FIXME - destroy()
286 
287     }
288 
289 
290     // ------------------------------------------------------------- View Events
291 
292 
293     /***
294      * <p>Cancel saving the updated subscription information.</p>
295      */
296     public String cancel() {
297 
298         return "registration";
299 
300     }
301 
302 
303     /***
304      * <p>Save away the updated subscription information.</p>
305      */
306     public String save() {
307 
308         try {
309 
310             if (Constants.CREATE_MODE.equals(getMode())) {
311                 getLogic().createSubscription(getSubscription());
312             } else if (Constants.DELETE_MODE.equals(getMode())) {
313                 getLogic().deleteSubscription(getSubscription());
314             } else if (Constants.EDIT_MODE.equals(getMode())) {
315                 this.subscription = getLogic().updateSubscription(getSubscription());
316             }
317 
318         } catch (Exception e) {
319 
320             error("Exception saving subscription information: " + e.getMessage());
321             log("Exception saving subscription information", e);
322             return null;
323         }
324 
325         return "registration";
326 
327     }
328 
329 
330     // --------------------------------------------------------- Support Methods
331 
332 
333     /***
334      * <p>Set up the <code>Subscription</code> instance we are creating and editing,
335      * by re-finding via the specified primary key (if any), or by creating
336      * a new instance.</p>
337      */
338     private void setup() {
339 
340         if (getId() != 0) {
341             this.subscription = getLogic().findSubscriptionById(getId());
342         } else {
343             this.subscription = new Subscription();
344             this.subscription.setUser(getState().getUser());
345             this.subscription.setProtocol((Protocol) getDomains().getProtocols()[0].getValue());
346         }
347 
348     }
349 
350 
351 }