/**
 * HomeControlServiceMessageReceiverInOut.java
 *
 * This file was auto-generated from WSDL
 * by the Apache Axis2 version: 1.1-SNAPSHOT Oct 25, 2006 (06:51:57 GMT+00:00)
 */
package homeNetworkServices.homeControl;

import java.util.ArrayList;

import nino.base.NetworkRepository;
import nino.base.NinoConf;
import nino.base.NinoObject;
import nino.homeControl.Room;
import nino.homeControl.function.TrafficPriorityFunction;
import nino.homeControl.gadget.Light;
import nino.homeControl.gadget.Sensor;
import nino.homeControl.property.LightStatus;
import nino.homeControl.property.Priority;
import nino.homeControl.property.SensorStatus;
import nino.homeControl.property.SensorType;
import nino.homeControl.property.TrafficClass;
import nino.homeControl.property.Vacation;
import nino.network.ressource.NetworkRessource;

import org.apache.axiom.om.OMElement;

/**
 * HomeControlServiceMessageReceiverInOut message receiver
 */

public class HomeControlServiceMessageReceiverInOut extends org.apache.axis2.receivers.AbstractInOutSyncMessageReceiver {

	private HomeControlService hcs;

	public void invokeBusinessLogic(org.apache.axis2.context.MessageContext msgContext, org.apache.axis2.context.MessageContext newMsgContext)
			throws org.apache.axis2.AxisFault {

		try {
			
			
			// get the implementation class for the Web Service
			Object obj = getTheImplementationObject(msgContext);

			hcs = (HomeControlService) obj;
			// Out Envelop
			org.apache.axiom.soap.SOAPEnvelope envelope = null;
			// Find the axisOperation that has been set by the Dispatch phase.
			org.apache.axis2.description.AxisOperation op = msgContext.getOperationContext().getAxisOperation();
			if (op == null) {
				throw new org.apache.axis2.AxisFault(
						"Operation is not located, if this is doclit style the SOAP-ACTION should specified via the SOAP Action to use the RawXMLProvider");
			}

			java.lang.String methodName;
			if (op.getName() != null & (methodName = op.getName().getLocalPart()) != null) {
				System.out.println("[MY_MSG_REC] methodename to execute" + methodName);
				if ("getHCS".equals(methodName)) {

					org.apache.axiom.om.OMElement param7 = null;

					// doc style
					org.apache.axiom.om.OMElement wrappedParam = (org.apache.axiom.om.OMElement) fromOM(msgContext.getEnvelope().getBody()
							.getFirstElement(), org.apache.axiom.om.OMElement.class, getEnvelopeNamespaces(msgContext.getEnvelope()));

					Light light = (Light) getNinoObject(wrappedParam, "light");

					//System.out.println("[MY_MSG_REC] This is the Light: " + light);

					NetworkRessource nr = hcs.getHCS(light);

					param7 = getOM(nr);

					envelope = toEnvelope(getSOAPFactory(msgContext), param7, false);

				}

				if ("getSensor".equals(methodName)) {

					org.apache.axiom.om.OMElement param9 = null;

					// doc style
					org.apache.axiom.om.OMElement wrappedParam = (org.apache.axiom.om.OMElement) fromOM(msgContext.getEnvelope().getBody()
							.getFirstElement(), org.apache.axiom.om.OMElement.class, getEnvelopeNamespaces(msgContext.getEnvelope()));

					Room room = (Room) getNinoObject(wrappedParam, "room");
					SensorType stype = (SensorType) getNinoObject(wrappedParam, "stype");

					Sensor sensor = hcs.getSensor(room, stype);

					param9 = getOM(sensor);

					envelope = toEnvelope(getSOAPFactory(msgContext), param9, false);

				}

				if ("getLight".equals(methodName)) {

					org.apache.axiom.om.OMElement param11 = null;

					// doc style
					org.apache.axiom.om.OMElement wrappedParam = (org.apache.axiom.om.OMElement) fromOM(msgContext.getEnvelope().getBody()
							.getFirstElement(), org.apache.axiom.om.OMElement.class, getEnvelopeNamespaces(msgContext.getEnvelope()));

					Room room = (Room) getNinoObject(wrappedParam, "room");

					//System.out.println("[MY_MSG_REC] This is the Room: " + room);

					Light light = hcs.getLight(room);

					param11 = getOM(light);
					// param11 = skel.getLight(wrappedParam);

					envelope = toEnvelope(getSOAPFactory(msgContext), param11, false);

				}

				if ("getNRsByFunction".equals(methodName)) {

					org.apache.axiom.om.OMElement param11 = null;

					// doc style
					org.apache.axiom.om.OMElement wrappedParam = (org.apache.axiom.om.OMElement) fromOM(msgContext.getEnvelope().getBody()
							.getFirstElement(), org.apache.axiom.om.OMElement.class, getEnvelopeNamespaces(msgContext.getEnvelope()));

					TrafficPriorityFunction func = (TrafficPriorityFunction) getNinoObject(wrappedParam, "func");

					ArrayList<NetworkRessource> nrs = hcs.getNRsByFunction(func);

					param11 = getOM(nrs);

					//System.out.println("[MY_MSG_REC] nrs-return: " + param11.toString());

					envelope = toEnvelope(getSOAPFactory(msgContext), param11, false);

				}

				if ("setTrafficClassPriority".equals(methodName)) {

					org.apache.axiom.om.OMElement param11 = null;

					// doc style
					org.apache.axiom.om.OMElement wrappedParam = (org.apache.axiom.om.OMElement) fromOM(msgContext.getEnvelope().getBody()
							.getFirstElement(), org.apache.axiom.om.OMElement.class, getEnvelopeNamespaces(msgContext.getEnvelope()));

					ArrayList<NetworkRessource> nrs = getNinoObjectList(wrappedParam, "nrs");
					TrafficClass tc = (TrafficClass) getNinoObject(wrappedParam, "tc");
					Priority prio = (Priority) getNinoObject(wrappedParam, "prio");

					String ret = hcs.setTrafficClassPriority(nrs, tc, prio);

					param11 = getOM(ret);
					//System.out.println("[MY_MSG_REC] setTCP-return: " + param11.toString());

					envelope = toEnvelope(getSOAPFactory(msgContext), param11, false);

				}
				
				if ("setVacationConf".equals(methodName)) {

					org.apache.axiom.om.OMElement param9 = null;

					// doc style
					org.apache.axiom.om.OMElement wrappedParam = (org.apache.axiom.om.OMElement) fromOM(msgContext.getEnvelope().getBody()
							.getFirstElement(), org.apache.axiom.om.OMElement.class, getEnvelopeNamespaces(msgContext.getEnvelope()));
					
									
					Vacation vac = (Vacation) getNinoObject(wrappedParam, "vac");
					NinoConf conf = (NinoConf) getNinoConf(wrappedParam, "conf");

					String ret = hcs.setVacationConf(vac, conf);

					param9 = this.getOM(ret);

					envelope = toEnvelope(getSOAPFactory(msgContext), param9, false);

				}

				if ("switchLightbySensor".equals(methodName)) {

					org.apache.axiom.om.OMElement param9 = null;

					// doc style
					org.apache.axiom.om.OMElement wrappedParam = (org.apache.axiom.om.OMElement) fromOM(msgContext.getEnvelope().getBody()
							.getFirstElement(), org.apache.axiom.om.OMElement.class, getEnvelopeNamespaces(msgContext.getEnvelope()));
					
					//System.out.println("[MY_MSG_REC] switchLightbySensor: " + wrappedParam);
					
					Light light = (Light) getNinoObject(wrappedParam, "light");
					LightStatus lstat = (LightStatus) getNinoObject(wrappedParam, "lstat");
					Sensor sensor = (Sensor) getNinoObject(wrappedParam, "sensor");
					SensorStatus sstat = (SensorStatus) getNinoObject(wrappedParam, "sstat");
					NetworkRessource nr = (NetworkRessource) getNinoObject(wrappedParam, "hcs");

					String ret = hcs.switchLightbySensor(light, lstat, sensor, sstat, nr);

					param9 = this.getOM(ret);

					envelope = toEnvelope(getSOAPFactory(msgContext), param9, false);

				}

				newMsgContext.setEnvelope(envelope);
			}
		} catch (Exception e) {
			throw org.apache.axis2.AxisFault.makeFault(e);
		}
	}

