package org.yacos.web.system.controller;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import org.yacos.core.conferences.IConferenceManager;
import org.yacos.core.users.IUserManager;
import org.yacos.web.system.session.SessionService;

public class ListConferenceController implements Controller{

	protected final Log logger = LogFactory.getLog(getClass());

	private IConferenceManager conferenceManager;
	private IUserManager userManager;

	public IUserManager getUserManager() {
		return userManager;
	}
	public void setUserManager(IUserManager userManager) {
		this.userManager = userManager;
	}
	public IConferenceManager getConferenceManager() {
		return conferenceManager;
	}
	public void setConferenceManager(IConferenceManager conferenceManager) {
		this.conferenceManager = conferenceManager;
	}

	/**
	 * Generate list of conference
	 */
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception 	{

		logger.info("Returning listConference view");
		
		Map<String, Object> model = new HashMap<String, Object>();
		
		// add list of conference
		model.put("listConference", conferenceManager.getConferences());
		
		// add list of role
		model.put("listRole", userManager.getRolesForUser(SessionService.getInstance().getCurrentUserLogin()));

		// Error choose conference
		String msgCode = request.getParameter("error");
		if (msgCode != null)
		{
			model.put("errorMessage", msgCode);
		}
		
		// Create User before
		String msgAccount = request.getParameter("account");
		if (msgAccount != null)
		{
			model.put("ac", msgAccount);
		}

		return new ModelAndView("listConference", model);
	}
}
