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.Windows.Forms
Imports System.Data.OleDb
Imports DevExpress.Data.Filtering

Public Class ImmovablesVC
    Inherits Nuvolar.Module.ImmovablesVC

    Dim _WaitFormClass As New WaitFormClass

    Public Sub New()
        MyBase.New()
        InitializeComponent()
        RegisterActions(components)
    End Sub
    Protected Overrides Sub OnActivated()
        MyBase.OnActivated()
        AddHandler DoWork, AddressOf _WaitFormClass.DoWork
        AddHandler InitWork, AddressOf _WaitFormClass.InitWork
        AddHandler FinalWork, AddressOf _WaitFormClass.FinalWork
    End Sub
    Protected Overrides Sub OnDeactivated()
        MyBase.OnDeactivated()
        RemoveHandler DoWork, AddressOf _WaitFormClass.DoWork
        RemoveHandler InitWork, AddressOf _WaitFormClass.InitWork
        RemoveHandler FinalWork, AddressOf _WaitFormClass.FinalWork
    End Sub
    Private Sub aImmovables_ImportLotNumber_Execute(sender As Object, e As SimpleActionExecuteEventArgs) Handles aImmovables_ImportLotNumber.Execute
        Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", False)

        Dim _ocur As Xpo.XPObjectSpace = TryCast(View.ObjectSpace, Xpo.XPObjectSpace)

        Dim _Array_UniqueObjects As New ArrayList
        Dim _Array_ImmovablesGroups As New ArrayList
        Dim _Array_ImmovablesType As New ArrayList
        Dim _Array_LotNumber As New ArrayList
        Dim _Array_ZIP As New ArrayList
        Dim _Array_City As New ArrayList
        Dim _Array_Street As New ArrayList
        Dim _MissingImmovable As Long = 0
        Dim _MissingImmovablesUO As New ArrayList

        Dim _OpenFileDialog As New OpenFileDialog
        _OpenFileDialog.Filter = "Xls Files (.xls)|*.xls|Xlsx Files (.xlsx)|*.xlsx|All Files (*.*)|*.*"

        If _OpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If Not String.IsNullOrEmpty(_OpenFileDialog.FileName) Then
                Dim _xlsFile As String = _OpenFileDialog.FileName

                Dim _ConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;"
                _ConnectionString += "Data Source=" & _xlsFile.ToString & ";"
                _ConnectionString += "Extended Properties=""Excel 12.0 Xml;HDR=YES;"""
                Dim _OleDbConnection As New OleDbConnection(_ConnectionString)
                Dim _OleDbCommand As New OleDbCommand
                Dim _OleDbDataReader As OleDbDataReader
                Dim _mSQL As String
                Dim _UpdatedCount As Long = 0

                '_WaitFormClass.InitWork
                Try

                    _mSQL = "select * from [Sheet$]"
                    _OleDbCommand.CommandTimeout = 60
                    _OleDbCommand.Connection = _OleDbConnection
                    _OleDbCommand.CommandType = CommandType.Text
                    _OleDbCommand.CommandText = _mSQL
                    _OleDbConnection.Open()

                    If _OleDbConnection.State = ConnectionState.Open Then
                        _OleDbDataReader = _OleDbCommand.ExecuteReader(CommandBehavior.SingleResult)
                        While _OleDbDataReader.Read
                            If Not String.IsNullOrEmpty(_OleDbDataReader(0).ToString) Then
                                _Array_UniqueObjects.Add(_OleDbDataReader(0).ToString)
                                _Array_ImmovablesGroups.Add(_OleDbDataReader(1).ToString)
                                _Array_ImmovablesType.Add(_OleDbDataReader(2).ToString)
                                _Array_LotNumber.Add(_OleDbDataReader(3).ToString)
                                _Array_ZIP.Add(_OleDbDataReader(4).ToString)
                                _Array_City.Add(_OleDbDataReader(5).ToString)
                                _Array_Street.Add(_OleDbDataReader(6).ToString)
                            End If
                        End While

                        _OleDbDataReader.Close()
                    End If
                    _OleDbConnection.Close()
                Catch ex As Exception
                    MsgBox(ex.Message)
                    _OleDbConnection.Close()
                End Try


                _WaitFormClass.InitWork(0, 1, _Array_UniqueObjects.Count)
                For ik = 0 To _Array_UniqueObjects.Count - 1 Step 1
                    Dim _UniqueObjects As UniqueObjects = _ocur.FindObject(Of UniqueObjects)(CriteriaOperator.Parse("Name=?", _Array_UniqueObjects(ik)))
                    If _UniqueObjects IsNot Nothing Then
                        Dim _Immovables As Immovables = _ocur.FindObject(Of Immovables)(CriteriaOperator.Parse("UniqueObjects=?", _UniqueObjects))
                        If _Immovables IsNot Nothing Then
                            _Immovables.LotNumber = _Array_LotNumber(ik)

                            If _Immovables.Address Is Nothing Then
                                Dim _Address As New DevExpress.Persistent.BaseImpl.Address(_ocur.Session)
                                _Address.ZipPostal = _Array_ZIP(ik)
                                _Address.City = _Array_City(ik)
                                _Address.Street = _Array_Street(ik)

                                _Immovables.Address = _Address
                            End If
                        Else
                            _MissingImmovable += 1
                            _MissingImmovablesUO.Add(_Array_UniqueObjects(ik))
                        End If
                    End If
                    _WaitFormClass.DoWork()
                Next
            End If
            _ocur.CommitChanges()
            _Waitformclass.FinalWork()
            If _MissingImmovable > 0 Then
                Dim _UOList As String = ""
                For Each _UO As String In _MissingImmovablesUO
                    _UOList += _UO + vbNewLine + vbNewLine
                Next
                _UOList += vbNewLine
                MsgBox(String.Format("{0} db ingatlant nem lehet beazonosítani az egyedi azonosító alapján! {1}", _MissingImmovable, _UOList))
            End If
        End If
        Frame.GetController(Of Validation.PersistenceValidationController).Active.SetItemValue("", True)
    End Sub
End Class
