Imports System.Collections.Generic
Imports MicroFour.StrataFrame.Strings.StringBasics
Imports MicroFour.StrataFrame.DBEngine
Imports MicroFour.StrataFrame.DBEngine.SQL

''' <summary>
''' This sample class shows how to use the DatabaseMigrator class to deploy a DDT package
''' file to a SQL server instance.  This sample assumes that the StrataFrameSample.pkg file
''' is in the same location as this assembly.  Also, you may need to adjust the SQL Server settings
''' that are used in the Delare Privates region to work for your environment.
''' </summary>
''' <remarks></remarks>
Public Class DataProgress

#Region " Declare Privates "

    Private WithEvents _DatabaseSetup As SQL.DatabaseMigrator
    Private _SQLServer As String = "localhost"
    Private _SQLUserName As String = ""
    Private _SQLPassword As String = ""
    Private _PackagePath As String = IO.Path.GetDirectoryName(Reflection.Assembly.GetEntryAssembly.Location)

#End Region

#Region " Deployment Methods "

    ''' <summary>
    ''' Starts the deployment process
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub StartDeployment()
        '-- Establish Locals
        Dim loSQL As New MicroFour.StrataFrame.DBEngine.SQL.SQLServerSchema()

        '-- See if the SQL connection can be established.  Change the privates above to make this
        '   work in your environment if localhost is not a valid connection.  Another option is to 
        '   create a form that asks for this information or pull it from the registry.
        If Not loSQL.IsValidConnection(_SQLServer, _SQLUserName, _SQLPassword) Then
            MsgBox("The specified SQL connection information could not be authenticated.  Deployment halted.", MsgBoxStyle.Exclamation, "Invalid Connection")
            DialogResult = Windows.Forms.DialogResult.Cancel
            Exit Sub
        End If

        '-- Adjust the path to move up two parent folders where the PKG file is expected to be.
        _PackagePath = IO.Directory.GetParent(_PackagePath).FullName
        _PackagePath = AddBS(IO.Directory.GetParent(_PackagePath).FullName)

        '-- Create the DatabaseMigrator instance
        If _SQLUserName.Length = 0 Then
            _DatabaseSetup = New DatabaseMigrator(_SQLServer, True, Me)
        Else
            _DatabaseSetup = New DatabaseMigrator(_SQLServer, False, Me, _SQLUserName, _SQLPassword)
        End If

        '-- Begin the deployment of the meta-data (does not include the data in this phase)
        _DatabaseSetup.DeployMetaData(_PackagePath & "StrataFrameSample.pkg", "framework")
    End Sub

    ''' <summary>
    ''' Assumes that the meta-data has already been deployed and will deploy the
    ''' deployment data packages within the package file.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub StartDataDeploymentPhase()
        '-- Establish Locals
        Dim laPKs As New List(Of Integer)
        Dim loItem As DeployDataInfo

        '-- Build a collection with all of the deployment primary keys.  This will cycle through
        '   all of the deployment packages within the package file.  You can use this same method
        '   do display a list to the end-user allowing them to choose which data items will be installed.
        '   In this sample, we are going to deploy all of the deployment data packages.
        For Each loItem In DatabaseMigrator.EnumDeploymentPackages(_PackagePath & "StrataFrameSample.pkg", "framework")
            laPKs.Add(loItem.PrimaryKey)
        Next

        '-- Start the deployment of the data
        Me._DatabaseSetup.DeployData(_PackagePath & "StrataFrameSample.pkg", "framework", laPKs.ToArray())
    End Sub

#End Region

#Region " Handled Events "

    ''' <summary>
    ''' Start the deployment timer on the form load
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DataProgress_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        '-- Used a timer simply to allow enough time for the form to display.
        tmStart.Enabled = True
    End Sub

    ''' <summary>
    ''' Handles the starting timer tick event.  THE DEPLOYMENT STARTS HERE!!!
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub tmStart_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmStart.Tick
        '-- Stop the timer
        tmStart.Enabled = False

        '-- Start the deployment
        StartDeployment()
    End Sub

    ''' <summary>
    ''' This event indicates that the meta-data portion has been completed
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub _DatabaseSetup_BuildComplete(ByVal e As MicroFour.StrataFrame.DBEngine.SQL.SQLManagerEventArgs) Handles _DatabaseSetup.BuildComplete
        '-- Starts the data deployment portion of the install
        StartDataDeploymentPhase()
    End Sub

    ''' <summary>
    ''' Indicates that the data deployment started by the DeployData command is complete
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub _DatabaseSetup_DeployDataComplete(ByVal e As MicroFour.StrataFrame.DBEngine.SQL.SQLManagerEventArgs) Handles _DatabaseSetup.DeployDataComplete
        '-- Since we are finished, close the form with a good result
        DialogResult = Windows.Forms.DialogResult.OK
    End Sub

    ''' <summary>
    ''' Handles the progress for the data deployment portion of the deployment
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub _DatabaseSetup_DeployDataProgress(ByVal e As MicroFour.StrataFrame.DBEngine.SQL.SQLManagerEventArgs) Handles _DatabaseSetup.DeployDataProgress
        Me.lblTitle.Text = e.Title
        Me.lblMsg.Text = e.Message
        Me.ProgressBar1.Value = e.ProgressPercent
    End Sub

    ''' <summary>
    ''' If an error occurs, display the error and exit
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub _DatabaseSetup_ErrorReached(ByVal e As MicroFour.StrataFrame.DBEngine.SQL.SQLManagerEventArgs) Handles _DatabaseSetup.ErrorReached
        MsgBox(e.Detail, MsgBoxStyle.Critical, "Error Occurred")
        DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    ''' <summary>
    ''' Display the progress update for the meta-data portion of the deployment
    ''' </summary>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub _DatabaseSetup_ProgressUpdate(ByVal e As MicroFour.StrataFrame.DBEngine.SQL.SQLManagerEventArgs) Handles _DatabaseSetup.ProgressUpdate
        Me.lblTitle.Text = e.Title
        Me.lblMsg.Text = e.Message
        Me.ProgressBar1.Value = e.ProgressPercent
    End Sub

#End Region

    
    
End Class