Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
vendors:cisco:uc:ucce:cvp:java [2019/02/20 12:01] – gerardorourkevendors:cisco:uc:ucce:cvp:java [2025/02/06 12:52] (current) – [Convert a Number to a String type with an intger value] gerardorourke
Line 1: Line 1:
 ====== Call Studio Java Examples ====== ====== Call Studio Java Examples ======
 +
 +Reference: https://www.cisco.com/c/en/us/td/docs/voice_ip_comm/cust_contact/contact_center/customer_voice_portal/cvp11_6/programming/guide/ccvp_b_programming-guide-for-cisco-unified-11-6/ccvp_b_programming-guide-for-cisco-unified_chapter_01.html#CCVP_TP_C5C1E0C4_00
 +
 +===== Create custom Java Code for CVP=====
 +Add the below two “external Jar” to the Library
 +  *C:\Cisco\CallStudio\eclipse\plugins\com.audiumcorp.studio.library.framework\lib\framework.jar
 +  *C:\Cisco\CallStudio\eclipse\plugins\com.audiumcorp.studio.library.framework\lib\servlet-api.jar
 +
 +
 +===== Convert a Number to a String type with an integer value =====
 +Note - you don't need to do this - you can use Set value as follows:
 +
 +=== JavaScript in Set Value node - can also do this ====
 +as you can see from below - do NOT use "eval"
 +<code>
 +var pollDurationInt; 
 +pollDurationInt = {Data.Session.pollDuration};
 +pollDurationInt = parseInt(pollDurationInt,10);
 +pollDurationInt = pollDurationInt .toString();
 +pollDurationInt;
 +</code>
 +
  
   * Set a session variable myInt to be your number, such as the string "12.0" which gets returned from Set value   * Set a session variable myInt to be your number, such as the string "12.0" which gets returned from Set value
   * The below Call Studio Action Java Class will retrieve this value and set the session variable "myString" to "12"   * The below Call Studio Action Java Class will retrieve this value and set the session variable "myString" to "12"
  
-==== Convert a Number to a String type with an intger value ==== 
 <code java> <code java>
 import com.audium.server.AudiumException; import com.audium.server.AudiumException;
Line 85: Line 106:
 </code> </code>
  
-==== Simulate a delay ====+===== Simulate a delay =====
 <code java> <code java>
 // Simple Action Element to Simulate a Delay // Simple Action Element to Simulate a Delay
