Jump to: navigation, search

Java samples

The following code fragments are designed to illustrate how to accomplish a particular task; they generally do not include robust error handling.

Basic Authentication

For every service accessed it is necessary to create the service and attach your credentials to that service as per the following example. The following sample assumes that only the Contact Management and Platform Management services are required. The authentication outlined assumes the WS-Security framework as discussed in the section entitled Integration Requirements.

ContactManagementServicePortType contactServicePort;
PlatformManagementServicePortType platformServicePort;
 
platformServicePort = StubFactory.createService("PlatformManagementService",
PlatformManagementServicePortType.class,
"https://serviceX.soundbite.com/SOAPAPI/PlatformManagementService";,
"user", "pwd");
contactServicePort = StubFactory.createService("ContactManagementService",
ContactManagementServicePortType.class,
"https://serviceX.soundbite.com/SOAPAPI/ContactManagementService";,
"user", "pwd");

Simple invocation

The simplest method supported is the showSystemInfo() method which returns basic information about the version of the platform and other static data.

Response response = platformServicePort.showSystemInfo();
for (Top top : response.getData()) {
System.out.println(((Attribute)top).getName() + " = " + ((Attribute)top).getValue());

Error/Warning Handling

All methods potentially return both a set of errors and a set of warnings. In general clients should rely on the associated code from the Error/Warning and create their own text to relay to their customers in their language of choice. These codes will remain constant over time and come from the finite set defined in the Error/Warning section. The message provided with the Error/Warning is for convenience/development only.

Response response = platformService.showSystemInfo();
if (response.getErrors() != null || response.getWarnings() != null)  {
if (response.getErrors() != null && !response.getErrors().isEmpty())
for (Error error : response.getErrors())
System.out.printf("%s %s %s\n",
error.getCode(),error.getMessage(),error.getContext());
if (response.getWarnings() != null && !response.getWarnings().isEmpty())
      for (Warning warning : response.getWarnings()) {
         System.out.printf("%s %s %s\n",
warning.getCode(), warning.getMessage(), warning.getContext());
}

Create and populate a List

Lists exist in their own right independent of the presence of any contacts in that list. In the following example the list is assumed not to exist and is initially created and then a set of contacts are added to that list.

// Setup the Account (use the account number provided by Professional Services)
Org account = new Org();
account.setType("Account");
account.setInternalId(400000034);
 
// Setup the List
List list = new List();
list.setName("Truancy Notification");
list.setType("Standard");
list.setBaseEntity(account);
 
// Create a new empty list
Response response;
response = contactServicePort.createList(list, account); 
List newList = (List) response.getData().get(0); 
 
// Add contacts to the list
response = contactServicePort.addToListUsingString(newList, "B124908, 617.875.9182", "");
Job job = (Job)response.getData().get(0);

Create a Script

This fragment creates a new script based on a pre-existing template by replacing a prompt with new content. It assumes the existence of the account object created in the 'Create and populate a List' example.

ArrayOfJavaLangstringLiteral prompts = new ArrayOfJavaLangstringLiteral();
prompts.getJavaLangstring().add("message");
ArrayOfbase64BinaryLiteral promptContents = new ArrayOfbase64BinaryLiteral();
promptContents.getBase64Binary().add(loadPrompt("test/prompts/jewel.wav")); 
 
// Create a new script based on a template and a new prompt file
Script scriptTemplate = new Script();
scriptTemplate.setExternalId("BasicAlertTemplate");
 
// Call service
Response response = campaignServicePort.createScript(scriptTemplate,
            "AlertBoston", prompts, promptContents, account);
 
Script newScript = (Script) response.getData().get(0);
 
// Load prompt contents based on file specified
private byte[] loadPrompt(String name) {
byte[] ret = null;
 
try {
File source = new File(name);
FileInputStream input = new FileInputStream(source);
ret = new byte[(int)source.length()];
input.read(ret);
}
catch (Exception e) {
System.err.println("Internal Error: " + e.getMessage());
}
 
return ret;
}

Setting an attribute on a domain object

Many of the domain objects have a set of attributes in addition to the core properties. This sample illustrates setting one of these attributes as part of the object creation.

// Create a script – make sure the list auto-expires at the end of the day
Script scriptTemplate = new Script();
scriptTemplate.setExternalId("BasicAlertTemplate");
Attribute expireAfterHours = new Attribute();
expireAfterHours.setName("expireAfterHours");
expireAfterHours.setValue("0");
scriptTemplate.getAttributes().add(expireAfterHours);

Initiate simple SubCampaign (outreach to customers)

To initiate outreach to a set of customers (defined by list) with a particular interaction (defined by script) use the createSubCampaign method. The behavior of this SubCampaign can be adjusted by using a prototype SubCampaign method (see overrides below).

XXXXX
// Select the existing campaign (i.e. Strategy) for all calling
campaign = new Campaign();
campaign.Name = "200808";
campaign.ExternalId = "200808";
 
// Specify the name of the Campaign to use and the name of this SubCampaign
SubCampaign overrides = new SubCampaign();
overrides.Campaign = campaign;
overrides.Name = "MySimpleOutreach";
 
// Initiate calling on the new SubCampaign
response = campaignService.createSubCampaign(overrides, list, script, account);
SubCampaign subCampaign = (SubCampaign)response.Data[0];


Monitor status of an existing SubCampaign

With a handle to an existing SubCampaign it is possible to monitor the status using showSubCampaignState. This will return detailed information about the progress of a particular SubCampaign.

XXXXX
Response response = campaignService.showSubCampaignState(subCampaign);
SubCampaignState subCampaignState = (SubCampaignState)response.Data[0];
Console.WriteLine("SubCampaign: " + subCampaign.Name + ", State: " + subCampaignState.state);
Console.WriteLine("\tdelivered: " + subCampaignState.deliveredCount +
 ", failed: " + subCampaignState.failedCount);
Console.WriteLine("\tfiltered: " + subCampaignState.filteredCount +
 ", not attempted: " + subCampaignState.notAttemptedCount);

Manage an existing SubCampaign

With a handle to an existing SubCampaign it is possible to manage (i.e. pause, resume, stop, start) using changeSubCampaignState.

XXXXX
response = campaignService.showSubCampaignState(subCampaign);
SubCampaignState subCampaignState = (SubCampaignState)response.Data[0];
// Pause the SubCampaign
subCampaignState.state = "Paused";
response = campaignService.changeSubCampaignState(subCampaignState);

Retrieve results from a SubCampaign

To retrieve results from a SubCampaign use createReportRequest with the Type of the report determining the style of results required. This will queue an asynchronous request to generate a results set; once this request is complete the results can be downloaded.

XXXXX
ReportRequest reportRequest = new ReportRequest();
reportRequest.Type = "All Attempts";
reportRequest.BaseEntity1 = subCampaign;
reportRequest.Frequency = "Now";
 
// Request a detailed report for the SubCampaign
Response response = reportService.createReportRequest(reportRequest, account);
 
Job job = (Job) response.Data[0];
JobState jobState = new JobState();
 
// Wait for queued job to complete
do {
response = platformService.showJobStateUsingJob(job);
jobState = (JobState)response.Data[0];
Thread.Sleep(30000);
} while (jobState.State.Equals("Pending") || jobState.State.Equals("Running"));
 
// Job is completed now retrieve results
response = platformService.showJobResultAsString(job);
String result = ((JobResult)response.Data[0]).StringResult;

Feedback

Comment on this article:

blog comments powered by Disqus
This page was last modified on 6 September 2016, at 11:25.