Tutorial : How to append to an existing contact list?

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 a new list bean


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

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

//We're going to call this with our newly created contextId, therefore applying it to our current session.
public String generateListBean(String contextId)
{
   /* So let's call the handle request with our contextId, to the campaign list facade bean class, we want to call the 
      create process, and pass in no data. */
   Hashtable result = this.sendRequest(contextId, "bus_facade_campaign_list", "create", null, null);
   /* The result data of the response is the list 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 listBean = (String)((Hashtable)resultData["bus_entity_campaign_list"])["beanId"];
    //So let's return this so we can use it, update it and write this back to the platform.
    return listBean;
 }
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 list bean

List Data:

<?php
	$pasteFile_base64 = base64_encode("email,signup,dob,name\ntest1@test.com,2013-01-01,26/11/1987,bob\ntest2@test.com,2013-02-02,01/04/1989,bill");
?>

//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);
}
String pasteFile_base64 = Pure360API.Base64_encode("email,signup,dob,name\ntest1@test.com,2013-01-01,26/11/1987,bob\ntest2@test.com,2013-02-02,01/04/1989,bill");

listConfiguration : EntityData for the generic function including the above pasteFile


<?php
	$listConfiguration = array(
						//The name of the existing list in the platform
						'listName_base64' 		=> "Master List",
						//Here we're assinging our data string, mentioned above
						'pasteFile_base64'		=> $pasteFile_base64,
						
						//In our pasteFile, the email address is at index 0 (the leftmost)
						'emailCol' 		=> 0,
						//And our alternative signup date comes next at index 1
						'signUpDateCol' => 1,

						//As for our custom data...
						//we want the DOB next, so let's give it the index and a name
						'field1Col' 	=> 2,
						'field1Name'    => "DOB",

						//We need to tell the system that this field is of type "date"
						'field1DataType' => 'date',
						//but we also have to give ut a data format, as it's a date
						'field1DataFormat' => 'dd/mm/yyyy',

						'field2Col' 	=> 3,
						'field2Name'	=> "Name"

						//Our default language is UTF8, but in this case we want standard english
						'languageCode'  => "en_GB.ISO8859-15",

						//let's ensure we get notified
						'uploadFileNotifyEmail_base64' => base64_encode("me@myemail.com"),
						'uploadFileNotifyType'  => "EMAIL",

						//Not forgetting, this is an APPEND (not a CREATE or REPLACE)
						'uploadTransactionType' => "APPEND"
						);
?>

Hashtable 	listConfiguration = new Hashtable();
			//The name of the list  within the platform
		  	listConfiguration.Add("listName", Pure360API.base64_encode("Master List"));
		  	//Here we're assinging our data string, mentioned above
		  	listConfiguration.Add("pasteFile", pasteFile_base64);

		  	//In our pasteFile, the email address is at index 0 (the leftmost)
		  	listConfiguration.Add("emailCol", 0);
		  	//And our alternative signup date comes next at index 1
		  	listConfiguration.Add("signUpDateCol", 1);

		  	//As for our custom data...
			//we want the DOB next, so let's give it the index and a name
		  	listConfiguration.Add("field1Col", 2);
		  	listConfiguration.Add("field1Name", "DOB");

			//We need to tell the system that this field is of type "date"
			listConfiguration.Add("field1DataType", "date");
		  	//but we also have to give ut a data format, as it's a date
		  	listConfiguration.Add("field1DataFormat", "dd/mm/yyyy");

		  	listConfiguration.Add("field2Col", 3);
		  	listConfiguration.Add("field2Name", "Name");

		  	//Our default language is UTF8, but in this case we want standard english
		  	listConfiguration.Add("languageCode", "en_GB.ISO8859-15");

		  	//let's ensure we get notified
		  	listConfiguration.Add("uploadFileNotifyEmail", Pure360API.base64_encode("me@myemail.com"));
		  	listConfiguration.Add("uploadFileNotifyType", "EMAIL");

		  	//Not forgetting, this is an APPEND (not a CREATE or REPLACE)
		  	listConfiguration.Add("uploadTransactionType", "APPEND");


<?php
	function updateListBean($contextId, $listBean, $listConfiguration){
		//Let's make sure our entityData is set, by assigning the listConfiguration to this
		$entityData = $listConfiguration;
		/* 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'] = $listBean;
		//Now we want to update the entity bean in the bean store we've created with this configuration
		$result = handleRequest($contextId, "bus_facade_campaign_list", "update", $entityData, null);
		return $result['result'];
	}
?>

//Let's make sure our entityData is set, by assigning the listConfiguration to this
public String updateListBean(String contextId, String listBean, Hashtable listConfiguration)
{
	/* 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 = listConfiguration;
    entityData.Add("beanId", listBean);
    //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_list", "update", listConfiguration, 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 list bean back to the platform


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

public String storeListBean(String contextId, String listBean)
{
    //All we need to pass in is the reference to the entity bean in the bean store...
    Hashtable entityData = new Hashtable();
    entityData.Add("beanId", listBean);
    //...and perform the store process on this
    Hashtable result =this.sendRequest(contextId, "bus_facade_campaign_list", "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 appended to a list in the system, with an alternative date of signup, and two custom fields (one of which is a date).

Things to remember