	/**
	 * Gets the NinoConfiguration that is the Input for the Service.
	 * 
	 * @param om Input
	 * @param type Input-type
	 * @return NinoConfiguration
	 */
	private NinoConf getNinoConf(OMElement om, String type) {		
		String in = om.toString();
		in = in.replaceAll("&lt;", "<");
		in = in.replaceAll("&gt;", ">");
		in = parseOM(in, type);
		String conf = parseOM(in, "localName");
		NinoConf nc = new NinoConf(conf);
		return nc;
	}

	/**
	 * Gets the NinoObjects that are the Input for the Service.
	 * 
	 * @param om Input
	 * @param type Input-type
	 * @return List of NinoObjects
	 */
	private ArrayList getNinoObjectList(OMElement om, String type) {
		ArrayList<NinoObject> nol = new ArrayList<NinoObject>();
		String in = om.toString();
		in = parseOM(in, type);
		while (in.startsWith("<hasObject>")) {
			int i = in.indexOf("</hasObject>");
			String localName = parseOM(in, "localName");
			String rdfType = parseOM(in, "rdfType");
			NinoObject no = getNinoObject(localName, rdfType);
			nol.add(no);
			in = in.substring(i + 12, in.length());
		}

		return nol;
	}
	
	/**
	 * Convert NinoObjects into an Output of the Service.
	 * 
	 * @param nrs List of NinoObjects.
	 * @return Output
	 */
	private OMElement getOM(ArrayList<NetworkRessource> nrs) {
		OMElement ret = org.apache.axiom.om.OMAbstractFactory.getOMFactory().createOMElement("return", null);
		OMElement nol = org.apache.axiom.om.OMAbstractFactory.getOMFactory().createOMElement("NinoObjectList", null);
		for (int i = 0; i < nrs.size(); i++) {
			NetworkRessource nr = nrs.get(i);
			String localName = "";
			String rdfType = "";
			if (!(nr == null)) {
				localName = nr.getLocalName();
				rdfType = nr.getRdfType();
			}
			OMElement out = org.apache.axiom.om.OMAbstractFactory.getOMFactory().createOMElement("NinoObject", null);
			OMElement localNameOM = org.apache.axiom.om.OMAbstractFactory.getOMFactory().createOMElement("localName", null);
			localNameOM.setText(localName);
			OMElement rdfTypeOM = org.apache.axiom.om.OMAbstractFactory.getOMFactory().createOMElement("rdfType", null);
			rdfTypeOM.setText(rdfType);
			out.addChild(localNameOM);
			out.addChild(rdfTypeOM);
			nol.addChild(out);
		}
		ret.addChild(nol);
		return ret;
	}

