/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: Subscription.java 464373 2006-10-16 04:21:54Z rahul $
*/
package org.apache.shale.examples.mailreader;
import javax.faces.application.FacesMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.apps.mailreader.dao.User;
import org.apache.struts.apps.mailreader.dao.UserDatabase;
/**
* <p><code>ViewController</code> for the <code>subscription</code> page.</p>
*/
public class Subscription extends BaseViewController {
// -------------------------------------------------------- Static Variables
/**
* <p>The log instance for this bean.</p>
*/
private static final Log log = LogFactory.getLog(Subscription.class);
// -------------------------------------------------------------- Properties
/**
* <p>The autoConnect state for this subscription.</p>
*/
private boolean autoConnect = false;
/**
* @return Returns the autoConnect.
*/
public boolean isAutoConnect() {
return this.autoConnect;
}
/**
* @param autoConnect The autoConnect to set.
*/
public void setAutoConnect(boolean autoConnect) {
this.autoConnect = autoConnect;
}
/**
* <p>The host for this subscription.</p>
*/
private String host = null;
/**
* @return Returns the host.
*/
public String getHost() {
return this.host;
}
/**
* @param host The host to set.
*/
public void setHost(String host) {
this.host = host;
}
/**
* <p>The login password for this subscription.</p>
*/
private String password = null;
/**
* @return Returns the password.
*/
public String getPassword() {
return this.password;
}
/**
* @param password The password to set.
*/
public void setPassword(String password) {
this.password = password;
}
/**
* <p>The type for this subscription.</p>
*/
private String type = null;
/**
* @return Returns the type.
*/
public String getType() {
return this.type;
}
/**
* @param type The type to set.
*/
public void setType(String type) {
this.type = type;
}
/**
* <p>The logon username for this subscription.</p>
*/
private String username = null;
/**
* @return Returns the username.
*/
public String getUsername() {
return this.username;
}
/**
* @param username The username to set.
*/
public void setUsername(String username) {
this.username = username;
}
// ---------------------------------------------------------- Event Handlers
/**
* <p>Return to the appropriate page depending on the current mode.</p>
*/
public String cancel() {
if ("CREATE".equals(getState().getMode())) {
return "welcome";
} else {
getState().setMode("EDIT");
return "registration";
}
}
/**
* <p>Create or update the user information.</p>
*/
public String save() {
State state = getState();
UserDatabase database = getUserDatabase();
String mode = state.getMode();
User user = state.getUser();
org.apache.struts.apps.mailreader.dao.Subscription subscription =
user.findSubscription(state.getHost());
if ("CREATE".equals(mode)) {
// Verify that the proposed hostname is not already taken
if (user.findSubscription(host) != null) {
// FIXME - localization
getFacesContext().addMessage("subscription:host",
new FacesMessage("That hostname is already defined"));
return null;
}
// Create a new subscription
subscription = user.createSubscription(host);
Registration registration = (Registration)getBean("registration");
user = getState().getUser();
registration.setFromAddress(user.getFromAddress());
registration.setFullName(user.getFullName());
registration.setPassword(user.getPassword());
registration.setPassword2(user.getPassword());
registration.setReplyToAddress(user.getReplyToAddress());
registration.setSubscriptions(user.getSubscriptions());
registration.setUsername(user.getUsername());
} else if ("DELETE".equals(mode)) {
user.removeSubscription(subscription);
try {
database.save();
} catch (Exception e) {
getFacesContext().addMessage(null,
new FacesMessage(e.getMessage()));
log.error("Database save exception", e);
return null;
}
state.setMode("EDIT");
return "registration";
} else /* if ("EDIT".equals(mode)) */ {
; // No special action required
}
// Copy the remaining properties
subscription.setUsername(username);
subscription.setPassword(password);
subscription.setType(type);
subscription.setAutoConnect(autoConnect);
// Save the updated information to the database
try {
database.save();
} catch (Exception e) {
getFacesContext().addMessage(null,
new FacesMessage(e.getMessage()));
log.error("Database save exception", e);
return null;
}
// Return to the registration page
state.setMode("EDIT");
return "registration";
}
// -------------------------------------------------- ViewController Methods
/**
* <p>If this is not a postack, and we are in DELETE or EDIT mode,
* prepopulate the field values for the subscription update form.</p>
*/
public void prerender() {
State state = getState();
// If we are not in DELETE or EDIT mode, there is nothing to do
if (!"DELETE".equals(state.getMode()) &&
!"EDIT".equals(state.getMode())) {
return;
}
// The first time in, prepopulate our input field values
User user = state.getUser();
org.apache.struts.apps.mailreader.dao.Subscription subscription =
user.findSubscription(state.getHost());
if (!isPostBack()) {
setHost(subscription.getHost());
setUsername(subscription.getUsername());
setPassword(subscription.getPassword());
setType(subscription.getType());
setAutoConnect(subscription.getAutoConnect());
}
}
}