/*
 * LoginController.java
 * 
 * Created on 2007.10.04., 22:03:03
 * 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hu.nir.nmail.spring.controllers;

import hu.nir.nmail.ejb.entity.NmailUser;
import hu.nir.nmail.services.DownloadMailService;
import hu.nir.nmail.services.LoginService;
import hu.nir.nmail.services.RestrictionService;
import hu.nir.nmail.spring.beans.MailsData;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/**
 *
 * @author Fodor Attila
 */
public class LoginController extends SimpleFormController {
    private LoginService loginService;
    private String errorView;
    private DownloadMailService downloadMailService;
    private RestrictionService restrictionService;
    
    @Override
    @SuppressWarnings("unchecked")
    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) {
        HttpSession session = request.getSession();
        
        //login
        Map<String,Object> loginData = loginService.processCommand(command);
        NmailUser nmailUser = (NmailUser) loginData.get("nmailUser");
        if (nmailUser == null) {
            return new ModelAndView(getFormView(),"loginData",command);
        }
        
        session.setAttribute("nmailUser", nmailUser);
        
        //download last 5 mail
        MailsData mailsData = (MailsData) session.getAttribute("mailsData");
        Map<String,Object> model = downloadMailService.processCommand(nmailUser, mailsData);
        if (model == null) {
            return new ModelAndView(getFormView(),"loginData",command);
        }
        session.setAttribute("mailsData", model.get("mailsData"));
        
        Map<String,Object> restrictionMap = restrictionService.processCommand(nmailUser);
        session.setAttribute("restriction", restrictionMap.get("restriction"));
        return new ModelAndView(getSuccessView(),model);
    }

    public LoginService getLoginService() {
        return loginService;
    }

    public void setLoginService(LoginService loginService) {
        this.loginService = loginService;
    }

    public DownloadMailService getDownloadMailService() {
        return downloadMailService;
    }

    public void setDownloadMailService(DownloadMailService downloadMailService) {
        this.downloadMailService = downloadMailService;
    }

    public String getErrorView() {
        return errorView;
    }

    public void setErrorView(String errorView) {
        this.errorView = errorView;
    }

    public RestrictionService getRestrictionService() {
        return restrictionService;
    }

    public void setRestrictionService(RestrictionService restrictionService) {
        this.restrictionService = restrictionService;
    }
}
