Package org.apache.shale.examples.mailreader

Source Code of org.apache.shale.examples.mailreader.Registration

/*
* 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: Registration.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.ExpiredPasswordException;
import org.apache.struts.apps.mailreader.dao.Subscription;
import org.apache.struts.apps.mailreader.dao.User;
import org.apache.struts.apps.mailreader.dao.UserDatabase;

/**
* <p><code>ViewController</code> for the <code>registration</code> page.</p>
*/

public class Registration extends BaseViewController {

   
    // -------------------------------------------------------- Static Variables


    /**
     * <p>The log instance for this bean.</p>
     */
    private static final Log log = LogFactory.getLog(Registration.class);


    // -------------------------------------------------------------- Properties


    /**
     * <p>The from address of this user.</p>
     */
    private String fromAddress = null;

    /**
     * @return Returns the fromAddress.
     */
    public String getFromAddress() {
        return this.fromAddress;
    }

    /**
     * @param fromAddress The fromAddress to set.
     */
    public void setFromAddress(String fromAddress) {
        this.fromAddress = fromAddress;
    }


    /**
     * <p>The full name of this user.</p>
     */
    private String fullName = null;

    /**
     * @return Returns the fullName.
     */
    public String getFullName() {
        return this.fullName;
    }

    /**
     * @param fullName The fullName to set.
     */
    public void setFullName(String fullName) {
        this.fullName = fullName;
    }
   
   







    /**
     * <p>The login password for this user.</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 confirmation password for this user.</p>
     */
    private String password2 = null;

    /**
     * @return Returns the password2.
     */
    public String getPassword2() {
        return this.password2;
    }

    /**
     * @param password2 The password2 to set.
     */
    public void setPassword2(String password2) {
        this.password2 = password2;
    }


    /**
     * <p>The reply to address of this user.</p>
     */
    private String replyToAddress = null;

    /**
     * @return Returns the replyToAddress.
     */
    public String getReplyToAddress() {
        return this.replyToAddress;
    }

    /**
     * @param replyToAddress The replyToAddress to set.
     */
    public void setReplyToAddress(String replyToAddress) {
        this.replyToAddress = replyToAddress;
    }


    /**
     * <p>The set of subscriptions for the currently logged in user.
     */
    private Subscription[] subscriptions = null;

    /**
     * @return Returns the subscriptions.
     */
    public Subscription[] getSubscriptions() {
        return this.subscriptions;
    }

    /**
     * @param subscriptions The subscriptions to set.
     */
    public void setSubscriptions(Subscription subscriptions[]) {
        if (log.isTraceEnabled()) {
            if (subscriptions == null) {
                log.trace("Erasing stored subscriptions");
            } else {
                log.trace("Storing " + subscriptions.length + " subscriptions");
            }
        }
        this.subscriptions = subscriptions;
    }


    /**
     * <p>The logon username for this user.</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 {
            return "menu";
        }

    }


    /**
     * <p>Create a new subscription.</p>
     */
    public String create() {

        State state = getState();
        state.setHost(null);
        state.setMode("CREATE");
        if (log.isTraceEnabled()) {
            log.trace("subscription(" + state.getMode() + "," + state.getHost() + ")");
        }
        return "subscription";

    }


    /**
     * <p>Delete an existing subscription.</p>
     */
    public String delete() {

        State state = getState();
        state.setHost(((Subscription) getBean("current")).getHost());
        state.setMode("DELETE");
        if (log.isTraceEnabled()) {
            log.trace("subscription(" + state.getMode() + "," + state.getHost() + ")");
        }
        return "subscription";

    }


    /**
     * <p>Edit an existing subscription.</p>
     */
    public String edit() {

        State state = getState();
        state.setHost(((Subscription) getBean("current")).getHost());
        state.setMode("EDIT");
        if (log.isTraceEnabled()) {
            log.trace("subscription(" + state.getMode() + "," + state.getHost() + ")");
        }
        return "subscription";

    }


    /**
     * <p>Create or update the user information.</p>
     * @throws ExpiredPasswordException
     */
    public String save() throws ExpiredPasswordException {

        UserDatabase database = getUserDatabase();
        String mode = getState().getMode();
        boolean ok = true;
        User user = null;

        if ("CREATE".equals(mode)) {

            // Verify that the proposed username is not already taken
            if (database.findUser(username) != null) {
                // FIXME - localization
                getFacesContext().addMessage("registration:username",
                  new FacesMessage("That username is already taken"));
                ok = false;
            }

            // Verify that the two password values match
            if (!password.equals(password2)) {
                // FIXME - localization
                getFacesContext().addMessage("registration:password2",
                  new FacesMessage("Password values do not match"));
                ok = false;
            }

            // Create a new user with the specified username and password
            // and log the new user on
            if (ok) {
                user = database.createUser(username);
                getState().setUser(user);
            }
           

        } else /* if ("EDIT".equals(mode)) */ {

            // Verify that the two password values match (if entered)
            if ((password != null) && (password.length() > 0) &&
                (password2 != null) && (password2.length() > 0) &&
                !password.equals(password2)) {
                // FIXME - localization
                getFacesContext().addMessage("registration:password2",
                  new FacesMessage("Password values do not match"));
                ok = false;
            }

            // Edit the currently logged on user
            user = getState().getUser();

        }
       
        // Copy the remaining properties
        if (ok) {
            if ((password != null) && (password.length() > 0)) {
                user.setPassword(password);
            }
            user.setFullName(fullName);
            user.setFromAddress(fromAddress);
            user.setReplyToAddress(replyToAddress);
        }

        // 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);
            ok = false;
        }

        // Return to the input page if there were any errors
        if (!ok) {
            return null;
        } else {
            return "menu";
        }

    }


    // -------------------------------------------------- ViewController Methods


    /**
     * <p>If this is a postback, and we are in EDIT mode, retrieve the
     * subscriptions for the currently logged on user.  This is required
     * in case the user clicked one of the Delete or Edit buttons in the
     * table, so that the correct row can be positioned to before calling
     * the event handler.</p>
     */
    public void preprocess() {

        State state = getState();
        if ("EDIT".equals(state.getMode())) {
            setSubscriptions(state.getUser().getSubscriptions());
        }

    }


    /**
     * <p>If this is not a postback, and we are in EDIT mode, prepopulate
     * the field values for the user registration update form and retrieve the
     * subscriptions for the currently logged in user.  (If this is a
     * postback, we will have done this already in <code>init()</code>.)</p>
     */
    public void prerender() {

        State state = getState();

        // If we are not in EDIT mode, there is nothing to do
        if (!"EDIT".equals(state.getMode())) {
            return;
        }

        // The first time in, prepopulate our input field values
        User user = state.getUser();
        if (!isPostBack()) {
            setUsername(user.getUsername()); //FIXME: NPE (after session timeout?)
            setPassword("");
            setPassword2("");
            setFullName(user.getFullName());
            setFromAddress(user.getFromAddress());
            setReplyToAddress(user.getReplyToAddress());

        // Look up the subscriptions for the currently logged on user,
        // if we have not done so already.
        // If we were using a real database, this is where executing the
        // query and opening the result set would occur
            setSubscriptions(user.getSubscriptions());
        }

    }


    /**
     * <p>Release our reference to to the retrieved subscriptions (if any).
     * If we were using a real database, this is where closing the result set
     * would go.</p>
     */
    public void destroy() {
      // TODO this is causing the page not to display the subscriptions
        //setSubscriptions(null);

    }


}
TOP

Related Classes of org.apache.shale.examples.mailreader.Registration

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.