/*
 * MessageDownloadController.java
 * 
 * Created on 2007.10.12., 20:09:16
 * 
 * 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.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.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author attila
 */
public class MessageDownloadController extends AbstractController {
    private String successView;
    private String logoutView;
    private DownloadMailService downloadMailService;
    
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        NmailUser nmailUser = (NmailUser) session.getAttribute("nmailUser");
        
        //if userId not found
        if (nmailUser == null) {
            return new ModelAndView(getLogoutView());
        }
        
        MailsData mailsData = (MailsData) session.getAttribute("mailsData");
        
        Map<String,Object> model = downloadMailService.processCommand(nmailUser, mailsData);
        if (model == null) {
            return new ModelAndView(getLogoutView());
        }
        
        session.setAttribute("mailsData", model.get("mailsData"));
        return new ModelAndView(getSuccessView(),model);
    }

    public String getSuccessView() {
        return successView;
    }

    public void setSuccessView(String successView) {
        this.successView = successView;
    }

    public DownloadMailService getDownloadMailService() {
        return downloadMailService;
    }

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

    public String getLogoutView() {
        return logoutView;
    }

    public void setLogoutView(String logoutView) {
        this.logoutView = logoutView;
    }
}
