⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datalayer.vb

📁 一个管理系统
💻 VB
字号:
Imports System.Net
Imports System.Threading
Imports System.Web.Services.Protocols
Imports TaskVision.DataWS
Imports TaskVision.AuthWS

Public Enum DataLayerResult
    None = 0
    Success = 1
    ServiceFailure = 2
    UnknownFailure = 3
    ConnectionFailure = 4
    AuthenticationFailure = 5
End Enum

Public Class DataLayer

    Public DsTasks As New DataSetTasks()
    Public DsProjects As New DataSetProjects()
    Public DsLookupTables As New DataSetLookupTables()
    Public DsProjectHistory As New DataSetProjectHistory()
    Public CurrentUserInformation As New UserInformation()

    Private m_Ticket As String = Nothing
    Private m_WsData As New DataService()
    Private m_WsAuth As New AuthService()
    Private Const c_wsTimeout As Integer = 30000 '30 seconds

    Public Sub New()

        'CurrentUserInformation is a type that exists on the web service
        'hence we lose default implementation
        CurrentUserInformation.UserID = -1
        CurrentUserInformation.UserEmail = String.Empty
        CurrentUserInformation.UserFullName = String.Empty
        CurrentUserInformation.UserName = String.Empty
        CurrentUserInformation.UserPassword = String.Empty

        m_WsData.Timeout = c_wsTimeout
        m_WsAuth.Timeout = c_wsTimeout
    End Sub

#Region " Login "
    Public Function Login(ByVal userName As String, ByVal userPassword As String) As DataLayerResult
        'save the user information
        CurrentUserInformation.UserName = userName
        CurrentUserInformation.UserPassword = userPassword

        'try to get a ticket
        Dim ticketResult As DataLayerResult = GetAuthorizationTicket()
        If ticketResult = DataLayerResult.Success Then
            Dim newUserInfo As UserInformation

            'try to get the current user information
            Try
                newUserInfo = m_WsAuth.GetUserInfo(m_Ticket)
            Catch ex As Exception
                Return HandleException(ex)
            End Try

            'this shouldn't happen unless the above
            If newUserInfo Is Nothing Then
                Return DataLayerResult.AuthenticationFailure
            End If

            'keep the returned information
            CurrentUserInformation.UserID = newUserInfo.UserID
            CurrentUserInformation.UserName = newUserInfo.UserName
            CurrentUserInformation.UserFullName = newUserInfo.UserFullName
            CurrentUserInformation.UserEmail = newUserInfo.UserEmail
            CurrentUserInformation.IsAdministrator = newUserInfo.IsAdministrator
            CurrentUserInformation.IsAccountLocked = newUserInfo.IsAccountLocked

            Return DataLayerResult.Success
        Else
            Return ticketResult
        End If
    End Function
#End Region

#Region " Auth Service "
    Private Function GetAuthorizationTicket() As DataLayerResult
        Try
            m_Ticket = m_WsAuth.GetAuthorizationTicket(CurrentUserInformation.UserName, CurrentUserInformation.UserPassword)
        Catch ex As Exception
            m_Ticket = Nothing
            Return HandleException(ex)
        End Try

        If m_Ticket Is Nothing Then
            'username/password failed
            Return DataLayerResult.AuthenticationFailure
        End If

        Return DataLayerResult.Success
    End Function

    'note: userID is ByRef
    Public Function InsertUser(ByRef userID As Integer, ByVal newUserInfo As UserInformation) As DataLayerResult
        Try
            userID = m_WsAuth.InsertUser(m_Ticket, newUserInfo)

            'all TaskVision web services return nothing to indicate an expired ticket
            If userID = -1 Then
                'get a new ticket and try the call again
                Dim ticketResult As DataLayerResult = GetAuthorizationTicket()
                If ticketResult <> DataLayerResult.Success Then
                    Return ticketResult
                End If

                userID = m_WsAuth.InsertUser(m_Ticket, newUserInfo)

                'this next block should never happen
                If userID = -1 Then
                    Return DataLayerResult.AuthenticationFailure
                End If
            End If
        Catch ex As Exception
            Return HandleException(ex)
        End Try

        Return DataLayerResult.Success
    End Function

    Public Function UpdateUser(ByVal updatedUserInfo As UserInformation) As DataLayerResult
        Dim uInfo As UserInformation
        Try
            uInfo = m_WsAuth.UpdateUser(m_Ticket, updatedUserInfo)

            'all TaskVision web services return nothing to indicate an expired ticket
            If uInfo Is Nothing Then
                'get a new ticket and try the call again
                Dim ticketResult As DataLayerResult = GetAuthorizationTicket()
                If ticketResult <> DataLayerResult.Success Then
                    Return ticketResult
                End If

                uInfo = m_WsAuth.UpdateUser(m_Ticket, updatedUserInfo)

                'this next block should never happen. It means it took more than TIMEOUT 
                '(default is 2 min) between GetAuthTicket and GetProjects
                If uInfo Is Nothing Then
                    Return DataLayerResult.AuthenticationFailure
                End If
            End If
        Catch ex As Exception
            Return HandleException(ex)
        End Try

        Return DataLayerResult.Success
    End Function

    Public Function ChangePassword(ByVal updatedUserInfo As UserInformation) As DataLayerResult
        Dim uInfo As UserInformation
        Try
            uInfo = m_WsAuth.ChangePassword(m_Ticket, updatedUserInfo)

            'all TaskVision web services return nothing to indicate an expired ticket
            If uInfo Is Nothing Then
                'get a new ticket and try the call again
                Dim ticketResult As DataLayerResult = GetAuthorizationTicket()
                If ticketResult <> DataLayerResult.Success Then
                    Return ticketResult
                End If

                uInfo = m_WsAuth.ChangePassword(m_Ticket, updatedUserInfo)

                'this next block should never happen. It means it took more than TIMEOUT 
                '(default is 2 min) between GetAuthTicket and GetProjects
                If uInfo Is Nothing Then
                    Return DataLayerResult.AuthenticationFailure
                End If
            End If
        Catch ex As Exception
            Return HandleException(ex)
        End Try

        Return DataLayerResult.Success
    End Function
