jojacafe
1/12/2016 - 12:53 PM

Template handler dao jqGrid

Template handler dao jqGrid

Imports System.Web
Imports System.Web.Services
Imports Toolfactory.Core.BaseTypes
Imports Toolfactory.Database
Imports Toolfactory.Database.Dao

Public Class handler
    Implements System.Web.IHttpHandler

    Private respuesta As String
    Private Shared permiso As Boolean
    Private Shared conectionAC As String

    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dao.ConectionSet()

        context.Response.ContentType = "xml"

        Dim action As String = context.Request("oper")
        Dim page As Integer = context.Request("page")
        Dim rows As Integer = context.Request("rows")
        Dim sord As String = context.Request("sord")

        If String.IsNullOrEmpty(context.Request("permiso")) Then
            permiso = False
        Else
            permiso = context.Request("permiso")
        End If

        Select Case action
            Case "add"
                respuesta = Dao.Insert(context.Request("nombre"))
            Case "upd"
                respuesta = Dao.Update(context.Request("id"), context.Request("nombre"))
            Case "del"
                respuesta = Dao.Delete(context.Request("id"))
            Case "get"
                respuesta = Dao.SelectDT(rows, sord, page)
        End Select

        context.Response.Write(respuesta)
    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

    Private Class Dao
        Public Shared Sub ConectionSet()
            Dim conections As ConnectionList = DaoConnections.SingleInstance(LineOfBusiness.Cruises, ApplicationType.Intranet)
            conectionAC = conections("AGREGADORCRUCEROS", True)
        End Sub

        Public Shared Function Insert(name As String)
            If permiso Then
                Dim query As StringBuilder = New StringBuilder()
                With query
                    .Append("INSERT INTO _ (_) values(_)")
                End With

                DB.SingleInstance.ExecuteNonQuery(conectionAC, query.ToString(), Nothing, CommandType.Text)
                Return True
            Else
                Return "No tienes permisos para añadir"
            End If
        End Function

        Public Shared Function Update(id As String, name As String)
            If permiso Then
                Dim query As StringBuilder = New StringBuilder()
                With query
                    .Append(String.Format("UPDATE _ SET _ = '{0}' WHERE _ = {1}", "", ""))
                End With

                DB.SingleInstance.ExecuteDataSet(conectionAC, query.ToString(), Nothing, CommandType.Text)
                Return True
            Else
                Return "No tienes permisos para modificar"
            End If
        End Function

        Public Shared Function Delete(id As String)
            If permiso Then
                Dim query As StringBuilder = New StringBuilder()
                With query
                    .Append("DELETE FROM _ WHERE _")
                End With

                DB.SingleInstance.ExecuteDataSet(conectionAC, query.ToString(), Nothing, CommandType.Text)
                Return True
            Else
                Return "No tienes permisos para borrar"
            End If
        End Function

        Public Shared Function SelectDT(rows As Integer, sord As String, page As Integer)
            Dim query As StringBuilder = New StringBuilder()
            With query
                .Append("SELECT * FROM _ with(nolock)")
            End With

            Dim dt As DataTable = DB.SingleInstance.ExecuteDataTable(conectionAC, query.ToString(), Nothing, CommandType.Text)

            If Not IsNothing(dt) AndAlso dt.Rows.Count > 0 Then
                Dim oJQGrid As New cJQGrid(dt, rows, sord, page)

                Return oJQGrid.toXML({"id"})
            Else
                Return "No hay información para mostrar"
            End If
        End Function
    End Class
End Class