Line 182: Line 203:
 } }
 </code> </code>
 +
 +
 +===== XML Element Extract Value =====
 +<code java>
 +/**
 + * Author: Gerry O'Rourke
 + * Date: 01 July 2021
 + * 
 + * This Java Class extract a value of a specific XML element.
 + * In my use case I extract from the below SOAP XML response from LCM the value of the <strContactDetail> Element. 
 + * 
 +<?xml version="1.0"?>
 +<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 +  <soap:Header/>
 +  <soap:Body>
 +    <GetContactDetailResponse xmlns="http://tempuri.org/">
 +      <GetContactDetailResult>1</GetContactDetailResult>
 +      <strContactDetail>&lt;DATA&gt;&lt;BussFlds&gt;&lt;FieldName&gt;FirstName&lt;/FieldName&gt;&lt;Value&gt;&lt;![CDATA[Bob]]&gt;&lt;/Value&gt;&lt;DataType&gt;String&lt;/DataType&gt;&lt;Format&gt;&lt;/Format&gt;&lt;/BussFlds&gt;&lt;BussFlds&gt;&lt;FieldName&gt;LastName&lt;/FieldName&gt;&lt;Value&gt;&lt;![CDATA[SCA]]&gt;&lt;/Value&gt;&lt;DataType&gt;String&lt;/DataType&gt;&lt;Format&gt;&lt;/Format&gt;&lt;/BussFlds&gt;&lt;BussFlds&gt;&lt;FieldName&gt;RegNum&lt;/FieldName&gt;&lt;Value&gt;&lt;![CDATA[]]&gt;&lt;/Value&gt;&lt;DataType&gt;String&lt;/DataType&gt;&lt;Format&gt;&lt;/Format&gt;&lt;/BussFlds&gt;&lt;BussFlds&gt;&lt;FieldName&gt;SpeakerID&lt;/FieldName&gt;&lt;Value&gt;&lt;![CDATA[1872730M5BS]]&gt;&lt;/Value&gt;&lt;DataType&gt;String&lt;/DataType&gt;&lt;Format&gt;&lt;/Format&gt;&lt;/BussFlds&gt;&lt;BussFlds&gt;&lt;FieldName&gt;Merchant&lt;/FieldName&gt;&lt;Value&gt;&lt;![CDATA[Amazon]]&gt;&lt;/Value&gt;&lt;DataType&gt;String&lt;/DataType&gt;&lt;Format&gt;&lt;/Format&gt;&lt;/BussFlds&gt;&lt;BussFlds&gt;&lt;FieldName&gt;Amt&lt;/FieldName&gt;&lt;Value&gt;&lt;![CDATA[150.00]]&gt;&lt;/Value&gt;&lt;DataType&gt;Number&lt;/DataType&gt;&lt;Format&gt;&lt;/Format&gt;&lt;/BussFlds&gt;&lt;BussFlds&gt;&lt;FieldName&gt;CardNum&lt;/FieldName&gt;&lt;Value&gt;&lt;![CDATA[1234]]&gt;&lt;/Value&gt;&lt;DataType&gt;Number&lt;/DataType&gt;&lt;Format&gt;&lt;/Format&gt;&lt;/BussFlds&gt;&lt;/DATA&gt;</strContactDetail>
 +    </GetContactDetailResponse>
 +  </soap:Body>
 +</soap:Envelope>
 +
 + * Required Session Data Variables:, response_xml, elementName_xml
 + * Set the Session Data Variable response_xml to the XML String you want to extract data from.
 + * Set the Session Data Variable elementName_xml to the element name you want to extract data from, e.g. "strContactDetail"
 + * 
 + * Session Data Variable Set by this Java Class: xmlElement
 + * {Data.Session.xmlElement} will be set to the value of the element
 + * 
 + * 
 + * References
 + * https://howtodoinjava.com/java/xml/parse-string-to-xml-dom/
 + * https://stackoverflow.com/questions/4076910/how-to-retrieve-element-value-of-xml-using-java/24497342
 + * 
 + */
 +
 +import com.audium.server.AudiumException;
 +import com.audium.server.voiceElement.ActionElementBase;
 +import com.audium.server.session.ActionElementData;
 +
 +import java.io.StringReader;
 +
 +import javax.xml.parsers.DocumentBuilder;
 +import javax.xml.parsers.DocumentBuilderFactory;
 +import org.w3c.dom.Document;
 +import org.w3c.dom.Element;
 +import org.w3c.dom.NodeList;
 +import org.xml.sax.InputSource;
 +
 +/**
 + * This class is called when a standard action has been configured to use a 
 + * Java class. Since this is a standard action element, it applies to a 
 + * specific application and does not have a configuration. As a result, the 
 + * only method needed in this class is the doAction method.
 + */
 +
 +
 +
 +public class GetXmlElement extends ActionElementBase
 +{
 +    /**
 +     * All action classes must implement this method. The data is retrieved 
 +     * through the ActionElementData class. Unlike decisions, there is no need 
 +     * to return anything as all actions have a single exit state.
 +     */
 +    public void doAction(String name, ActionElementData data) throws AudiumException
 +    {
 +    String xmlStr = data.getSessionData("response_xml").toString();
 + data.addToLog("xmlStr", xmlStr);
 +
 +    String xmlElementName = data.getSessionData("elementName_xml").toString();
 + data.addToLog("xmlElementName", xmlElementName);
 +
 +    Document doc = convertStringToXMLDocument(xmlStr);
 +    Element rootElement = doc.getDocumentElement();
 +   
 +    String xmlElement = getString(xmlElementName, rootElement);
 + data.setSessionData("xmlElement", xmlElement);
 +    data.addToLog("xmlElement", xmlElement);
 +    }
 +    
 +
 +    private static Document convertStringToXMLDocument(String xmlString) 
 +    {
 +        //Parser that produces DOM object trees from XML content
 +        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 +         
 +        //API to obtain DOM Document instance
 +        DocumentBuilder builder = null;
 +        try
 +        {
 +            //Create DocumentBuilder with default configuration
 +            builder = factory.newDocumentBuilder();
 +             
 +            //Parse the content to Document object
 +            Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
 +            return doc;
 +        } 
 +        catch (Exception e) 
 +        {
 +            e.printStackTrace();
 +        }
 +        return null;
 +    }
 +
 +    protected String getString(String tagName, Element element) {
 +        NodeList list = element.getElementsByTagName(tagName);
 +        if (list != null && list.getLength() > 0) {
 +            NodeList subList = list.item(0).getChildNodes();
 +
 +            if (subList != null && subList.getLength() > 0) {
 +                return subList.item(0).getNodeValue();
 +            }
 +        }
 +
 +        return null;
 +    }    
 +    
 +}
 +</code>
 +===== Parse / Loop the BussFld tags of an XML String (retrieved from LCM) =====
 +
 +<code java>
 +/**
 + * Author: Gerry O'Rourke
 + * Date: 01 July 2021
 + * 
 + * This Java Class parses an extract of XML returned from Acquoen's LCM GetContactDetail Web Service Call.
 + * Below is an example of what is expected to be set in the Session Variable 'strContactDetail'
 + * 
 + * This session variable is retrieved and the 'BussFlds' tags are parsed. The Value of Each FieldName is created a Session Variable, who value is set to the 'Value' tags value. 
 + * e.g. {Data.Session.FirstName} == "Bob" etc.
 +    <DATA>
 + <BussFlds><FieldName>FirstName</FieldName><Value><![CDATA[Bob]]></Value><DataType>String</DataType><Format></Format></BussFlds>
 + <BussFlds><FieldName>LastName</FieldName><Value><![CDATA[SCA]]></Value><DataType>String</DataType><Format></Format></BussFlds>
 + <BussFlds><FieldName>RegNum</FieldName><Value><![CDATA[]]></Value><DataType>String</DataType><Format></Format></BussFlds>
 + <BussFlds><FieldName>SpeakerID</FieldName><Value><![CDATA[1872730M5BS]]></Value><DataType>String</DataType><Format></Format></BussFlds>
 + <BussFlds><FieldName>Merchant</FieldName><Value><![CDATA[Amazon]]></Value><DataType>String</DataType><Format></Format></BussFlds>
 + <BussFlds><FieldName>Amt</FieldName><Value><![CDATA[150.00]]></Value><DataType>Number</DataType><Format></Format></BussFlds>
 + <BussFlds><FieldName>CardNum</FieldName><Value><![CDATA[1234]]></Value><DataType>Number</DataType><Format></Format></BussFlds>
 + </DATA>
 + *
 + * References:
 + * https://howtodoinjava.com/java/xml/read-xml-dom-parser-example/
 + * Reference: https://howtodoinjava.com/java/xml/parse-string-to-xml-dom/
 + */
 +
 +/**
 + * Required Session Variable: strContactDetail
 + * 
 + * Session Variables Set:
 + * Each FieldName will retrieved will have a Session Variable set to its FieldName and its value set to its Value.
 + */
 +
 +import com.audium.server.AudiumException;
 +import com.audium.server.voiceElement.ActionElementBase;
 +import com.audium.server.session.ActionElementData;
 +
 +import java.io.StringReader;
 +
 +import javax.xml.parsers.DocumentBuilder;
 +import javax.xml.parsers.DocumentBuilderFactory;
 +import org.w3c.dom.Document;
 +import org.w3c.dom.Element;
 +import org.w3c.dom.Node;
 +import org.w3c.dom.NodeList;
 +import org.xml.sax.InputSource;
 +
 +/**
 + * This class is called when a standard action has been configured to use a 
 + * Java class. Since this is a standard action element, it applies to a 
 + * specific application and does not have a configuration. As a result, the 
 + * only method needed in this class is the doAction method.
 + */
 +
 +
 +public class LCMSetBussFldsVariables extends ActionElementBase
 +{
 +    /**
 +     * All action classes must implement this method. The data is retrieved 
 +     * through the ActionElementData class. Unlike decisions, there is no need 
 +     * to return anything as all actions have a single exit state.
 +     */
 +    public void doAction(String name, ActionElementData data) throws AudiumException
 +    {
 + //Set the string variable 'strContactDetail ' to the CVP Session Variable 'strContactDetail'
 +    String strContactDetail = data.getSessionData("strContactDetail").toString();
 +
 +    //Convert XML string to Document
 +    Document document = convertStringToXMLDocument(strContactDetail);
 +     
 +    //Normalize the XML Structure; It's just too important !!
 +    document.getDocumentElement().normalize();
 +     
 +    //Here comes the root node
 +    Element root = document.getDocumentElement();
 +    data.addToLog("root.getNodeName()", root.getNodeName());
 +     
 +    //Get all BussFlds
 +    NodeList nList = document.getElementsByTagName("BussFlds");
 +         
 +    for (int temp = 0; temp < nList.getLength(); temp++)
 +    {
 +    Node node = nList.item(temp);
 +    if (node.getNodeType() == Node.ELEMENT_NODE)
 +    {
 +    Element eElement = (Element) node;
 +   
 +    //Retrieve and set the String variable 'FieldName' to the XML Tag 'FieldName' 
 +        String FieldName = eElement.getElementsByTagName("FieldName").item(0).getTextContent().toString();
 +        
 +    //Retrieve and set the String variable 'Value' to the XML Tag 'Value' 
 +        String Value = eElement.getElementsByTagName("Value").item(0).getTextContent().toString();
 +       
 +        // Set the Session Data Variable of the current 'FieldName' tag value to the current 'Value' tag value.
 +        // e.g. {Data.Session.FirstName} = "Bob"
 +    data.setSessionData(FieldName, Value);
 +
 +    // Log the Session Data Variable Name and value
 +    data.addToLog("Setting Session.Data."+ FieldName, Value);
 +       
 +    }
 +    }   
 + 
 +    }
 +
 +    private static Document convertStringToXMLDocument(String xmlString) 
 +    {
 +        //Parser that produces DOM object trees from XML content
 +        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
 +         
 +        //API to obtain DOM Document instance
 +        DocumentBuilder builder = null;
 +        try
 +        {
 +            //Create DocumentBuilder with default configuration
 +            builder = factory.newDocumentBuilder();
 +             
 +            //Parse the content to Document object
 +            Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
 +            return doc;
 +        } 
 +        catch (Exception e) 
 +        {
 +            e.printStackTrace();
 +        }
 +        return null;
 +    }
 +
 +}
 +
 +</code>
 +
 +
 +==== Cisco Stock Elements ====
 +
 +The out of the box elements for CVP in CVP Call Studio are located in the JAR file
 +  *C:\Cisco\CallStudio\eclipse\plugins\com.audiumcorp.studio.elements.core\lib\elements.jar
  • vendors/cisco/uc/ucce/cvp/java.1550664088.txt.gz
  • Last modified: 2019/02/20 12:01
  • by gerardorourke