package com.ferienclub.mb;

import org.apache.log4j.Logger;
import org.mule.extras.client.MuleClient;
import org.mule.impl.RequestContext;
import org.mule.umo.UMOEventContext;
import org.mule.umo.UMOException;
import org.mule.umo.UMOMessage;

import com.ferienclub.mb.interfaces.IGetRoomsPM;
import com.ferienclub.vacancymgm.beans.Room;

public class GetRoomsPM implements IGetRoomsPM{

	Logger log = Logger.getLogger(GetRoomsPM.class);
	
	
	public Room[] getRooms(String dummy) {
		log.info(this.getClass() + " : Entering method getRooms()");
		UMOEventContext context = RequestContext.getEventContext();
		try {
			/* 
			 * Really BAD!
			 * I have to do this, because MULE is not able to transfer comlpex objects over AXIS!
			 * E.g. String values all become ""!
			 * The system behaves like this, if the UMOEventContext is used to send events!
			 * If we use MuleClient everything is fine, BUT the components have to be aware
			 * of the destination of the message!
			 */ 
			MuleClient client = new MuleClient();
			UMOMessage result = client.send("axis:http://localhost:8081/axis/services/VacancyManagement?method=getRooms","",null);
			/* 
			UMOMessage result = context.sendEvent("");
			*/
			Room[] rooms = (Room[])result.getPayload();
			log.info("Received " + rooms.length + " rooms");
			
			printReceivedRooms(rooms);
			
			// Have to prevent MULE to do further processing, because the outbound router would be invoked again!
			context.setStopFurtherProcessing(true);
			System.out.println("Returning ...");
			return rooms;
		} catch (UMOException e) {
			e.printStackTrace();
		}
		return null;
	}
    
	private void printReceivedRooms(Room[] rooms){
		for (int i = 0; i < rooms.length; i++)
		{
			System.out.println("Room No: " + i);
			System.out.println(rooms[i]);
		}
	}
	
}