	/**
	 * Convert a String into an Output of the Service.
	 * 
	 * @param in String to convert
	 * @return output
	 */
	private OMElement getOM(String in) {
		OMElement ret = org.apache.axiom.om.OMAbstractFactory.getOMFactory().createOMElement("return", null);
		OMElement out = org.apache.axiom.om.OMAbstractFactory.getOMFactory().createOMElement("NinoConf", null);
		out.setText(in);
		ret.addChild(out);
		return ret;
	}

	/**
	 * Convert a NinoObject into an Output of the Service.
	 * 
	 * @param no NinoObject to convert.
	 * @return Output.
	 */
	private OMElement getOM(NinoObject no) {
		String localName = "";
		String rdfType = "";
		if (!(no == null)) {
			localName = no.getLocalName();
			rdfType = no.getRdfType();
		}

		OMElement ret = org.apache.axiom.om.OMAbstractFactory.getOMFactory().createOMElement("return", null);
		OMElement out = org.apache.axiom.om.OMAbstractFactory.getOMFactory().createOMElement("NinoObject", null);
		OMElement localNameOM = org.apache.axiom.om.OMAbstractFactory.getOMFactory().createOMElement("localName", null);
		localNameOM.setText(localName);
		OMElement rdfTypeOM = org.apache.axiom.om.OMAbstractFactory.getOMFactory().createOMElement("rdfType", null);
		rdfTypeOM.setText(rdfType);
		out.addChild(localNameOM);
		out.addChild(rdfTypeOM);
		ret.addChild(out);
		return ret;
	}

