// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
package test;

import java.io.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;

import org.apache.struts.action.*;

public class LoginAction extends Action {
    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws IOException, ServletException {
        LoginForm lgifrm = (LoginForm)form;
        String username = lgifrm.getUsername();
        String password = lgifrm.getPassword();
        try {
            if(password.equals(StrutsUsers.getInstance().getPassword(username))) {
                request.getSession().setAttribute("user", lgifrm.getUsername());
                return mapping.findForward("login.ok");
            } else {
                ActionErrors errors = new ActionErrors();
                errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("username.password.not.valid"));
                saveErrors(request, errors);
                return mapping.findForward("login.failure");
            }
        } catch (ClassNotFoundException ex) {
            ActionErrors errors = new ActionErrors();
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("database.error"));
            saveErrors(request, errors);
            return mapping.findForward("login.failure");
        } catch (SQLException ex) {
            ActionErrors errors = new ActionErrors();
            errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("database.error"));
            saveErrors(request, errors);
            return mapping.findForward("login.failure");
        }
    }
}
