package com.ferienclub.admin.actions;

import java.util.ArrayList;
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.custadmin.beans.Place;


public class InitPlacesAdmAction extends Action {

	public static Logger log = Logger.getLogger(InitPlacesAdmAction.class);
	
	public ActionForward perform(ActionMapping mapping,
			 ActionForm form,
			 HttpServletRequest request,
			 HttpServletResponse response){
		
		ActionErrors errors = new ActionErrors();
		
		try {
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/UserManagementPM?method=getPlaces","",null);
			Object o = result.getPayload();
			Place[] places = null;
			if (result != null)
			{
				places = (Place[])o;
				log.info(this.getClass() + " : Received " + places.length + " Places");
			} else {
				log.info(this.getClass() + " : Received NULL");
			}
			List p = new ArrayList();
			for (int i = 0; i < places.length; i++)
			{
				p.add(places[i]);
			}
			log.info(this.getClass() + " : Put List with Places in Session scope");
			request.getSession().setAttribute("places",p);
		} catch (UMOException e) {
			errors.add("technical Error",new ActionError("technical.error.mule"));
			super.saveErrors(request,errors);
			return (new ActionForward(mapping.getInput()));
		}
		
		return mapping.findForward("success");
		
	}
	
}