	/**
	 * Convert the Input of the Service into an NinoObject
	 * 
	 * @param om input
	 * @param type Input-type
	 * @return NinoObject
	 */
	private NinoObject getNinoObject(OMElement om, String type) {
		String in = om.toString();
		in = parseOM(in, type);
		// System.out.println("[MY_MSG_REC] in: " + in);
		String localName = parseOM(in, "localName");
		// System.out.println("[MY_MSG_REC] localName: " + localName);
		String rdfType = parseOM(in, "rdfType");
		// System.out.println("[MY_MSG_REC] rdfType: " + rdfType);
		NinoObject no = getNinoObject(localName, rdfType);
		return no;
	}
	
	
	/**
	 * Gets the NinoObject in the Repository, which have the given LocalName and RDFType.  
	 * 
	 * @param localName LocalName of the NinoObject.
	 * @param rdfType RDFType of the NinoObject.
	 * @return NinoObject
	 */
	private NinoObject getNinoObject(String localName, String rdfType) {
		NetworkRepository ninoRep = new NetworkRepository(this.hcs.getOntURL());
		return ninoRep.getNinoObject(localName, rdfType);
	}

	/**
	 * Parses the input for the Value of the Tag of the given type.
	 * 
	 * @param in input
	 * @param type type of the Tag.
	 * @return Value of the Tag.
	 */
	private String parseOM(String in, String type) {
		String out = "";
		int i = in.indexOf("<" + type);
		in = in.substring(i + 1);
		i = in.indexOf(">");
		int k = in.indexOf("</" + type);
		out = in.substring(i + 1, k);
		return out;
	}

	
	/**
	 * Generated by AXIS2
	 * 
	 * @param param
	 * @param type
	 * @param extraNamespaces
	 * @return
	 */
	private org.apache.axiom.om.OMElement fromOM(org.apache.axiom.om.OMElement param, java.lang.Class type, java.util.Map extraNamespaces) {
		return param;
	}

	/**
	 * Generated by AXIS2
	 * 
	 * @param param
	 * @param optimizeContent
	 * @return
	 */
	private org.apache.axiom.om.OMElement toOM(org.apache.axiom.om.OMElement param, boolean optimizeContent) {
		return param;
	}

	/**
	 * Generated by AXIS2
	 * 
	 * @param factory
	 * @param param
	 * @param optimizeContent
	 * @return
	 */
	private org.apache.axiom.soap.SOAPEnvelope toEnvelope(org.apache.axiom.soap.SOAPFactory factory, org.apache.axiom.om.OMElement param,
			boolean optimizeContent) {
		org.apache.axiom.soap.SOAPEnvelope envelope = factory.getDefaultEnvelope();
		envelope.getBody().addChild(param);
		return envelope;
	}

	/**
	 * get the default envelope
	 */
	private org.apache.axiom.soap.SOAPEnvelope toEnvelope(org.apache.axiom.soap.SOAPFactory factory) {
		return factory.getDefaultEnvelope();
	}

	/**
	 * A utility method that copies the namepaces from the SOAPEnvelope
	 */
	private java.util.Map getEnvelopeNamespaces(org.apache.axiom.soap.SOAPEnvelope env) {
		java.util.Map returnMap = new java.util.HashMap();
		java.util.Iterator namespaceIterator = env.getAllDeclaredNamespaces();
		while (namespaceIterator.hasNext()) {
			org.apache.axiom.om.OMNamespace ns = (org.apache.axiom.om.OMNamespace) namespaceIterator.next();
			returnMap.put(ns.getPrefix(), ns.getNamespaceURI());
		}
		return returnMap;
	}

	/**
	 * Generated by AXIS2
	 * 
	 * @param e
	 * @return
	 */
	private org.apache.axis2.AxisFault createAxisFault(java.lang.Exception e) {
		org.apache.axis2.AxisFault f;
		Throwable cause = e.getCause();
		if (cause != null) {
			f = new org.apache.axis2.AxisFault(e.getMessage(), cause);
		} else {
			f = new org.apache.axis2.AxisFault(e.getMessage());
		}

		return f;
	}

}// end of class
