package com.ferienclub.pda.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOException;
import org.mule.umo.UMOMessage;

import com.ferienclub.beachclub.esb.messages.MenuRequest;

public class GetBeachClubMenuAction extends Action {

	Logger log = Logger.getLogger(GetBeachClubMenuAction.class);
	
	public ActionForward perform(ActionMapping mapping,
			 ActionForm form,
			 HttpServletRequest request,
			 HttpServletResponse response){
		
		log.info("Entering GetBeachClubMenuAction");
		ActionErrors errors = new ActionErrors();
		
		try {
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/BeachClubManagement?method=getMenu","",null);
			
			if (result == null)
			{
				errors.add("errors",new ActionError("no.menu.available"));
				super.saveErrors(request,errors);
				return (new ActionForward(mapping.getInput()));
			}
			MenuRequest mr = (MenuRequest)result.getPayload();
			List menu = mr.getMenu();
			
			if (menu == null || menu.size() == 0){
				errors.add("errors",new ActionError("no.menu.available"));
				super.saveErrors(request,errors);
				return (new ActionForward(mapping.getInput()));
			}
			
			request.getSession().setAttribute("menu",menu);
		} catch (UMOException e) {
			errors.add("errors",new ActionError("technical.error.mule"));
			super.saveErrors(request,errors);
			return (new ActionForward(mapping.getInput()));
		}
		
		return mapping.findForward("success");
		
	}
	
}
