Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text

Imports DevExpress.ExpressApp
Imports DevExpress.ExpressApp.Actions
Imports DevExpress.Persistent.Base
Imports System.Xml
Imports DevExpress.Xpo
Imports DevExpress.Persistent.BaseImpl
Imports DevExpress.Data.Filtering
Imports DevExpress.ExpressApp.CloneObject

Public Class BusinessDirectory_VC
    Inherits DevExpress.ExpressApp.ViewController

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        If View.Id = "Role_ListView_BusinessDirectory" Then
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", False)
        End If
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        If View.Id = "Role_ListView_BusinessDirectory" Then
            Frame.GetController(Of CloneObjectViewController)().Active.SetItemValue("", True)
        End If
    End Sub
    Private Sub aBusinessDirectory_OppenDocument_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aBusinessDirectory_OppenDocument.Execute
        Try
            Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
            Dim _BusinessDirectory As BusinessDirectory = TryCast(View.SelectedObjects(0), BusinessDirectory)
            ProcessDocument(_BusinessDirectory.FileData)

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Critical, "Hiba!")
        End Try
    End Sub
    Private Sub ProcessDocument(_DocumentFile As DevExpress.Persistent.BaseImpl.FileData)
        Dim _FilePath As String = System.IO.Path.Combine(System.IO.Path.GetTempPath(), _DocumentFile.FileName)
        Dim _Stream As System.IO.Stream = New System.IO.FileStream(_FilePath, System.IO.FileMode.Create)
        _DocumentFile.SaveToStream(_Stream)
        _Stream.Close()
        If IO.Path.GetExtension(_FilePath).Replace(".", "").ToUpper = "XML" Then

            'Dim _Arguments As String
            'Select Case GLOBAL_SQLType
            '    Case eSQLType.MSSQL
            '        _Arguments = """" & _FilePath + """ " + GLOBAL_SQLUser + " " + GLOBAL_SQLPassword + " MSSQL " + GLOBAL_SQLDatabase + " " + GLOBAL_SQLServer
            '    Case eSQLType.MySQL
            '        _Arguments = """" & _FilePath + """ " + GLOBAL_SQLUser + " " + GLOBAL_SQLPassword + " MySQL " + GLOBAL_SQLDatabase + " " + GLOBAL_SQLServer
            '    Case eSQLType.PostgreSQL
            '        _Arguments = """" & _FilePath + """ " + GLOBAL_SQLUser + " " + GLOBAL_SQLPassword + " PostgreSQL " + GLOBAL_SQLDatabase + " " + GLOBAL_SQLServer
            'End Select

            'Process.Start(GLOBAL_ApplicationStartupPath + "DashBoardViewer.exe", _Arguments)
        Else

            Process.Start(_FilePath)
        End If
    End Sub
    Private Sub aBusinessDirectory_OpenDashboardWithViewer_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aBusinessDirectory_OpenDashboardWithViewer.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _BusinessDirectory As BusinessDirectory = TryCast(View.SelectedObjects(0), BusinessDirectory)

        Dim _Dashboard As New Dashboard(_onew.Session)
        With _Dashboard
            .DashboardViewMode = DashboardViewMode.Viewer
            .UserName = _BusinessDirectory.DashBoard_UserName
            .Password = _BusinessDirectory.DashBoard_Password
            .SQLServer = _BusinessDirectory.DashBoard_Server
            .Database = _BusinessDirectory.DashBoard_Database
            .XMLFileData = _onew.GetObject(_BusinessDirectory.FileData)
            .SQLType = _BusinessDirectory.SQLType

        End With

        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "Dashboard_DetailView_Viewer", True, _Dashboard)
    End Sub
    Private Sub aBusinessDirectory_OpenDashboardWithDesigner_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aBusinessDirectory_OpenDashboardWithDesigner.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _BusinessDirectory As BusinessDirectory = TryCast(View.SelectedObjects(0), BusinessDirectory)

        Dim _Dashboard As New Dashboard(_onew.Session)
        With _Dashboard
            .DashboardViewMode = DashboardViewMode.Designer
            .UserName = _BusinessDirectory.DashBoard_UserName
            .Password = _BusinessDirectory.DashBoard_Password
            .SQLServer = _BusinessDirectory.DashBoard_Server
            .Database = _BusinessDirectory.DashBoard_Database
            .XMLFileData = _onew.GetObject(_BusinessDirectory.FileData)
            .SQLType = _BusinessDirectory.SQLType
            .Object_Oid = _BusinessDirectory.Oid.ToString
            .Object_Type = GetType(BusinessDirectory)
        End With

        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "Dashboard_DetailView_Designer", True, _Dashboard)

    End Sub
    Private Sub aBusinessDirectory_SetRoleDisabled_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aBusinessDirectory_SetRoleDisabled.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Role_Collection As New CollectionSource(_onew, GetType(Role))

        e.View = Application.CreateListView("Role_ListView_BusinessDirectory", _Role_Collection, True)
    End Sub
    Private Sub aBusinessDirectory_SetRoleDisabled_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aBusinessDirectory_SetRoleDisabled.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        For Each _Role As Role In e.PopupWindow.View.SelectedObjects
            For Each _BusinessDirectory As BusinessDirectory In View.SelectedObjects
                Dim _BusinessDirectory_Roles As New BusinessDirectory_Roles(_ocur.Session)
                With _BusinessDirectory_Roles
                    .BusinessDirectory = _BusinessDirectory
                    .Role = _ocur.GetObjectByKey(Of Role)(_Role.Oid)
                End With
            Next
        Next
        _ocur.CommitChanges()
    End Sub
    Private Sub aBusinessDirectory_SetRoleEnabled_CustomizePopupWindowParams(sender As Object, e As CustomizePopupWindowParamsEventArgs) Handles aBusinessDirectory_SetRoleEnabled.CustomizePopupWindowParams
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Role_Collection As New CollectionSource(_onew, GetType(Role))

        e.View = Application.CreateListView("Role_ListView_BusinessDirectory", _Role_Collection, True)
    End Sub
    Private Sub aBusinessDirectory_SetRoleEnabled_Execute(sender As Object, e As PopupWindowShowActionExecuteEventArgs) Handles aBusinessDirectory_SetRoleEnabled.Execute
        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)
        Dim _BusinessDirectory_Roles_DELCollection As New ArrayList
        For Each _Role As Role In e.PopupWindow.View.SelectedObjects
            For Each _BusinessDirectory As BusinessDirectory In View.SelectedObjects
                Dim _BusinessDirectory_Roles As BusinessDirectory_Roles = _ocur.FindObject(Of BusinessDirectory_Roles)(CriteriaOperator.Parse("BusinessDirectory=? AND Role=?", _BusinessDirectory, _ocur.GetObjectByKey(Of Role)(_Role.Oid)))
                If _BusinessDirectory_Roles IsNot Nothing Then
                    _BusinessDirectory_Roles_DELCollection.Add(_BusinessDirectory_Roles)
                End If
            Next
        Next
        _ocur.Delete(_BusinessDirectory_Roles_DELCollection)
        _ocur.CommitChanges()
    End Sub
    Private Sub aBusinessDirectory_Clone_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aBusinessDirectory_Clone.Execute
        Dim _ocur As Xpo.XPObjectSpace = Application.CreateObjectSpace

        For Each _BusinessDirectory As BusinessDirectory In View.SelectedObjects
            Dim _BusinessDirectory_New As New BusinessDirectory(_ocur.Session)
            With _BusinessDirectory_New
                .BusinessDirectory_Parent = _ocur.GetObject(_BusinessDirectory.Parent)
                .ShortName = _BusinessDirectory.ShortName + " Másolat"
                .IsFolder = _BusinessDirectory.IsFolder
                If _BusinessDirectory.FileData IsNot Nothing Then
                    Dim _FileName As String = System.IO.Path.GetTempPath() + _BusinessDirectory.FileData.FileName
                    Dim _StreamFile As System.IO.Stream = New System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write)
                    _BusinessDirectory.FileData.SaveToStream(_StreamFile)
                    _StreamFile.Close()

                    .FileData = Nothing

                    .FileData = LoadOtherFile(_FileName, New FileData(_ocur.Session))
                Else
                    .FileData = Nothing
                End If

                .FileCreated = _BusinessDirectory.FileCreated
                .FileModified = _BusinessDirectory.FileModified
                .Completed = _BusinessDirectory.Completed
                .TaskDescription = _BusinessDirectory.TaskDescription
                .EMail_Sender = _BusinessDirectory.EMail_Sender
                .Email_Copy = _BusinessDirectory.Email_Copy
                .IsDashBoard = _BusinessDirectory.IsDashBoard
                .DashBoard_Server = _BusinessDirectory.DashBoard_Server
                .DashBoard_Database = _BusinessDirectory.DashBoard_Database
                .DashBoard_UserName = _BusinessDirectory.DashBoard_UserName
                .DashBoard_Password = _BusinessDirectory.DashBoard_Password
                .SQLType = _BusinessDirectory.SQLType

            End With
            
        Next

        _ocur.CommitChanges()
    End Sub
    Private Function LoadOtherFile(ByVal _ActFile As String, _FileData As FileData) As FileData
        Dim _StreamFile = New System.IO.FileStream(_ActFile, System.IO.FileMode.Open, System.IO.FileAccess.Read)
        Dim _StreamReader As New System.IO.StreamReader(_StreamFile)
        _StreamFile.Seek(0, System.IO.SeekOrigin.Begin)

        _FileData.LoadFromStream(_ActFile, _StreamFile)
        _FileData.FileName = System.IO.Path.GetFileName(_ActFile)
        _StreamReader.Close()
        _StreamFile.Close()

        Return _FileData
    End Function

  
    Private Sub aBusinessDirectory_NewDashboard_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aBusinessDirectory_NewDashboard.Execute
        Dim _onew As Xpo.XPObjectSpace = Application.CreateObjectSpace
        Dim _Dashboard As New Dashboard(_onew.Session)
        With _Dashboard
            .DashboardViewMode = DashboardViewMode.Designer
            .IsNewDashboard = True
        End With

        e.ShowViewParameters.CreatedView = Application.CreateDetailView(_onew, "Dashboard_DetailView_Designer", True, _Dashboard)
    End Sub
End Class
