Tutorial : How to create an Email?

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. Generating an Email Bean


<?php
	//We're going to call this with our newly created contextId, therefore applying it to our current session.
	function generateEmailBean($contextId){
		/* So let's call the handle request with our contextId, to the campaign email facade bean class, we want to call the 
		   create process, and pass in no data. */
		$result = handleRequest($contextId, "bus_facade_campaign_email", "create", null, null);
		/* The result data of the response is the email entity, and in here is a variable called bean id
		   This is used to reference our newly generated bean in the bean store. */
		$emailBean = $result['resultData']['bus_entity_campaign_email']['beanId'];

		//So let's return this so we can use it, update it and write this back to the platform.
		return $emailBean;
	}
?>

//We're going to call this with our newly created contextId, therefore applying it to our current session.
public String generateEmailBean(String contextId)
{
   /* So let's call the handle request with our contextId, to the campaign email facade bean class, we want to call the 
      create process, and pass in no data. */
   Hashtable result = this.sendRequest(contextId, "bus_facade_campaign_email", "create", null, null);
   /* The result data of the response is the email 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 emailBean = (String)((Hashtable)resultData["bus_entity_campaign_email"])["beanId"];
    //So let's return this so we can use it, update it and write this back to the platform.
    return emailBean;
 }
As this is .NET we're going to be adding this as a method to the object we created in the first step.

4. Updating the email bean

Here is theemailConfiguration, that is essentially the entitydata of the request : ( See here: for required and optional variables that can be used)

<?php
	$emailConfiguration = array(
						// We're concatenating this with the unix timestamp so it's unique!
						'messageName_base64' 		=> base64_encode("Testemail".date('U'),

						// We want the html of our email body (content) to be this string(blob)						
						'bodyHtml_base64' 			=> base64_encode("<h1>Header</h1><br />
												           Email to {~first_name~}<hr />"),
						// And we want the plain text of our email body (content) to be this string (blob)
						'bodyPlain_base64'			=> base64_encode("Header
															----------
															Email to {~first_name~})",
						
						// We want to set the subject line for our new email
						'subject_base64' 			=> base64_encode("Testing API Email Creation"),

						// optional parameters I want to set are for tracking purposes, so...
						'googleTrackingEnabledInd' 	=> "Y",
						'trackHtmlInd'				=> "Y",
						'trackPlainInd'				=> "Y"
						);
?>

//First we need to create a method for encoding strings to base 64
public static String Base64_encode(String PlainText){
	var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(PlainText);
	return System.Convert.ToBase64String(plainTextBytes);
}

Hashtable 	emailConfiguration = new Hashtable();
			//Let's start with a unique name of the email
		  	emailConfiguration.Add("messageName_base64", Pure360API.Base64_encode("Testemail");

		  	//We want the html of our email body (content) to be this string(blob)
		  	emailConfiguration.Add("bodyHtml_base64", Pure360API.Base64_encode("<h1>Header</h1><br />Email to {~first_name~}<hr />"));

		  	// And we want the plain text of our email body (content) to be this string (blob)
			emailConfiguration.Add("bodyPlain_base64", Pure360API.Base64_encode("Header\n----------\nEmail to {~first_name~}"));

			// We want to set the subject line for our new email
			emailConfiguration.Add("subject_base64", Pure360API.Base64_encode("Testing API Email Creation"));

			// Optional parameters I want to set are for tracking purposes so...
			emailConfiguration.Add("googleTrackingEnabledInd", "Y");
			emailConfiguration.Add("trackHtmlInd", "Y");
			emailConfiguration.Add("trackPlainInd", "Y");


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

//Let's make sure our entityData is set, by assigning the emailConfiguration to this
public String updateemailBean(String contextId, String emailBean, Hashtable emailConfiguration)
{
	/* Don't forget to add the beanId to the entityData, so the system knows to update the 
	   new entity bean in the bean store!  */
	Hashtable entityData = emailConfiguration;
    entityData.Add("beanId", emailBean);
    //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_email", "update", emailConfiguration, 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.

5. Write the email bean back to the platform


<?php
	function storeEmailBean($contextId, $emailBean){
		//All we need to pass in is the reference to the entity bean in the bean store...
		$entityData = array(
						'beanId' = $emailBean;
						);
		//...and perform the store process on this
		$result = handleRequest($contextId, "bus_facade_campaign_email", "store", $entityData, null);
		return $result['result'];
	}
?>

public String storeEmailBean(String contextId, String emailBean)
{
    //All we need to pass in is the reference to the entity bean in the bean store...
    Hashtable entityData = new Hashtable();
    entityData.Add("beanId", emailBean);
    //...and perform the store process on this
    Hashtable result =this.sendRequest(contextId, "bus_facade_campaign_email", "store", 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.

6. 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 created a email in the system.

Things to remember