Package org.simplecart.webapp.actions

Source Code of org.simplecart.webapp.actions.CustomerManageAction

/*
* CustomerManage.java
*
* Created on February 18, 2005, 3:25 PM
*/

package org.simplecart.webapp.actions;

import javax.servlet.http.*;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.*;

import org.apache.commons.beanutils.PropertyUtils;

import org.simplecart.webapp.Constants;
import org.simplecart.webapp.forms.PartyForm;

import org.simplecart.account.Customer;
import org.simplecart.dao.CustomerDAO;
import org.simplecart.exceptions.AuthenticationException;
import org.simplecart.security.CustomerSecurityService;
import org.simplecart.security.SecurityService;

/**
*
* @author Daniel Watrous
*/
public class CustomerManageAction extends DispatchAction {

    private CustomerDAO dao;
    private ActionMessages errors;
    private PartyForm customerForm;

    /**
     */
    public ActionForward dispaly(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        errors = new ActionMessages();
        HttpSession session = request.getSession();

        // try first to find a logged in user object
        Customer customer = (Customer) session.getAttribute(Constants.LOGGED_IN_USER_KEY);

        // if no user is found fail otherwise succeed
        if (customer == null) return mapping.findForward(Constants.DISPLAY_PAGE_KEY);
        else return mapping.findForward(Constants.ERROR_KEY);

    }

    /**
     * Retrieves a collection of <em>Administrator</em> objects and
     * stores them in a collection, then forward along to
     * the display page.
     */
    public ActionForward login(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        HttpSession session = request.getSession();
        String username = (String) PropertyUtils.getSimpleProperty(form,"username");
        String password = (String) PropertyUtils.getSimpleProperty(form,"password");
        ActionMessages errors = new ActionMessages();
        SecurityService securityService = new CustomerSecurityService();
        Customer customer = null;

        // perform authentication
        try {
            customer = (Customer) securityService.authenticate(username, password);
        } catch (AuthenticationException e) {
            if (e.getMessage().equals("Error initializing dao"))
                errors.add(
                        ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("error.database"));
            else if (e.getMessage().equals("Error validating user"))
                errors.add(
                        ActionMessages.GLOBAL_MESSAGE,
                        new ActionMessage("error.login"));
            else errors.add(
                    ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("error.login"));
        }
       
        // Report back any errors, and exit if any
        if (!errors.isEmpty()) {
            this.saveErrors(request, errors);
            return (mapping.getInputForward());
        } else {
            // add user to session
            session.setAttribute(Constants.LOGGED_IN_USER_KEY,customer);
            return mapping.findForward(Constants.SUCCESS_KEY);
        }
    }

}
TOP

Related Classes of org.simplecart.webapp.actions.CustomerManageAction

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.