
Imports System.Web.Services
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Odbc
Imports System.IO
Imports System

'Changed the Namespace below to hogwarts

<System.Web.Services.WebService(Namespace:="http://hogwarts.cs.clemson.edu/mcoull/Listener")> _
Public Class Service1
    Inherits System.Web.Services.WebService

#Region " Web Services Designer Generated Code "

    Public Sub New()
    MyBase.New()

    'This call is required by the Web Services Designer.
    InitializeComponent()

    'Add your own initialization code after the InitializeComponent() call

End Sub

'Required by the Web Services Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Web Services Designer
'It can be modified using the Web Services Designer.  
'Do not modify it using the code editor.
    Friend WithEvents OracleConnection1 As System.Data.OracleClient.OracleConnection
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.OracleConnection1 = New System.Data.OracleClient.OracleConnection

    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        'CODEGEN: This procedure is required by the Web Services Designer
        'Do not modify it using the code editor.
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

#End Region


    '
    'Use this function to update the database with the newest contact information from
    'the windows CE application
    '
    'This is what the incoming XML string should look like
    '
    '<contactlist> 
    '<contact> 
    '  <username>Cameron</username> 
    '  <status>broadcast</status> 
    '  <latitude>14.3456345</latitude> 
    '  <longitude>-34.33456</longitude> 
    '  <time>9:15</time> 
    '</contact> 
    '</contactlist> 

    <WebMethod(Description:="Updates the current Contact's information")> _
    Public Function UpdateXML(ByVal XMLData As String) As String

        Dim xmlText As String = XMLData
        Dim strReader As StringReader = New StringReader(xmlText)
        Dim reader As XmlTextReader = New XmlTextReader(strReader)

        Dim username As String
        Dim status As String
        Dim latitude As String
        Dim longitude As String
        Dim time As String

        '''''''''''''''''''''''''' Parse the XML and put the values into variables ''''''''''''''

        ' Read the Xml.
        While reader.Read()
            ' Got an element?
            If reader.NodeType = XmlNodeType.Element Then
                ' Get this node.
                Dim name As String = reader.Name

                ' Get the username
                If name = "username" Then
                    ' Set it.
                    reader.Read()
                    username = reader.Value
                End If

                ' Get the status
                If name = "status" Then
                    ' Set it.
                    reader.Read()
                    status = reader.Value
                End If
                ' Get the latitude
                If name = "latitude" Then
                    ' Set it.
                    reader.Read()
                    latitude = reader.Value
                End If
                ' Get the longitude
                If name = "longitude" Then
                    ' Set it.
                    reader.Read()
                    longitude = reader.Value
                End If
                ' Get the time
                If name = "time" Then
                    ' Set it.
                    reader.Read()
                    time = reader.Value
                End If
            End If
        End While

        'The line below is to test the the XML is being parsed correctly
        'Return username & " " & status & " " & latitude & " " & longitude & " " & time



        '''''''''''''''''''''''''''''  Update Records in the Database ''''''''''''''''''''''''

        Dim ConnectString, UpdateStatement As String
        ConnectString = "server =hogwarts.cs.clemson.edu; uid=boylec; pwd=vivalaca; database=deptdb"
        UpdateStatement = "UPDATE BOYLEC.CONTACTS SET STATUS = '" & status & "', LATTITUDE = '" & latitude & "', LONGITUDE = '" & longitude & "', ACCESS_TIME = '" & time & "' WHERE USERNAME = '" & username & "'"

        Dim Connect As New SqlConnection(ConnectString)
        Dim objCmd As New SqlCommand(UpdateStatement, Connect)
        Connect.Open()

        Try
            objCmd.ExecuteNonQuery() 'Execute the SQL query
        Catch e As Exception
            Return ("Error")
        End Try

        Return ("Record Updated") 'When testing from localhost this message will be returned

    End Function

    'This is what the generated XML From GetXML() will look like
    '
    '<contactlist> 
    '<contact> 
    '  <username>cameron</username> 
    '  <status>broadcast</status> 
    '  <latitude>14.3456345</latitude> 
    '  <longitude>-34.33456</longitude> 
    '  <time>9:15</time> 
    '</contact> 
    '</contactlist> 

    <WebMethod(Description:="Returns up to date XML ContactList")> _
    Public Function GetXML(ByVal UserName As String) As String 'As contactlist As String


        Dim ConnectionString, SelectStatement As String
        Dim Connection As New SqlConnection
        Dim Adapter As New SqlDataAdapter
        Dim objDS As New DataSet
        Dim objCB As SqlCommandBuilder

        SelectStatement = "SELECT * FROM BOYLEC.CONTACTS"
        ConnectionString = "server =hogwarts.cs.clemson.edu; uid=boylec; pwd=vivalaca; database=deptdb"
        Connection.ConnectionString = ConnectionString
        Adapter.SelectCommand = New SqlCommand(SelectStatement, Connection)
        objCB = New SqlCommandBuilder(Adapter)
        Adapter.SelectCommand.Connection.Open()
        Adapter.Fill(objDS)


        'Dim user As String
        'Dim stat As String
        'Dim latt As String
        'Dim lon As String
        'Dim time As String
        'Dim Con As New contactlist
        Dim I As Integer
        Dim xml As String

        '******************  Build the XML  ****************'

        xml = xml + "<contactlist>"

        '******************  Get the data out of the dataset and into Strings  ****************'

        For I = 0 To objDS.Tables(0).Rows.Count - 1

            xml = xml + "<contact>"

            xml = xml + "<username>" + objDS.Tables(0).Rows(I).Item("USERNAME").ToString + "</username>"
            xml = xml + "<status>" + objDS.Tables(0).Rows(I).Item("STATUS").ToString + "</status>"
            xml = xml + "<latitude>" + objDS.Tables(0).Rows(I).Item("LATTITUDE").ToString + "</latitude>"
            xml = xml + "<longitude>" + objDS.Tables(0).Rows(I).Item("LONGITUDE").ToString + "</longitude>"
            xml = xml + "<time>" + objDS.Tables(0).Rows(I).Item("ACCESS_TIME").ToString + "</time>"

            xml = xml + "</contact>"

            '***  The code below uses the classes contact and contactlist to create the xml ***"

            'user = objDS.Tables(0).Rows(I).Item("USERNAME").ToString
            'stat = objDS.Tables(0).Rows(I).Item("STATUS").ToString
            'latt = objDS.Tables(0).Rows(I).Item("LATTITUDE").ToString
            'lon = objDS.Tables(0).Rows(I).Item("LONGITUDE").ToString
            'time = objDS.Tables(0).Rows(I).Item("ACCESS_TIME").ToString

            'Con.contact.username = user
            'Con.contact.status = stat
            'Con.contact.lattitude = latt
            'Con.contact.longitude = lon
            'Con.contact.time = time

        Next
        xml = xml + "</contactlist>"

        'Return Con // if your are creating the xml using the classes
        Return xml

    End Function

End Class
'Public Class contactlist
'Public contact As Contact
'End Class
'
'Public Class Contact
'Public username As String
'Public status As String
'Public time As String
'Public lattitude As Decimal
'Public longitude As Decimal

'End Class

' Use these classes if you need to have your GPS coordinates in direction, degrees, minutes and seconds

'Public Class Lattitude

'   Public direction As String
'   Public degrees As String
'   Public seconds As String
'   Public minutes As String
'
'End Class

'Public Class Longitude

'   Public direction As String
'   Public degrees As String
'   Public seconds As String
'   Public minutes As String

'End Class