<%@ page language="java" import="com.cisco.ipphone.sdk.*, org.w3c.dom.*, javax.xml.parsers.*, org.xml.sax.*, java.io.*, java.util.*" %>

<%
///////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Title:  		GroupMWI
// Author: 		kstearns
// Source File(s):  	groupmwi.jsp
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
// Description:
//
// GroupMWI accepts a list of vmail extensions and their partition and returns a list
// extensions/user which have voicemails waiting (MWI status is On). GroupMWI works by
// querying the CallManager AXL interface using SOAP/XML to retrieve the MWI status from
// the CallManager database.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////
//  Requirements and Caveats:
//
// -  Client: Cisco IP Phone XSI Browser
//
//  - Voicemail must be configred for the specified extensions in order for this application to
//    function properly.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

//
//  Edit the following Strings for your environment.
//  This is for sample purposes only - placing passwords and other
//  sensitive information in an open text file is obviously NOT recommended !!!
//

    String callManager = "10.0.0.1";
    String callManagerUserId = "administrator";
    String callManagerPassword = "cisco";

    String partitionName = "Standard";
    String[] vmailboxes = { "6593536", "6593535" };
    String[] vmailboxNames = { "Kelly", "Sam" };

    response.setContentType("text/xml");

    // Create new Provider for accessing AXL

    AXLProvider ap = new AXLProvider(callManager, callManagerUserId, callManagerPassword);


    String messages = "";

    // Create a new XML document builder

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    // Query AXL to get the specified Partition's information

    String axlResult = ap.sendRequest("getRoutePartition", "<partitionName>" + partitionName + "</partitionName>");

    // Create a new XML document using the SOAP/XML response from AXL

    StringBufferInputStream axlResultStream = new StringBufferInputStream(axlResult);
    Document axlResultDoc = builder.parse(axlResultStream);

    // Parse the XML response and find the UUID (unique ID) of the Partition

    NodeList partitionList = axlResultDoc.getElementsByTagName("routePartition");
    String partitionId = "";
    if (partitionList.getLength() != 0) {
      Element partition = (Element)partitionList.item(0);
      partitionId = partition.getAttribute("uuid");
    }

    NodeList lines;
    Element line;

    %>
    <CiscoIPPhoneText>
    <Title>Messages Waiting for ...</Title>
    <Text><%

    //  Step thru each vmail extension (in the given Partition) and query AXL for MWI status

    for (int index = 0; index < vmailboxes.length; index++)
    {
      axlResult = ap.sendRequest("getLine", "<pattern>" + vmailboxes[index] + "</pattern><routePartitionId>" + partitionId + "</routePartitionId><routeFilterId></routeFilterId>");

      axlResultStream = new StringBufferInputStream(axlResult);
      axlResultDoc = builder.parse(axlResultStream);

      lines = axlResultDoc.getElementsByTagName("mwiStatus");
      if (lines.getLength() > 0)
      {
        line = (Element)lines.item(0);

        Node status = line.getFirstChild();

        if (status != null) {
          //  If the MWI status is On, then add this extension/user to the list
          if (status.getNodeValue().equals("Lamp On"))
          {
            messages += vmailboxNames[index] + " (" + vmailboxes[index] + ")\n";
          }
        }
      }
      else
      {
        out.print("ERROR: Extension '" + vmailboxes[index] + "' not found in partition '" + partitionName + "'.\n");
      }
    }

%>
<%=messages%></Text>
</CiscoIPPhoneText>


<%
//  THIS SAMPLE APPLICATION AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND BY CISCO, 
//  EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY
//  FITNESS FOR A PARTICULAR PURPOSE, NONINFRINGEMENT, SATISFACTORY QUALITY OR ARISING FROM A COURSE
//  OF DEALING, LAW, USAGE, OR TRADE PRACTICE.  CISCO TAKES NO RESPONSIBILITY REGARDING ITS USAGE IN AN
//  APPLICATION., THE APPLICATION IS PROVIDED AS AN EXAMPLE ONLY, THEREFORE CISCO DOES NOT MAKE ANY
//  REPRESENTATIONS REGARDING ITS RELIABILITY, SERVICEABILITY, OR FUNCTION.  IN NO EVENT DOES CISCO
//  WARRANT THAT THE SOFTWARE IS ERROR FREE OR THAT CUSTOMER WILL BE ABLE TO OPERATE THE SOFTWARE WITHOUT
//  PROBLEMS OR INTERRUPTIONS.  NOR DOES CISCO WARRANT THAT THE SOFTWARE OR ANY EQUIPMENT ON WHICH THE
//  SOFTWARE IS USED WILL BE FREE OF VULNERABILITY TO INTRUSION OR ATTACK.  THIS SAMPLE APPLICATION IS
//  NOT SUPPORTED BY CISCO IN ANY MANNER. CISCO DOES NOT ASSUME ANY LIABILITY ARISING FROM THE USE OF THE
//  APPLICATION. FURTHERMORE, IN NO EVENT SHALL CISCO OR ITS SUPPLIERS BE LIABLE FOR ANY INCIDENTAL OR
//  CONSEQUENTIAL DAMAGES, LOST PROFITS, OR LOST DATA, OR ANY OTHER INDIRECT DAMAGES EVEN IF CISCO OR ITS
//  SUPPLIERS HAVE BEEN INFORMED OF THE POSSIBILITY THEREOF.
%>


