// 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 com.opensymphony.webwork.*;
import com.opensymphony.xwork.*;

public class LoginAction extends ActionSupport {
    public String execute() {
        try {
            if(password.equals(WebworkUsers.getInstance().getPassword(username))) {
                ServletActionContext.getRequest().getSession().setAttribute("user", username);
                return "login.ok";
            } else {
                addActionError("Username/password combination not valid.");
                return "login.failure";
            }
        } catch (ClassNotFoundException ex) {
            addActionError("A database error happend - try again and contact admin if it fails again.");
            return "login.failure";
        } catch (SQLException ex) {
            addActionError("A database error happend - try again and contact admin if it fails again.");
            return "login.failure";
        }
    }
    private String username;
    private String password;
    public String getUsername() {
        return username;
    }
    public String getPassword() {
        return password;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public void validate() {
        if((username == null) || (username.trim().equals(""))) {
            addFieldError("username", "No username specified.");
        }
        if((password == null) || (password.trim().equals(""))) {
            addFieldError("password", "No password specified.");
        }
    }
}
