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.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.UMOMessage;

import com.ferienclub.custadmin.beans.Appointment;
import com.ferienclub.custadmin.beans.User;

public class InitAppointmentAction extends Action {

	public ActionForward perform(ActionMapping mapping,
			 ActionForm form,
			 HttpServletRequest request,
			 HttpServletResponse response){
		
		ActionErrors errors = new ActionErrors();
		try {
            // Get all Users and Appointments
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/UserManagementPM?method=getAppointments","",null);
			Appointment[] appointments = (Appointment[])result.getPayload();
			List apps = new ArrayList();
			for (int i = 0; i < appointments.length; i++) apps.add(appointments[i]);
			request.getSession().setAttribute("appointments",apps);
			
			result = client.send("axis:http://localhost:8081/axis/services/UserManagementPM?method=getUsers","",null);
			User[] users = (User[])result.getPayload();
			List us = new ArrayList();
			for (int i = 0; i < users.length; i++) us.add((User)users[i]);
			request.getSession().setAttribute("users",us);
			
			// All required data has been put into the session scope
		} catch (Exception e) {
			errors.add("technical Error",new ActionError("technical.error.mule"));
			super.saveErrors(request,errors);
			return (new ActionForward(mapping.getInput()));
		}
		return mapping.findForward("success");
	}
	
}
