Tutorial : How to create (schedule) a delivery?

1. Creating the generic method

Let's start by writing a function to handle our requests and responses via the languages SOAP library.

<?php
	//We're going to structure this to look very similar to the WSDL
	function handleRequest($contextId, $className, $processName, $entityData, $processData){
		//Let's instantiate a soapClient object, using the wsdl as a blueprint and setup a trace
		$soapClient = new SoapClient('http://paint.pure360.com/paint.pure360.com/ctrlPaint.wsdl', array('trace' => TRUE));
		//Now let's send the request via the handleRequest specified in the wsdl
		$RESULT = $soapClient->handleRequest($contextId, $className,	$processName, $entityData, $processData);
		return $RESULT;
	}
?>
	

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using com.pure360.paint;
/*
 *  As this is .NET we're going to have to do this in objects and classes, as a bit of pre-pre ground work
 *  don't forget to Add a service, click advanced, and add the wsdl as a web service reference 
 *  http://paint.pure360.com/paint.pure360.com/ctrlPaintLiteral.wsdl (NOT as simply a service reference)
 *  For a how to follow this link : Configuring VS with .NET
 */
public class Pure360API
{
	public Pure360API()
	{
	}
    //We're going to structure this to look very similar to the WSDL
    public Hashtable sendRequest(   String contextId, 
    								String className, 
    								String processName, 
    								Hashtable entityData, 
    								Hashtable processData)
    {
        /* As we're using pairs (we can't use associative arrays) we need to use a method of this class to 
           apply a quick conversion */
        paintKeyValuePair[] entityPairs = this.convertDataToPairs(entityData);
        paintKeyValuePair[] processPairs = this.convertDataToPairs(processData);
   
       /* We also need to instantiate the paintService we referenced earlier - like the SoapClient in php this is structured
        * based on the WSDL, and uses this blueprint, this will be handling our requests and responses
        */
        paintService API = new paintService();
        //So as we're using pairs, the result will come back as an array of pairs
        paintKeyValuePair[] resultPairs =  API.handleRequest(contextId,
                                                className,
                                                processName,
                                                entityPairs,
                                                processPairs
                                            );
        //So again we need to do a bit of converting
        Hashtable resultData = this.convertPairsToHashtable(resultPairs);
        //and we're going to return the hashtable
        return resultData;

    }

