001: /*
002: * $Id: RegistrationBacking.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: package org.apache.struts.webapp.example2;
023:
024: import java.io.IOException;
025: import javax.faces.FacesException;
026: import javax.faces.context.FacesContext;
027:
028: /**
029: * <p>Backing bean for the <code>registration.jsp</code> page.</p>
030: */
031:
032: public class RegistrationBacking {
033:
034: // -------------------------------------------------------------- Properties
035:
036: // These methods exist to work around a bug in the PFD version of the
037: // rendering for <h:data_table> that disallows constant values on
038: // per-row command and output components
039: public String getDeleteLabel() {
040: return ("Delete");
041: }
042:
043: public String getEditLabel() {
044: return ("Edit");
045: }
046:
047: // ----------------------------------------------------------------- Actions
048:
049: /**
050: * <p>Create a new subscription.</p>
051: */
052: public String create() {
053:
054: FacesContext context = FacesContext.getCurrentInstance();
055: StringBuffer url = base(context);
056: url.append("?action=Create");
057: url.append("&username=");
058: User user = (User) context.getExternalContext().getSessionMap()
059: .get("user");
060: url.append(user.getUsername());
061: forward(context, url.toString());
062: return (null);
063:
064: }
065:
066: /**
067: * <p>Delete an existing subscription.</p>
068: */
069: public String delete() {
070:
071: FacesContext context = FacesContext.getCurrentInstance();
072: StringBuffer url = base(context);
073: url.append("?action=Delete");
074: url.append("&username=");
075: User user = (User) context.getExternalContext().getSessionMap()
076: .get("user");
077: url.append(user.getUsername());
078: url.append("&host=");
079: Subscription subscription = (Subscription) context
080: .getExternalContext().getRequestMap().get(
081: "subscription");
082: url.append(subscription.getHost());
083: forward(context, url.toString());
084: return (null);
085:
086: }
087:
088: /**
089: * <p>Edit an existing subscription.</p>
090: */
091: public String edit() {
092:
093: FacesContext context = FacesContext.getCurrentInstance();
094: StringBuffer url = base(context);
095: url.append("?action=Edit");
096: url.append("&username=");
097: User user = (User) context.getExternalContext().getSessionMap()
098: .get("user");
099: url.append(user.getUsername());
100: url.append("&host=");
101: Subscription subscription = (Subscription) context
102: .getExternalContext().getRequestMap().get(
103: "subscription");
104: url.append(subscription.getHost());
105: forward(context, url.toString());
106: return (null);
107:
108: }
109:
110: // --------------------------------------------------------- Private Methods
111:
112: /**
113: * <p>Return the context relative base URL for the "edit subscriptions"
114: * action.</p>
115: *
116: * @param context <code>FacesContext</code> for the current request
117: */
118: private StringBuffer base(FacesContext context) {
119:
120: // FIXME - assumes extension mapping for Struts
121: return (new StringBuffer("/editSubscription.do"));
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: }
|