P-I: Add an Attachment to a CSR with Java Web Service Calls

Intended Audience: Technical Users and Administrators of Stat

Intended Subject: Add an Attachment to a CSR via Java Web Service calls

Intended Reaction: Happy Stat Customers

Keywords: Stat, Web Services, Java, CSR, Attachments

Hello fellow Stat Technical Users & Administrators!

Welcome to a new year and a new series of Blog posts!

This series will focus on getting the most out of Stat integration with other systems such as keeping documents synchronized as they are added, updated or deleted in different locations or applications.

What’s the goal of this Post?

The goal for this post is to enable your technical team to quickly setup things for documentation synchronization across various applications and/or locations by
adding an object to a CSR.

Examples of this may be:

  • When a document gets added or updated in a CSR, update the document in another location or vise-versa

  • Attach Log Files, Screenshots, Change Control Visio diagrams and the like

Caution & User Reference:

If you have not worked with Stat Web Services in the past, this is not the place to begin. You’ll need to setup your environment and ensure other settings are verified.

If you are not familiar with Stat Web Services, please refer to “Appendix G: Stat Web Services” in the Stat System Administrators Guide.


You can also find sample code and further documentation on the Stat Installation CD. On the CD you will see a directory called “web_services_sample“.

In this directory is a PDF on Stat Web Services along with a Zip file called StatWS.zip The zip file contains the sample code and additional instructions on setting
up your environment and running the samples included.


Let’s Go!

For this specific post we will look at adding an attachment to a CSR.

Add a new attachment to a CSR:

To add a new attachment to a CSR we need to make the following Web Service call: addCSRAttachments()

For this, I’m using a Java Source/Class file named = StatWSCSRAttachCalls.java, and StatWSCSRAttachCalls.class respectively.


In this class there are three calls available to me:


/* StatWSCSRAttachCalls CSR Attachment Related Calls:

1 - retrieveCSRAttachments

2 - addCSRAttachments

3 - deleteCSRAttachments


Remembering that we need to first establish a connection to the Stat Web Service as part of the process, we will pass in our User Id and Password along with the call to make.


For the test environment that would be:


My actual call is %JAXWS_RUN_COMMAND% StatWSCSRAttachCalls WIN-52C9JR1VJH7 8080 MyUserID MyPassword 2


Here’s a screenshot of the Java call running:


Let’s see the results within Stat:

Before adding a new attachment we can see the CSR currently has an existing attachment: (That is not a prerequisite.)


After adding the attachment we see there are now two attachments:


Now let’s see what’s happening in the code:

Here’s the Attachment Type Bean:

// --------------------------------------------------

// Begin AttachmentType:

<complexType name="AttachmentType">


<element name="fileId" type="xsd:long"/>

<element name="filename" type="xsd:string"/>

<element name="contentType" type="xsd:string"/>

<element name="comments" type="xsd:string"/>

<element name="content" type="xsd:base64Binary" xmime:expectedContentTypes="application/octet-stream"/>



// End AttachmentType
// --------------------------------------------------

Here’s the call: *1, *2

%JAXWS_RUN_COMMAND% StatWSCSRAttachCalls WIN-52C9JR1VJH7 8080 MyUserID MyPassword 2


public static void main(String[] args) throws Exception {


… Set Variables and the like…


// ----------------------------------------------------

// Establish a Connection to Stat Web Services Central Agent:

try {

connectId = loginPort.connect(user, pass);

System.out.println(" ");

System.out.println("Stat Web Service ConnectId = " + connectId);

System.out.println(" ");

} catch(WSLoginFault e) {

WSLoginFaultType ue = e.getFaultInfo();

System.out.println(" ");

System.out.println("Stat Web Service ERROR: " + ue.getMessage());

System.out.println(" ");



… Set CSR ID, Service Domain Variables and the like…


case 2: // Do addCSRAttachments:

// ===================================================================================

// 2 = addCSRAttachments (long connectId, String sdcd, long csrId, List<AttachmentType> attachments) throws WSCsrFault;

// Users will call this method to add attachments to an existing CSR in Stat.

// Server will throw an exception in case of an invalid Service Domain or CsrId, or if CSR is currently open/locked.

// Server will throw an exception if user has restriction, CSR is Closed, Assigned to another user

// Server will throw an exception if the attachment list is empty.

// CSR History records are inserted for added attachments.

// In Parameters:

// connectId - A valid connect Id obtained by calling WSLoginService.connect.

// sdcd – Service Domain Code.

// csrid – A valid CSR Number (Integer).

// attachments – List of attachments

// Return Parameter:

// void - No Return Value.

// ===================================================================================

System.out.println(" ");

System.out.println("Call #" + calltorun + ": addCSRAttachments...");

System.out.println(" ");

try {

// --------------------------------------------------

// Set AttachmentType attachments:

List<AttachmentType> attList = new ArrayList<AttachmentType>();


// --------------------------------------------------

// File #1:

// NOTE: Real world these values would be CLI Parameters or Variable Values. Hardcoded for sample code length)

AttachmentType att = new AttachmentType();

DataHandler content = new DataHandler(new FileDataSource("C:/Temp/BillAttached_1.txt"));

att.setComments("Bill Attached This File Via Web Services for CSR #" + icsr + ".");






//Enable MTOM on client side

SOAPBinding binding = (SOAPBinding)((BindingProvider)csrPort).getBinding();



// --------------------------------------------------

// Do addCSRAttachments Call:

csrPort.addCSRAttachments(connectId, sdcd, csrId, attList);


System.out.println(" Add Attachments to CSR ID: " + csrId + " - Filename #1 = " + att.getFilename());


} catch(WSCsrFault e) {

WSCsrFaultType ue = e.getFaultInfo();

// Weed out 'No Rows Returned' as a Non-Error.

String strOrig = ue.getMessage();

int intIndex = strOrig.indexOf("NoResultException");

System.out.println(" ");

if(intIndex == - 1){

System.out.println("ERROR: Call = addCSRAttachments. Error = " + ue.getMessage());




System.out.println("Add Attachments to CSR ID: NO Rows Returned.");



System.out.println(" ");



// … Next set of calls and end of app.

} // end switch (calltorun)

System.out.println(" ");

System.out.println("Java Web Service Call Completed Successfully.");

System.out.println(" ");



And that’s it!

With less than 100 lines of code we set and established a connection to Stat. We then added an attachment to our CSR taking into consideration error control and user feedback/output.

The above code set is not a complete standalone app. With the proper imports, variables, and a few other wrappers, it would still not be more than 100 lines or so.

*1 If you would like a copy of the sample code used here, please see the Documents section of the Stat User Community.

*2 I do not profess to be a pro at Java, but more of a Java noob. Please excuse any blatant coding mishaps.


In Closing…

In the next post we will continue with the Attachment theme and look at retrieving and deleting attachments. Stay Tuned!

Thank you for taking the time to read this post. I hope you found it informative.



William R. Hart

Solutions Architect


Dell | Quest Group, Pre-Sales