/*
 * AccountManagerBean.java
 *
 * Created on 10. April 2005, 00:23
 */

package com.mycorp.myapp.jsf;

import org.apache.log4j.Logger;
import org.springframework.dao.DataIntegrityViolationException;

import com.mycorp.myapp.domain.Account;
import com.mycorp.myapp.domain.service.Facade;

/**
 * Account manager bean
 * Bean to be used for the view controller logic of user account management.
 * It is supposed to be stateless so that it can be declared in application scope
 * and can be shared among all sessions and requests.
 */
public class AccountManagerBean {
	//the logger for this class
	private Logger logger = Logger.getLogger(this.getClass());

    /**
     * Holds value of property service.
     */
    private com.mycorp.myapp.domain.service.Facade service;

    
    /** Creates a new instance of AccountManagerBean */
    public AccountManagerBean() {
    }

    /**
     * Getter for property service.
     * @return Value of property service.
     */
    public Facade getService() {
        return this.service;
    }

    /**
     * Setter for property service.
     * @param service New value of property service.
     */
    public void setService(Facade service) {
        this.service = service;
    }
    
	/**
	 * @return Returns true if user is logged on.
	 */
	public boolean getIsLoggedOn() {
		Account loggedOnAccount = (Account) FacesUtils.getManagedBean("loggedOnAccount");
		return ((loggedOnAccount != null) && (loggedOnAccount.getId() != null));
	}
    
	/**
	 * JSF-binding method for logging user on
	 */
	public String logon() {
    	Account account = this.getService().getAccountByUserId(FacesUtils.getManagedBean("logonId").toString());
    	if (account == null) {
    		FacesUtils.addErrorMessageByKey("logon_box", "com.mycorp.myapp.jsf.USER_ID_NOT_FOUND");
    		return "error";
    	}
    	else {
	    	if (account.getPassword().equals(FacesUtils.getManagedBean("logonPassword").toString()) && !account.getLocked().booleanValue()) {
	    		FacesUtils.setManagedBean("loggedOnAccount", account);
	    	} else {
	    		FacesUtils.addErrorMessageByKey("logon_box", "com.mycorp.myapp.jsf.WRONG_PASSWORD");
	    		return "error";
	    	}
    	}
    	return "success";
    }
    
	/**
	 * JSF-binding method for logging user off
	 */
	public String logoff() {
    	FacesUtils.setManagedBean("loggedOnAccount", null);
    	return "toHome";
    }
    
	/**
	 * JSF-binding method for registering new user account
	 */
    public String registerAccount() {
    	String registerPasswordRepeat = FacesUtils.getManagedBean("registerPasswordRepeat").toString();
    	Account registerAccount = (Account) FacesUtils.getManagedBean("registerAccount"); 
		if ((registerPasswordRepeat != null) && (registerPasswordRepeat.equals(registerAccount.getPassword()))) {
	        try {
	        	this.logger.info("Trying to create account.");
				this.getService().saveAccount(registerAccount);
				FacesUtils.setManagedBean("loggedOnAccount", registerAccount);
			} catch (DataIntegrityViolationException e) {
				this.logger.warn("Could not create account - data integrity violation.");
				FacesUtils.addWarnMessageByKey(null, "com.mycorp.myapp.jsf.USER_ID_EXISTS");
				return "toRegistration";
			}
	        return "toHome";
		}
		else {
			this.logger.info("Passwords do not match");
			FacesUtils.addErrorMessageByKey(null, "com.mycorp.myapp.jsf.PASSWORDS_DO_NOT_MATCH");
			return "toRegistration";
			
		}
    }
    
	/**
	 * JSF-binding method for updating account of logged on user
	 */
    public String updateAccount() {
    	String logonPasswordRepeat = FacesUtils.getManagedBean("logonPasswordRepeat").toString();
    	Account loggedOnAccount = (Account) FacesUtils.getManagedBean("loggedOnAccount"); 
		if ((logonPasswordRepeat != null) && (logonPasswordRepeat.equals(loggedOnAccount.getPassword()))) {
	        try {
	        	this.logger.info("Trying to update account.");
				this.getService().updateAccount(loggedOnAccount);
			} catch (DataIntegrityViolationException e) {
				this.logger.warn("Could not create account - data integrity violation.");
				FacesUtils.addWarnMessageByKey(null, "com.mycorp.myapp.jsf.USER_ID_EXISTS");
				return "toAccount";
			}
	        return "toHome";
		}
		else {
			this.logger.info("Passwords do not match");
			FacesUtils.addErrorMessageByKey(null, "com.mycorp.myapp.jsf.PASSWORDS_DO_NOT_MATCH");
			return "toAccount";
			
		}
    }
}