#End Region

#Region " Data Service "
    Public Function GetProjects() As DataLayerResult
        Dim ds As DataSetProjects
        Try
            ds = m_WsData.GetProjects(m_Ticket)

            ' All TaskVision web services return nothing to indicate an expired ticket
            If ds Is Nothing Then
                ' Get a new ticket and try the call again
                Dim ticketResult As DataLayerResult = GetAuthorizationTicket()
                If ticketResult <> DataLayerResult.Success Then
                    Return ticketResult
                End If

                ds = m_WsData.GetProjects(m_Ticket)

                ' This next block should never happen. It means it took more than TIMEOUT 
                ' (default is 2 min) between GetAuthTicket and GetProjects
                If ds Is Nothing Then
                    Return DataLayerResult.AuthenticationFailure
                End If
            End If
        Catch ex As Exception
            Return HandleException(ex)
        End Try

        DsProjects.Clear()
        DsProjects.Merge(ds)
        Return DataLayerResult.Success
    End Function

    Public Function GetTasks(ByVal projectID As Integer, ByVal clearData As Boolean) As DataLayerResult
        Dim ds As DataSetTasks
        Try
            ds = m_WsData.GetTasks(m_Ticket, projectID)

            If ds Is Nothing Then
                Dim ticketResult As DataLayerResult = GetAuthorizationTicket()
                If ticketResult <> DataLayerResult.Success Then
                    Return ticketResult
                End If

                ds = m_WsData.GetTasks(m_Ticket, projectID)

                If ds Is Nothing Then
                    Return DataLayerResult.AuthenticationFailure
                End If
            End If
        Catch ex As Exception
            Return HandleException(ex)
        End Try

        If clearData Then DsTasks.Tasks.Clear()

        DsTasks.Merge(ds)
        Return DataLayerResult.Success
    End Function

    Public Function GetLookUpTables() As DataLayerResult
        Dim ds As DataSetLookupTables
        Try
            ds = m_WsData.GetLookupTables(m_Ticket)

            If ds Is Nothing Then
                Dim ticketResult As DataLayerResult = GetAuthorizationTicket()
                If ticketResult <> DataLayerResult.Success Then
                    Return ticketResult
                End If

                ds = m_WsData.GetLookupTables(m_Ticket)

                If ds Is Nothing Then
                    Return DataLayerResult.AuthenticationFailure
                End If
            End If
        Catch ex As Exception
            Return HandleException(ex)
        End Try

        DsLookupTables.Priorities.Clear()
        DsLookupTables.Users.Clear()
        DsLookupTables.Statuses.Clear()
        DsLookupTables.Merge(ds)
        Return DataLayerResult.Success
    End Function

    Public Function InsertProject(ByRef projectID As Integer, ByVal projectName As String, ByVal projectDescription As String) As DataLayerResult
        Try
            'note: projectID is ByRef
            projectID = m_WsData.InsertProject(m_Ticket, projectName, projectDescription)

            '-1 is our magic number for nothing, like above the ws returns nothing if the ticket is expired
            If projectID = -1 Then
                Dim ticketResult As DataLayerResult = GetAuthorizationTicket()
                If ticketResult <> DataLayerResult.Success Then
                    Return ticketResult
                End If

                projectID = m_WsData.InsertProject(m_Ticket, projectName, projectDescription)

                If projectID = -1 Then
                    Return DataLayerResult.AuthenticationFailure
                End If
            End If
        Catch ex As Exception
            Return HandleException(ex)
        End Try

        Return DataLayerResult.Success
    End Function

    Public Function UpdateTasks(ByVal projectID As Integer) As DataLayerResult
        Dim ds As DataSetTasks
        Try
            'note that we are sending the whole dataset
            'if during data collision handling, the web service breaks
            'we'll be stuck with a short dataset
            ds = m_WsData.UpdateTasks(m_Ticket, projectID, DsTasks)

            If ds Is Nothing Then
                Dim ticketResult As DataLayerResult = GetAuthorizationTicket()
                If ticketResult <> DataLayerResult.Success Then
                    Return ticketResult
                End If

                ds = m_WsData.UpdateTasks(m_Ticket, projectID, DsTasks)

                If ds Is Nothing Then
                    Return DataLayerResult.AuthenticationFailure
                End If
            End If
        Catch ex As Exception
            Return HandleException(ex)
        End Try

        DsTasks.Tasks.Clear()
        DsTasks.Merge(ds)
        Return DataLayerResult.Success
    End Function

    Public Function BeginGetTasks(ByVal projectID As Integer) As IAsyncResult
        Try
            'workaround for the expiring ticket during idle time
            'start an async call for ticket
            Dim authAr As IAsyncResult = m_WsAuth.BeginGetAuthorizationTicket(CurrentUserInformation.UserName, CurrentUserInformation.UserPassword, Nothing, Nothing)

            'start an async call for the dataset and send the ticket async result along for the ride
            Return m_WsData.BeginGetTasks(m_Ticket, projectID, Nothing, authAr)
        Catch ex As Exception
            LogError.Write(ex.Message & vbNewLine & ex.StackTrace)
            Return Nothing
        End Try
    End Function

    Public Function EndGetTasks(ByVal ar As IAsyncResult) As DataLayerResult
        Dim ds As DataSetTasks

        Try
            'this method was called after checking that both results are completed
            'grab the new ticket
            m_Ticket = m_WsAuth.EndGetAuthorizationTicket(CType(ar.AsyncState, IAsyncResult))

            'grab the new dataset
            ds = m_WsData.EndGetTasks(ar)

            If ds Is Nothing Then
                Return DataLayerResult.AuthenticationFailure
            End If
        Catch ex As Exception
            Return HandleException(ex)
        End Try

        DsTasks.Tasks.Clear()
        DsTasks.Merge(ds)
        Return DataLayerResult.Success
    End Function

    Public Function BeginGetProjectHistory(ByVal projectID As Integer) As IAsyncResult
        Try
            'note: there is an assumption here that our ticket is always valid
            'because this method called immediately after a project or task request.

            'start an async call for the dataset
            Return m_WsData.BeginGetProjectHistory(m_Ticket, projectID, Nothing, New Object() {projectID})
        Catch ex As Exception
            LogError.Write(ex.Message & vbNewLine & ex.StackTrace)
            Return Nothing
        End Try
    End Function

    Public Function EndGetProjectHistory(ByVal ar As IAsyncResult) As DataLayerResult
        Dim ds As DataSetProjectHistory

        Try
            'grab the new dataset
            ds = m_WsData.EndGetProjectHistory(ar)

            If ds Is Nothing Then
                Return DataLayerResult.AuthenticationFailure
            End If
        Catch ex As Exception
            Return HandleException(ex)
        End Try

        DsProjectHistory.ProjectHistory.Clear()
        DsProjectHistory.Merge(ds)
        Return DataLayerResult.Success
    End Function
#End Region

#Region " Helper Functions "
    Private Function HandleException(ByVal ex As Exception) As DataLayerResult
        LogError.Write(ex.Message & vbNewLine & ex.StackTrace)

        If ex.GetType() Is GetType(WebException) Then
            Return DataLayerResult.ConnectionFailure
        ElseIf ex.GetType() Is GetType(SoapException) Then
            Return DataLayerResult.ServiceFailure
        Else
            Return DataLayerResult.UnknownFailure
        End If
    End Function
#End Region

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -