package mildo.wiwo.components;

import mildo.wiwo.Session;
import mildo.wiwo.user.UserLoginResult;
import mildo.wiwo.user.UserManager;

import com.webobjects.appserver.*;
import com.webobjects.foundation.NSTimestamp;

// Generated by the WOLips Templateengine Plug-in at Apr 13, 2009 7:31:51 PM
public class LoginComp 
	extends BaseComp 
{
    
	public LoginComp(WOContext context) {
        super(context);
    }
	
	private String login;
	
	public void setLogin(String aValue) {
		login = aValue;
	}
	
	public String getLogin() {
		return login;
	}
	
	//---------------
	
	private String password;
	
	public void setPassword(String aValue) {
		password = aValue;
	}
	
	public String getPassword() {
		return password;
	}
	
	// ---------------
	
	public WOComponent loginAction() {
		if (login != null && password != null && login.length() > 0 && password.length() > 0) {
			UserManager theUsrMngr = ((Session) session()).getManager();
			UserLoginResult theUserLogginRes = theUsrMngr.processUserLogin(((Session) session()), login, password);
			theUserLogginRes.getUser().setLastLogin(new NSTimestamp());
			session().defaultEditingContext().saveChanges();
			if (theUserLogginRes.isLoginOk()) {
				System.out.println("User " + login + " is logged in");
			}
		}
		return null;
	}
	
	// ---------------
	
	public WOComponent logout() {
		((Session)session()).setActualSessionUser(null);
		return null;
	}
	
}