    //The two following methods are used to convert the data to and from pairs
    protected paintKeyValuePair[] convertDataToPairs(Hashtable source)
    {
    	//if the source is null, let's just create a new Hashtable preventing errors
        if (source == null) { source = new Hashtable(); }
        //We also need to create a destination for our pairs as we create them of the same size as the source
        paintKeyValuePair[] convertedPairs = new paintKeyValuePair[source.Count];
        //And we need a counter to know which element we're on
        int i = 0;
        foreach (String key in source.Keys)
        {
            paintKeyValuePairValue newValue = new paintKeyValuePairValue();
            paintKeyValuePair newPair = new paintKeyValuePair();
            //If we come across a hashtable, use a recursive function to convert this
            if (source[key] is Hashtable)
            {
                newValue.arr = this.convertDataToPairs((Hashtable)source[key]);
            }
            //if not let's cast it as a string
            else
            {
                newValue.str = source[key].ToString();
            }
            //create the new paintKeyValuePair from these
            newPair.key = key;
            newPair.value = newValue;
            //and push this on to the new convertedPairs array at index assigned by the counter
            convertedPairs[i] = newPair;
            //finally let's increment that counter so we don't overwrite anything
            i++;
        }
        //and return those convertedPairs
        return convertedPairs;
    }
   protected Hashtable convertPairsToHashtable(paintKeyValuePair[] source)
    {
        if (source != null)
        {
            Hashtable convertedHashtable = new Hashtable();
            for (int i = 0; i < source.Length; i++)
            {
                paintKeyValuePair item = source[i];
                String key = item.key;
                String valueStr = item.value.str;
                paintKeyValuePair[] valueArr = item.value.arr;
                if (valueArr != null)
                {
                    convertedHashtable.Add(item.key, convertPairsToHashtable(valueArr));
                }
				else
				{
					convertedHashtable.Add(item.key, valueStr);
				}
            }
            return convertedHashtable;
        }
        else
        {
            return null;
        }
    }

The .NET code has more setup required, mainly due to the nature of the language, and the data structures it can handle being more strictly typed
As the WSDL is defined to handle only associative arrays or pairs, we need to do some converting to and from pairs, as they are not a native type

As you can see this function follows the blueprint of the WSDL and uses the handleRequest generic function in the WSDL, and receives the Result response.

2. Creating a login method


<?php
	//So this is going to take in a username and password as the parameters and hopefully return the contextId
	function login($userName, $password){
		//ok, this facade therefore takes in a username and password as the entityData (parameters) for the process "Login"
		$entityData = array(
							'userName' => $userName,
							'password' => $password
						);
		/* We're going to send this without a context id, as we don't have one, to the context facade bean, with the process 
		  login, and the entity data  */
		$result = handleRequest(null, "bus_facade_context", "login", $entityData, null);
		/* In the result data of the response is the context entity, and in here is a variable called bean id.
		   This is used to tie the whole session together in the bean store */
		$contextId = $result['resultData']['bus_entity_context']['beanId'];
		//And finally let's send back the contextId
		return $contextId;
	}
?>

//So this is going to take in a username and password as the parameters and hopefully return the contextId
public String login(String userName, String password){
	//ok, this facade therefore takes in a username and password as the entityData (parameters) for the process "Login"
	Hashtable entityData = new Hashtable();
	entityData.Add("userName", userName);
	entityData.Add("password", password);
	/* We're going to send this without a context id, as we don't have one, to the context facade bean, with the process 
	login, and the entity data  */
	Hashtable result = this.sendRequest(null, "bus_facade_context", "login", entityData, null);
	/* In the result data of the response is the context entity, and in here is a variable called bean id.
	This is used to tie the whole session together in the bean store */
	Hashtable resultData = (Hashtable)result["resultData"];
	String contextId = (String)((Hashtable)resultData["bus_entity_context"])["beanId"];
	//And finally let's send back the contextId
	return contextId;
}
As this is .NET we're going to be adding this as a method to the object we created in the first step.

3. Searching for the messageId to schedule the delivery of said message


<?php
	//Our two parameters are our contextID, tying the session together, and our messageName search parameter
	function searchMessageId($contextId, $messageName){
		/* So let's call the handle request with our contextId.
		   We're using the message facade bean class.
		   We want to call the "search" process.
		   We want to load the entityData on to the search bean, in this case the "messageName" */
		  $entityData = array('messageName' => $messageName);
		$result = handleRequest($contextId, "bus_facade_campaign_message", "search", $entityData, null);
		/* The result data of the response is the message search bean, and in here is a an array called "idData" that should contain a single element.
		   The value of this element is our messageId */
		$messageId = $result['resultData']['bus_search_campaign_message']['idData'][0];

		//Let's return just the messageId value
		return $listId['messageId'];
	}
?>

//We're going to call this with our newly created contextId, therefore applying it to our current session. Also we're going to pass in the name of the message that we want to search for.
public String searchMessageId(String contextId, String messageName)
{
   /* So let's call the handle request with our contextId.
	  We're using the list facade bean class.
	  We want to call the "search" process.
	  We want to load the entityData on to the search bean, in this case the "messageName".
*/
   Hashtable entityData = new Hashtable();
   entityData.add('messageName', messageName);
   Hashtable result = this.sendRequest(contextId, "bus_facade_campaign_message", "search", entityData, null);
   /* The result data of the response is the message search bean, and in here is a an hashtable called "idData" that should contain a single element.
		   The value of this element is our messageId */
    Hashtable resultData = (Hashtable)result["resultData"];
    
    Hsahtable messageId = (Hashtable)((Hashtable)resultData["bus_search_campaign_message"])["idData"][0];
   
    //So let's return just the messageId value
    return String messageId["messageId"];
 }
As this is .NET we're going to be adding this as a method to the object we created in the first step.

4. Searching for the listId(s) to schedule the delivery to


<?php
	//Our two parameters are our contextID, tying the session together, and our listName search parameter
	function searchListId($contextId, $listName){
		/* So let's call the handle request with our contextId.
		   We're using the list facade bean class.
		   We want to call the "search" process.
		   We want to load the entityData on to the search bean, in this case the "listName" */
		  $entityData = array('listName' => $listName);
		$result = handleRequest($contextId, "bus_facade_campaign_list", "search", $entityData, null);
		/* The result data of the response is the list search bean, and in here is a an array called "idData" that should contain a single element.
		   The value of this element is our listId */
		$listId = $result['resultData']['bus_search_campaign_list']['idData'][0];

		//Let's return just the listId value
		return $listId['listId'];
	}
?>

//We're going to call this with our newly created contextId, therefore applying it to our current session. Also we're going to pass in the name of the list that we want to search for.
public String searchListId(String contextId, String listName)
{
   /* So let's call the handle request with our contextId.
	  We're using the list facade bean class.
	  We want to call the "search" process.
	  We want to load the entityData on to the search bean, in this case the "listName".
*/
   Hashtable entityData = new Hashtable();
   entityData.add('listName', listName);
   Hashtable result = this.sendRequest(contextId, "bus_facade_campaign_list", "search", entityData, null);
   /* The result data of the response is the list search bean, and in here is a an hashtable called "idData" that should contain a single element.
		   The value of this element is our listId */
    Hashtable resultData = (Hashtable)result["resultData"];
    
    Hsahtable listId = (Hashtable)((Hashtable)resultData["bus_search_campaign_list"])["idData"][0];
   
    //So let's return just the listId value
    return String listId["listId"];
 }
As this is .NET we're going to be adding this as a method to the object we created in the first step.

5. Generating a deliver bean in the temporary store


<?php
	//We're going to call this with our contextId
	function generateDeliveryBean($contextId){
		/* So let's call the handle request with our contextId.
		   We're handling delivery beans so need to reference the delivery facade class.
		   We're calling a "create" process to generate a new delivery entity bean.
		 */
		$result = handleRequest($contextId, "bus_facade_campaign_delivery", "create", null, null);
		/* The result data of the response is the delivery entity, and in here is a variable called bean id
		   This is used to reference our newly generated bean in the bean store. */
		$deliveryBean = $result['resultData']['bus_entity_campaign_delivery']['beanId'];	
		//so return this....
		return $deliveryBean;
	}
?>

//We're going to call this with our newly created contextId, therefore applying it to our current session.
public String generateDeliveryBean(String contextId)
{
   /* So let's call the handle request with our contextId.
      We're handling delivery beans so need to reference the delivery facade class.
	  We're calling a "create" process to generate a new delivery entity bean.*/
   Hashtable result = this.sendRequest(contextId, "bus_facade_campaign_delivery", "create", null, null);
   /* The result data of the response is the filter entity, and in here is a variable called bean id
	  This is used to reference our newly generated bean in the bean store. */
    Hashtable resultData = (Hashtable)result["resultData"];
    String deliveryBean = (String)((Hashtable)resultData["bus_entity_campaign_delivery"])["beanId"];  
    //And return this...
    return deliveryBean;
 }
As this is .NET we're going to be adding this as a method to the object we created in the first step.

6. Updating the delivery bean in the bean store

Here is the delivery configuration:

<?php
	$deliveryConfiguration = array(
				//We need a datetime for our delivery DD/MM/YYYY HH:MM
				"deliveryDtTm" => "01/01/1970 00:00",
				//We need our listIds we generated in (4)
				"listIds"	 => array($listId),
				//We need out messageId in (3)
				"messageId" => $messageId
			);
?>

Hashtable 	listIds = new Hashtable();
			listIds.add("0", listId);
Hashtable 	deliveryConfiguration = new Hashtable();
			//We need a datetime for our delivery DD/MM/YYYY HH:MM
			deliveryConfiguration.add("deliveryDtTm", "01/01/1970 00:00");
			//Our listId we generated in (4)
			deliveryConfiguration.add("listIds", listIds);
			//We need out messageId in (3)
			deliveryConfiguration.add("messageId", messageId);

<?php
	function updateDeliveryBean($contextId, $beanId, $deliveryConfiguration){
		//Let's make sure our entityData is set, by assigning the deliveryConfiguration to this
		$entityData = $deliveryConfiguration;
	 	// Don't forget to add the beanId to the entityData to update the new entity bean in the bean store!
		$entityData['beanId'] = $beanId;
		//Now we want to update the entity bean in the bean store we've created with this configuration
		$result = handleRequest($contextId, "bus_facade_campaign_delivery", "update", $entityData, null);
		return $result['result'];
	}
?>

public String updateDeliveryBean(String contextId, String beanId, Hashtable deliveryConfiguration){
	//Let's make sure our entityData is set, by assigning the filterConfiguration to this
	Hashtable entityData = deliveryConfiguration;
	// Don't forget to add the beanId to the entityData to update the new entity bean in the bean store! 
	entityData.add("beanId", beanId);
	//Now we want to update the entity bean in the bean store we've created with this configuration
	Hashtable result = this.sendRequest(contextId, "bus_facade_campaign_delivery", "update" entityData, null);
	return (String)result['result'];
}
As this is .NET we're going to be adding this as a method to the object we created in the first step.

7. Storing the delivery bean


<?php
	function storeDeliveryBean($contextId, $beanId){
		//The entity data is a reference to our bean in the bean store
		$entityData = array('beanId' => $beanId);
		//Now we want to store the entity bean, validating the configuration fields and writing this back to the platform
		$result = handleRequest($contextId, "bus_facade_campaign_delivery", "store", $entityData, null);
		return $result['result'];
	}
?>

public String storeDeliveryBean(String contextId, String beanId){
	//The entity data is a reference to our bean in the bean store
	Hashtable entityData = new Hashtable();
	entityData.add("beanId", beanId);
	//Now we want to store the entity bean, validating the configuration fields and writing this back to the platform
	Hashtable result = this.sendRequest(contextId, "bus_facade_campaign_delivery", "update" entityData, null);
	return (String)result['result'];
}
As this is .NET we're going to be adding this as a method to the object we created in the first step.

8. End the session


<?php
	function logout($contextId){
		//Let's finish by calling the login process on the context facade, removing this session from the bean store...
		$result = handleRequest($contextId, "bus_facade_context", "logout", null, null);
		return $result['result'];
	}

?>

public String logout(String contextId)
{
    //Let's finish by calling the login process on the context facade, removing this session from the bean store...
    Hashtable result = this.sendRequest(contextId, "bus_facade_context", "logout", null, null);
    return (String)result["result"];
}
As this is .NET we're going to be adding this as a method to the object we created in the first step.

Hey Presto! We've shceduled a delivery from the system, this is attached to a list and message, this will also send on 01/01/1970 00:00

Things to remember