package usecase.prosodie.deliveredService;

import javax.xml.namespace.QName;

import com.ebmwebsourcing.easybpel.model.bpel.api.BPELException;
import com.ebmwebsourcing.easybpel.model.bpel.api.message.BPELExternalMessage;
import com.ebmwebsourcing.easybpel.model.bpel.impl.message.BPELExternalMessageImpl;
import com.ebmwebsourcing.easyviper.core.api.CoreException;
import com.ebmwebsourcing.easyviper.environment.test.env.api.MockService;
import com.ebmwebsourcing.easyviper.environment.test.env.api.Operation;
import com.ebmwebsourcing.easyviper.environment.test.env.api.ProviderEndpoint;
import com.ebmwebsourcing.easyviper.environment.test.env.impl.AbstractServiceImpl;
import com.ebmwebsourcing.easyviper.environment.test.env.impl.OperationImpl;

public class AliasService extends AbstractServiceImpl implements MockService {

	public AliasService(final ProviderEndpoint pep) throws BPELException {
		super(pep);
		
		this.setName(new QName("http://sgdt.prosodie/AliasService/1/",
		"AliasService"));
		
		this.getDeliveredServiceAlias();
	}
	
	private void getDeliveredServiceAlias() throws BPELException {
		System.out.println("getDeliveredServiceAlias");
		
		try {
			final Operation getDeliveredServices = new OperationImpl("getDeliveredServiceAlias",
					Operation.IN_OUT, this);
			this.addOperation(getDeliveredServices);

			final BPELExternalMessage in1 = new BPELExternalMessageImpl();
			in1.setService(this.getName());
			in1.setEndpoint(this.getProviderEndpoint().getName());
			in1.setQName(new QName("http://sgdt.prosodie/AliasService/1/",
					"getDeliveredServiceAliasRequest"));
			in1
					.setMessage("<tns:getDeliveredServiceAlias xmlns:tns=\"http://sgdt.prosodie/AliasService/1/\">\r\n" + 
							"  <deliveredService xmlns:sd=\"http://document.prosodie.com/DeliveredService/1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"sd:DeliveredService\">\r\n" + 
							"    <ns1:uuid xmlns:ns1=\"http://document.prosodie.com/DeliveredService/1/\">SN1</ns1:uuid>\r\n" + 
							"    <ns1:mainAffair xmlns:ns1=\"http://document.prosodie.com/DeliveredService/1/\">\r\n" + 
							"      <ns5:uuid xmlns:ns5=\"http://document.prosodie.com/MainAffair/1/\">AP1254</ns5:uuid>\r\n" + 
							"    </ns1:mainAffair>\r\n" + 
							"  </deliveredService>\r\n" + 
							"</tns:getDeliveredServiceAlias>");

			final BPELExternalMessage out1 = new BPELExternalMessageImpl();
			out1.setService(this.getName());
			out1.setEndpoint(this.getProviderEndpoint().getName());
			out1.setQName(new QName("http://sgdt.prosodie/AliasService/1/",
					"getDeliveredServiceAliasResponse"));
			out1
					.setMessage("<ns:getDeliveredServiceAliasResponse xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns=\"http://sgdt.prosodie/AliasService/1/\">\r\n" + 
							"         <aliases>\r\n" + 
							"            <alias>test@service.com</alias>\r\n" + 
							"         </aliases>\r\n" + 
							"      </ns:getDeliveredServiceAliasResponse>\r\n" + 
							"");
			getDeliveredServices.addMessageExchangeInstances(in1, out1, null);
			
			
			final BPELExternalMessage in2 = new BPELExternalMessageImpl();
			in2.setService(this.getName());
			in2.setEndpoint(this.getProviderEndpoint().getName());
			in2.setQName(new QName("http://sgdt.prosodie/AliasService/1/",
					"getDeliveredServiceAliasRequest"));
			in2
					.setMessage("<tns:getDeliveredServiceAlias xmlns:tns=\"http://sgdt.prosodie/AliasService/1/\">\r\n" + 
							"  <deliveredService xmlns:sd=\"http://document.prosodie.com/DeliveredService/1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"sd:DeliveredService\">\r\n" + 
							"    <ns1:uuid xmlns:ns1=\"http://document.prosodie.com/DeliveredService/1/\">SN2</ns1:uuid>\r\n" + 
							"    <ns1:mainAffair xmlns:ns1=\"http://document.prosodie.com/DeliveredService/1/\">\r\n" + 
							"      <ns5:uuid xmlns:ns5=\"http://document.prosodie.com/MainAffair/1/\">AP1254</ns5:uuid>\r\n" + 
							"    </ns1:mainAffair>\r\n" + 
							"  </deliveredService>\r\n" + 
							"</tns:getDeliveredServiceAlias>");

			final BPELExternalMessage out2 = new BPELExternalMessageImpl();
			out2.setService(this.getName());
			out2.setEndpoint(this.getProviderEndpoint().getName());
			out2.setQName(new QName("http://sgdt.prosodie/AliasService/1/",
					"getDeliveredServiceAliasResponse"));
			out2
					.setMessage("<ns:getDeliveredServiceAliasResponse xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns=\"http://sgdt.prosodie/AliasService/1/\">\r\n" + 
							"         <aliases>\r\n" + 
							"            <alias>test2@service.com</alias>\r\n" + 
							"            <alias>test3@service.com</alias>\r\n" + 
							"         </aliases>\r\n" + 
							"      </ns:getDeliveredServiceAliasResponse>\r\n" + 
							"");
			getDeliveredServices.addMessageExchangeInstances(in2, out2, null);

			//TEST des exceptions
			
			final BPELExternalMessage illegalException = new BPELExternalMessageImpl();
			illegalException.setService(this.getName());
			illegalException.setEndpoint(this.getProviderEndpoint().getName());
			illegalException.setQName(new QName("http://sgdt.prosodie/AliasService/1/",
					"getDeliveredServiceAliasRequest"));
			illegalException
					.setMessage("<tns:getDeliveredServiceAlias xmlns:tns=\"http://sgdt.prosodie/AliasService/1/\">\r\n" + 
							"  <deliveredService xmlns:sd=\"http://document.prosodie.com/DeliveredService/1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"sd:DeliveredService\">\r\n" + 
							"    <ns1:uuid xmlns:ns1=\"http://document.prosodie.com/DeliveredService/1/\">SN9</ns1:uuid>\r\n" + 
							"    <ns1:mainAffair xmlns:ns1=\"http://document.prosodie.com/DeliveredService/1/\">\r\n" + 
							"      <ns5:uuid xmlns:ns5=\"http://document.prosodie.com/MainAffair/1/\">AP9000</ns5:uuid>\r\n" + 
							"    </ns1:mainAffair>\r\n" + 
							"  </deliveredService>\r\n" + 
							"</tns:getDeliveredServiceAlias>");

			final BPELExternalMessage illegalFault = new BPELExternalMessageImpl();
			illegalFault.setService(this.getName());
			illegalFault.setEndpoint(this.getProviderEndpoint().getName());
			illegalFault.setQName(new QName("http://sgdt.prosodie/AliasService/1/",
					"getDeliveredServiceAliasIllegalArgumentFault"));
			illegalFault
					.setMessage("<soapenv:Fault xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns=\"http://sgdt.prosodie/AliasService/1/\" xmlns:ns1=\"http://document.prosodie.com/Fault/1/\">\r\n" + 
							"      <faultcode>?</faultcode>\r\n" + 
							"      <faultstring xml:lang=\"?\">?</faultstring>\r\n" + 
							"      <faultactor>?</faultactor>\r\n" + 
							"      <detail>\r\n" + 
							"        <ns:getDeliveredServiceAliasIllegalArgumentFault>\r\n" + 
							"          <ns1:message>Alias Fault</ns1:message>\r\n" + 
							"          <ns1:attributes>%45</ns1:attributes>\r\n" + 
							"        </ns:getDeliveredServiceAliasIllegalArgumentFault>\r\n" + 
							"      </detail>\r\n" + 
							"    </soapenv:Fault>\r\n" + 
							"");
			getDeliveredServices.addMessageExchangeInstances(illegalException, null, illegalFault);
			
			final BPELExternalMessage technicalException = new BPELExternalMessageImpl();
			technicalException.setService(this.getName());
			technicalException.setEndpoint(this.getProviderEndpoint().getName());
			technicalException.setQName(new QName("http://sgdt.prosodie/AliasService/1/",
					"getDeliveredServiceAliasRequest"));
			technicalException
					.setMessage("<tns:getDeliveredServiceAlias xmlns:tns=\"http://sgdt.prosodie/AliasService/1/\">\r\n" + 
							"  <deliveredService xmlns:sd=\"http://document.prosodie.com/DeliveredService/1/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"sd:DeliveredService\">\r\n" + 
							"    <ns1:uuid xmlns:ns1=\"http://document.prosodie.com/DeliveredService/1/\">SN10</ns1:uuid>\r\n" + 
							"    <ns1:mainAffair xmlns:ns1=\"http://document.prosodie.com/DeliveredService/1/\">\r\n" + 
							"      <ns5:uuid xmlns:ns5=\"http://document.prosodie.com/MainAffair/1/\">AP9001</ns5:uuid>\r\n" + 
							"    </ns1:mainAffair>\r\n" + 
							"  </deliveredService>\r\n" + 
							"</tns:getDeliveredServiceAlias>");

			final BPELExternalMessage technicalFault = new BPELExternalMessageImpl();
			technicalFault.setService(this.getName());
			technicalFault.setEndpoint(this.getProviderEndpoint().getName());
			technicalFault.setQName(new QName("http://sgdt.prosodie/AliasService/1/",
					"getDeliveredServiceAliasTechnicalFault"));
			technicalFault
					.setMessage("<soapenv:Fault xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns=\"http://sgdt.prosodie/AliasService/1/\" xmlns:ns1=\"http://document.prosodie.com/Fault/1/\">\r\n" + 
							"      <faultcode>?</faultcode>\r\n" + 
							"      <faultstring xml:lang=\"?\">?</faultstring>\r\n" + 
							"      <faultactor>?</faultactor>\r\n" + 
							"      <detail>\r\n" + 
							"        <ns:getDeliveredServiceAliasTechnicalFault>\r\n" + 
							"          <ns1:message>Alias Fault</ns1:message>\r\n" + 
							"          <ns1:attributes>%45</ns1:attributes>\r\n" + 
							"        </ns:getDeliveredServiceAliasTechnicalFault>\r\n" + 
							"      </detail>\r\n" + 
							"    </soapenv:Fault>\r\n" + 
							"");
			getDeliveredServices.addMessageExchangeInstances(technicalException, null, technicalFault);
			
			
			
			
			
			
			
			
			
			
		} catch (final CoreException e) {
			throw new BPELException(e);
		}
		System.out.println("FIN getDeliveredServiceAlias");
	}
	
	
}
