Imports fatd_faturamento.BLL.Repositorio
Imports fatd_faturamento.ENTIDADE

Public Class frmCadastroPermissaoMenu

    Dim vResp As Short
    Dim vNew As Boolean
    Dim contTag As Integer = 1
    Dim mMenuStrip As New MenuStrip
    Dim bChildTrigger As Boolean = True
    Dim bParentTrigger As Boolean = True

    Sub New(pMenuPrincipal As MenuStrip)
        InitializeComponent()
        mMenuStrip = pMenuPrincipal
    End Sub

#Region "Propriedades"

    Private Sub frmCadastroPermissaoMenu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Try

            SetWindowTheme(Me.tvPermissoes.Handle, "explorer", Nothing)

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtCodigo_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCodigo.LostFocus

        Try

            If Me.txtCodigo.Text <> "" Then
                SetaCodigo("permissao_menu", Me.txtCodigo.Text)
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtCodigo_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCodigo.GotFocus

        Try

            Me.txtCodigo.SelectAll()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtCodigo_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCodigo.KeyDown

        Try

            If e.KeyCode = Keys.Enter Then
                If Me.txtCodigo.Text = "" Then
                    Me.btNovo.PerformClick()
                Else
                    e.SuppressKeyPress = True
                    SetaCodigo("permissao_menu", Me.txtCodigo.Text)
                End If
            ElseIf e.KeyCode = Keys.F3 Then

                frmLocalizadorGeral.SetParametros("permissao_menu", "Pesquisar Perfil", Me)
                frmLocalizadorGeral.Show()

            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub txtCodigo_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCodigo.KeyPress

        Try

            SomenteInteger(e.KeyChar, Me.txtCodigo, False)

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub chkSelecionarTodos_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSelecionarTodos.CheckedChanged

        Try

            If Me.chkSelecionarTodos.Checked Then
                Me.SelectAllTreeViewNodes(Me.tvPermissoes.Nodes, True)
            Else
                Me.SelectAllTreeViewNodes(Me.tvPermissoes.Nodes, False)
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub tv_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvPermissoes.AfterCheck

        Try

            If bChildTrigger Then
                CheckAllChildren(e.Node, e.Node.Checked)
            End If

            If bParentTrigger Then
                CheckMyParent(e.Node, e.Node.Checked)
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub chkExpandirTudo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkExpandirTudo.CheckedChanged

        Try

            If Me.chkExpandirTudo.Checked Then
                Me.tvPermissoes.ExpandAll()
            Else
                Me.tvPermissoes.CollapseAll()
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try
    End Sub

    Private Sub btPesquisar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btPesquisar.Click

        Try

            frmLocalizadorGeral.SetParametros("permissao_menu", "Pesquisar Perfil", Me)
            frmLocalizadorGeral.Show()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub btNovo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btNovo.Click

        Try

            lNew()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub btFechar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btFechar.Click

        Try

            lClose()

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub btSalvar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btSalvar.Click

        Dim vlist

        Dim objPermissaoMenu As New entidadePermissaoMenu
        Dim repositorioPermissaoMenu As New repositorioPermissaoMenu

        Dim objPermissaoMenuDet As New entidadePermissaoMenuDet
        Dim repositorioPermissaoMenuDet As New repositorioPermissaoMenuDet

        Try

            If Me.txtDescricao.Text.Trim = "" Then
                MessageBox.Show("Por favor, digite a descrição do perfil.", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.txtDescricao.Select()
                Exit Sub
            End If

            If vNew = True Then

                If Me.txtCodigo.Text = String.Empty Then

                    If lInsert() Then

                        vlist = repositorioPermissaoMenu.SelectMax(objPermissaoMenu)

                        If vlist.Count > 0 Then

                            For Each objPermissaoMenuFor As entidadePermissaoMenu In vlist
                                Me.txtCodigo.Text = CStr(CInt(IIf(IsDBNull(objPermissaoMenuFor.ID_PERMISSAO), 0, objPermissaoMenuFor.ID_PERMISSAO)))
                            Next

                            '[Excluir possíveis detalhes do código selecionado]
                            objPermissaoMenuDet.ID_SISTEMA = vintIdSistema
                            objPermissaoMenuDet.ID_PERMISSAO = CInt(Me.txtCodigo.Text)

                            repositorioPermissaoMenuDet.Delete(objPermissaoMenuDet)

                            '[Insere os detalhes da permissão]
                            Me.SaveTree(Me.tvPermissoes.Nodes)

                            SetaCodigo("permissao_menu", CInt(Me.txtCodigo.Text))

                        End If

                        MessageBox.Show("Perfil criado com sucesso!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)


                    Else
                        MessageBox.Show("Perfil não pode ser criado!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Exit Sub
                    End If

                End If

            Else

                If Me.txtCodigo.Text <> String.Empty Then

                    If lUpdate() Then

                        objPermissaoMenu.ID_PERMISSAO = CInt(Me.txtCodigo.Text)
                        vlist = repositorioPermissaoMenu.Select_1(objPermissaoMenu)

                        If vlist.Count > 0 Then

                            For Each objPermissaoMenuFor As entidadePermissaoMenu In vlist
                                Me.txtCodigo.Text = CStr(CInt(IIf(IsDBNull(objPermissaoMenuFor.ID_PERMISSAO), 0, objPermissaoMenuFor.ID_PERMISSAO)))
                            Next

                            '[Excluir possíveis detalhes do código selecionado]
                            objPermissaoMenuDet.ID_SISTEMA = vintIdSistema
                            objPermissaoMenuDet.ID_PERMISSAO = CInt(Me.txtCodigo.Text)

                            repositorioPermissaoMenuDet.Delete(objPermissaoMenuDet)

                            '[Insere os detalhes da permissão]
                            Me.SaveTree(Me.tvPermissoes.Nodes)

                            SetaCodigo("permissao_menu", CInt(Me.txtCodigo.Text))

                        End If

                        MessageBox.Show("Perfil atualizado com sucesso!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)


                    Else
                        MessageBox.Show("Perfil não pode ser atualizado!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)
                        Exit Sub
                    End If

                End If

            End If


        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try


    End Sub

    Private Sub btExcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btExcluir.Click

        Dim vlist

        Dim vCountPerfil As Integer

        Dim objUsuario As New entidadeUsuario
        Dim repositorioUsuario As New repositorioUsuario

        Dim objPermissaoMenu As New entidadePermissaoMenu
        Dim repositorioPermissaoMenu As New repositorioPermissaoMenu

        Dim objPermissaoMenuDet As New entidadePermissaoMenuDet
        Dim repositorioPermissaoMenuDet As New repositorioPermissaoMenuDet

        Try

            If Trim(Me.txtCodigo.Text) <> "0" Then

                objUsuario.PERFIL_MENU = CInt(Me.txtCodigo.Text)

                vlist = repositorioUsuario.SelectCount(objUsuario)

                If vlist.Count > 0 Then

                    For Each objUsuarioFor As entidadeUsuario In vlist

                        vCountPerfil = Convert.ToInt32(Trim(IIf(String.IsNullOrEmpty((objUsuarioFor.ID)), "0", objUsuarioFor.ID)))

                    Next

                End If

                If vCountPerfil > 0 Then

                    MessageBox.Show("Não é possível excluir o perfil selecionado, pois existe um ou mais usuários vinculados a ele!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)

                Else

                    vResp = MessageBox.Show("Esta operação não possui retorno!" & vbCrLf & vbCrLf & "Tem certeza de que deseja excluir este registro?", "Confirmação de Exclusão", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

                    If vResp = DialogResult.Yes Then

                        objPermissaoMenuDet.ID_SISTEMA = vintIdSistema
                        objPermissaoMenuDet.ID_PERMISSAO = CInt(Me.txtCodigo.Text)

                        If Not lDelete(objPermissaoMenuDet) Then

                            MessageBox.Show("Perfil não pode ser excluído!", "Confirmação de Exclusão", MessageBoxButtons.OK, MessageBoxIcon.Information)

                        Else

                            objPermissaoMenuDet.ID_PERMISSAO = CInt(Me.txtCodigo.Text)
                            vlist = repositorioPermissaoMenuDet.SelectCount(objPermissaoMenuDet)

                            If vlist.Count > 0 Then

                                objPermissaoMenu.ID_PERMISSAO = CInt(Me.txtCodigo.Text)

                                repositorioPermissaoMenu.Delete(objPermissaoMenu)

                            End If

                            MessageBox.Show("Perfil excluído com sucesso!", "Confirmação de Exclusão", MessageBoxButtons.OK, MessageBoxIcon.Information)

                            lClear()

                        End If

                    ElseIf vResp = DialogResult.Cancel Then
                        Exit Sub
                    End If

                End If
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try


    End Sub

    Private Sub frmCadastroPermissaoMenu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

        Try

            If e.Control Then
                Select Case e.KeyCode
                    Case Keys.N
                        e.SuppressKeyPress = True
                        btNovo.PerformClick()
                    Case Keys.S
                        e.SuppressKeyPress = True
                        btSalvar.PerformClick()
                    Case Keys.E
                        e.SuppressKeyPress = True
                        btExcluir.PerformClick()
                    Case Keys.L
                        e.SuppressKeyPress = True
                        btPesquisar.PerformClick()
                End Select
            Else
                If e.KeyCode = Keys.Escape Then
                    e.SuppressKeyPress = True
                    Me.btFechar.PerformClick()
                End If
            End If

        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

    Private Sub frmCadastroPermissaoMenu_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

        Try
            If Not vfrmOld Is Nothing Then
                vfrmOld.Enabled = True
            End If
        Catch ex As Exception
            MessageBox.Show("Ocorreu um Erro Inesperado. Favor, Contate o Administrador do Sistema ou a Equipe de Suporte de sua localidade! ", "Fatal - Erro", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End Try

    End Sub

#End Region

#Region "Métodos"

    Private Sub lNew()

        Try

            vNew = True

            lClear()

            Me.getTreeFromMenu(Me.tvPermissoes, Me.mMenuStrip)
            Me.txtCodigo.Select()


        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub lClose()

        Try
            Me.Close()

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub lClear()

        Try

            Me.txtCodigo.Text = String.Empty
            Me.txtDescricao.Text = String.Empty

            Me.chkExpandirTudo.Checked = False
            Me.chkSelecionarTodos.Checked = False

        Catch ex As Exception
            Exit Sub
        End Try



    End Sub

    Public Sub SetaParametros(ByRef frmOld As System.Windows.Forms.Form)

        Try

            vfrmOld = frmOld
            vfrmOld.Enabled = False

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Public Sub SetaCodigo(ByVal astrTabela As String, ByVal alngId_A As Integer, Optional ByVal alngId_B As Integer = 0, Optional ByVal alngId_C As Integer = 0, Optional ByVal alngId_D As Integer = 0)

        Dim vlist

        Dim objPermissaoMenu As New entidadePermissaoMenu
        Dim repositorioPermissaoMenu As New repositorioPermissaoMenu

        Dim objPermissaoMenuDet As New entidadePermissaoMenuDet
        Dim repositorioPermissaoMenuDet As New repositorioPermissaoMenuDet

        Try

            If astrTabela.ToUpper() = "PERMISSAO_MENU" Then

                objPermissaoMenu.ID_PERMISSAO = alngId_A

                vlist = repositorioPermissaoMenu.Select_1(objPermissaoMenu)

                If vlist.Count > 0 Then

                    For Each objPermissaoMenuFor As entidadePermissaoMenu In vlist

                        Me.txtCodigo.Text = IIf(IsDBNull(Strings.Format(objPermissaoMenuFor.ID_PERMISSAO, "00000")), 0, Strings.Format(objPermissaoMenuFor.ID_PERMISSAO, "00000"))
                        Me.txtDescricao.Text = IIf(IsDBNull(objPermissaoMenuFor.DESCRICAO), String.Empty, objPermissaoMenuFor.DESCRICAO)

                        '[Preenche a TreeView com um modelo do menu atual]
                        Me.getTreeFromMenu(Me.tvPermissoes, mMenuStrip)

                        vNew = False

                        '[Carrega as permissões gravadas para o perfil selecionado e exibe na TreeView]

                        objPermissaoMenuDet.ID_SISTEMA = vintIdSistema
                        objPermissaoMenuDet.ID_PERMISSAO = CInt(Me.txtCodigo.Text)


                        vlist = repositorioPermissaoMenuDet.Select(objPermissaoMenuDet)

                        If vlist.Count > 0 Then

                            For Each objPermissaoMenuDetFor As entidadePermissaoMenuDet In vlist

                                Me.getTree(Me.tvPermissoes.Nodes, objPermissaoMenuDetFor.NAME, CBool(objPermissaoMenuDetFor.HABILITADO))

                            Next

                        End If

                    Next

                Else
                    MessageBox.Show("Não foi possível encontrar o registro!", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information)

                    Me.txtCodigo.Clear()
                    Me.txtDescricao.Clear()

                    Me.tvPermissoes.Nodes.Clear()

                End If

                Me.txtCodigo.SelectAll()

            End If

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub SelectAllTreeViewNodes(ByVal nodes As TreeNodeCollection, ByVal pSelect As Boolean)

        Try

            For Each node As TreeNode In nodes
                Me.SelectAllTreeViewDrillDownNodes(node, pSelect)
                Me.SelectAllTreeViewNodes(node.Nodes, pSelect)
            Next

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub SelectAllTreeViewDrillDownNodes(ByVal Node As TreeNode, ByVal pSelect As Boolean)

        Try

            Node.Checked = pSelect

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub CheckAllChildren(ByVal tn As TreeNode, ByVal bCheck As Boolean)

        Try

            bParentTrigger = False

            For Each ctn As TreeNode In tn.Nodes
                bChildTrigger = False
                ctn.Checked = bCheck
                bChildTrigger = True

                CheckAllChildren(ctn, bCheck)
            Next

            bParentTrigger = True

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub CheckMyParent(ByVal tn As TreeNode, ByVal bCheck As Boolean)


        Try

            If tn Is Nothing Then
                Exit Sub
            End If

            If tn.Parent Is Nothing Then
                Exit Sub
            End If

            bChildTrigger = False
            bParentTrigger = False
            tn.Parent.Checked = bCheck

            CheckMyParent(tn.Parent, bCheck)

            bParentTrigger = True
            bChildTrigger = True

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    '[######## Monta a TreeView de acordo com o menu passado por parâmetro ################]

    Private Sub getTreeFromMenu(ByRef pTreeView As TreeView, ByRef pMenuStrip As MenuStrip)

        Try

            pTreeView.Nodes.Clear()

            contTag = 1

            For Each tsmi As ToolStripMenuItem In pMenuStrip.Items
                Dim tn = New TreeNode(Replace(Replace(tsmi.Text, "&", ""), "...", ""))

                tn.Name = tsmi.Name

                Me.getChildNodes(tsmi, tn)

                tn.Tag = contTag
                contTag += 1

                pTreeView.Nodes.Add(tn)
            Next

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub getChildNodes(ByVal mi As ToolStripDropDownItem, ByVal tn As TreeNode)


        Try

            For Each item As Object In mi.DropDownItems
                If TypeOf item Is ToolStripSeparator Then
                    Dim node = New TreeNode("–––––––––––––––––––––––––")

                    node.Name = item.name
                    node.Tag = contTag

                    contTag += 1

                    tn.Nodes.Add(node)
                Else
                    Dim node = New TreeNode(Replace(Replace(item.Text, "&", ""), "...", ""))

                    node.Name = item.name
                    node.Tag = contTag

                    contTag += 1

                    tn.Nodes.Add(node)

                    Me.getChildNodes(item, node)
                End If
            Next

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    '[#####################################################################################]	

    '[Pega as permissões gravadas para o perfil e exibe na TreeView para edição]

    Private Sub getTree(ByVal nodes As TreeNodeCollection, ByVal nodeName As String, ByVal nodeVisible As Boolean)

        Try

            For Each node As TreeNode In nodes
                Me.getNode(node, nodeName, nodeVisible)
                Me.getTree(node.Nodes, nodeName, nodeVisible)
            Next

        Catch ex As Exception
            Exit Sub
        End Try


    End Sub

    Private Sub getNode(ByVal Node As TreeNode, ByVal nodeName As String, ByVal nodeVisible As Boolean)

        Try

            If Node.Name.ToUpper() = nodeName Then
                Node.Checked = nodeVisible
            End If

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    '[##########################################################################]	

    '[######## Salva a TreeView no banco de dados; grava também o estado dos checkboxes ###]

    Private Sub SaveTree(ByVal nodes As TreeNodeCollection)

        Try

            For Each node As TreeNode In nodes
                Me.SaveNode(node, vintIdSistema)
                Me.SaveTree(node.Nodes)
            Next

        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    Private Sub SaveNode(ByVal Node As TreeNode, ByVal pIdSistema As Integer)

        Dim nodeId As Integer
        Dim nodeName As String
        Dim nodeText As String
        Dim nodeParentId As Integer
        Dim nodeIsChecked As Boolean

        Dim objPermissaoMenuDet As New entidadePermissaoMenuDet
        Dim repositorioPermissaoMenuDet As New repositorioPermissaoMenuDet

        Try

            nodeId = Node.Tag
            nodeName = Node.Name
            nodeText = Node.Text
            nodeParentId = -1
            nodeIsChecked = Node.Checked

            If Not IsNothing(Node.Parent) Then
                nodeParentId = Node.Parent.Tag
            End If



            objPermissaoMenuDet.ID_SISTEMA = pIdSistema
            objPermissaoMenuDet.ID_DETALHE = nodeId
            objPermissaoMenuDet.ID_PERMISSAO = CInt(Me.txtCodigo.Text)
            objPermissaoMenuDet.NAME = nodeName
            objPermissaoMenuDet.TEXT = nodeText
            objPermissaoMenuDet.PARENT_ID = nodeParentId
            objPermissaoMenuDet.HABILITADO = nodeIsChecked

            repositorioPermissaoMenuDet.Insert(objPermissaoMenuDet)


        Catch ex As Exception
            Exit Sub
        End Try

    End Sub

    '[#####################################################################################]	

    Private Function lInsert() As Boolean

        Dim objPermissaoMenu As New entidadePermissaoMenu
        Dim repositorioPermissaoMenu As New repositorioPermissaoMenu

        Try

            objPermissaoMenu.DESCRICAO = Convert.ToString(Trim(IIf(String.IsNullOrEmpty((Me.txtDescricao.Text.Trim)), String.Empty, Me.txtDescricao.Text.Trim)))

            repositorioPermissaoMenu.Insert(objPermissaoMenu)

            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Function lUpdate() As Boolean

        Dim objPermissaoMenu As New entidadePermissaoMenu
        Dim repositorioPermissaoMenu As New repositorioPermissaoMenu


        Try

            objPermissaoMenu.DESCRICAO = Convert.ToString(Trim(IIf(String.IsNullOrEmpty((Me.txtDescricao.Text.Trim)), String.Empty, Me.txtDescricao.Text.Trim)))
            objPermissaoMenu.ID_PERMISSAO = Convert.ToInt32(Trim(IIf(String.IsNullOrEmpty((Me.txtCodigo.Text)), "0", Me.txtCodigo.Text)))

            repositorioPermissaoMenu.Update(objPermissaoMenu)

            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Private Function lDelete(ByRef objPermissaoMenuDet As entidadePermissaoMenuDet) As Boolean

        Dim repositorioPermissaoMenuDet As New repositorioPermissaoMenuDet

        Try

            repositorioPermissaoMenuDet.Delete(objPermissaoMenuDet)

            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

#End Region

End Class