docs.intersystems.com
Home  /  Application Development: Additional Options  /  Web Services Quick Start Tutorial  /  The Sample Web Service


Web Services Quick Start Tutorial
The Sample Web Service
[Back]
InterSystems: The power behind what matters   
Search:  


The sample web service provides SOAP-based web service access to the contact application data. You need to have the contact application data model created and populated in order to use these web services. See Creating and Populating the Application Object Model for instructions. Note that part I of the tutorial guides you step-by-step through creating these classes.
Use Atelier to create the following classes for the SOAP-based web service:
SOAPTutorial.SOAPService This is the web service class.
/// SOAPTutorial.SOAPService
Class SOAPTutorial.SOAPService Extends %SOAP.WebService [ ClassType = "", ProcedureBlock ]
{

/// Name of the WebService.
Parameter SERVICENAME = "SOAPService";

/// TODO: change this to actual SOAP namespace.
/// SOAP Namespace for the WebService
Parameter NAMESPACE = "http://tempuri.org";

Method GetContact(id As %String) As SOAPTutorial.Contact [ WebMethod ]
{
    if ##class(SOAPTutorial.Contact).%ExistsId(id) {
        return ##class(SOAPTutorial.Contact).%OpenId(id)
    }
    else {
        return ""
    }
}

Method SaveContact(contact As SOAPTutorial.Contact) As %Integer [ WebMethod ]
{
    set sc = contact.%Save()
    if sc {
        return contact.%Id()
    }
    else {
        return 0
    }
}

Method RunPhoneNumberQuery(contactId As %String, phoneNumberType As %String) As %XML.DataSet [ WebMethod ]
{
    set query = ##class(%XML.DataSet).%New("%DynamicQuery:SQL")
    set sql = "SELECT p.PhoneNumberType, p.Number FROM SOAPTutorial.Contact" 
    set sql = sql_" as c inner join SOAPTutorial.PhoneNumber as p"
    set sql = sql_" on p.Contact=c.ID WHERE c.ID=? AND p.PhoneNumberType=?"
    do query.Prepare(sql)
    do query.SetArgs(contactId, phoneNumberType)
    return query
}

Method GetContactByName(Name As %String) As SOAPTutorial.ContactList [ WebMethod ]
{
    set stmt=##class(%SQL.Statement).%New()
    set query="SELECT %Id FROM SOAPTutorial.Contact WHERE Name %STARTSWITH ?"
    set list=##class(SOAPTutorial.ContactList).%New()
    do stmt.%Prepare(query)
    set rs = stmt.%Execute(Name)
    while rs.%Next()
    {
        set ref=##class(SOAPTutorial.Contact).%OpenId(rs.Id)
        do list.Insert(ref)	
    }
    return list
}

Method AddPhoneNumber(phone As SOAPTutorial.PhoneNumber, id As %String) As %Integer [ WebMethod ]
{
    set contact = ##class(SOAPTutorial.Contact).%OpenId(id)
    set phone.Contact = contact
    if contact.%Save() {
        return contact.%Id()
    }
    else {
        return 0
    }
}

Method GetContactByType(type As %String) As SOAPTutorial.ContactList [ WebMethod ]
{
    set stmt=##class(%SQL.Statement).%New()
    set query="SELECT %Id FROM SOAPTutorial.Contact WHERE ContactType = ?"
    set list=##class(SOAPTutorial.ContactList).%New()
    do stmt.%Prepare(query)
    set rs = stmt.%Execute(type)
    while rs.%Next()
    {
        set ref=##class(SOAPTutorial.Contact).%OpenId(rs.Id)
        do list.Insert(ref)	
    }
    return list
}

}
SOAPTutorial.ContactList This class defines the collection returned by the GetContactByType method of the above web service class.
Class SOAPTutorial.ContactList Extends %Library.ListOfObjects [ ClassType = serial, ProcedureBlock ]
{

Parameter ELEMENTTYPE = "SOAPTutorial.Contact";

}