Imports System.Collections.ObjectModel Imports System.Windows.Forms Imports EgtWPFLib5 Imports MySql.Data.MySqlClient Public Class SearchKeyPageVM Inherits KeyPageVM #Region "FIELDS & PROPERTIES" Private m_SelSearchResult As SearchKey Public Property SelSearchResult As SearchKey Get Return m_SelSearchResult End Get Set(value As SearchKey) m_SelSearchResult = value End Set End Property Private m_Number As Integer Public Property Number As String Get Return If(m_Number <> 0, m_Number.ToString(), "") End Get Set(value As String) If Not String.IsNullOrWhiteSpace(value) Then If Not Integer.TryParse(value, m_Number) Then m_Number = Nothing End If Else m_Number = Nothing End If End Set End Property Private m_SearchResult As ObservableCollection(Of SearchKey) Public ReadOnly Property SearchResult As ObservableCollection(Of SearchKey) Get Return m_SearchResult End Get End Property Private m_StateList As New List(Of String)({"Consegnata", "InDeposito", "InDepositoEgt", "Guasta", "Interna", "InternaEgt", "InDepositoTrial", "---ANY---"}) Public Overloads ReadOnly Property StateList As List(Of String) Get Return m_StateList End Get End Property Friend Overloads Sub SetStateList(value As List(Of String)) m_StateList = value End Sub Private m_SelState As Key.KeyState Public Overloads Property SelState As String Get Return m_SelState.ToString() End Get Set(value As String) If String.IsNullOrWhiteSpace(value) Then ' m_SelState = Nothing ElseIf value.Equals("Consegnata") Then m_SelState = Key.KeyState.Consegnata ElseIf value.Equals("InDeposito") Then m_SelState = Key.KeyState.InDeposito ElseIf value.Equals("InDepositoEgt") Then m_SelState = Key.KeyState.InDepositoEgt ElseIf value.Equals("Guasta") Then m_SelState = Key.KeyState.Guasta ElseIf value.Equals("Interna") Then m_SelState = Key.KeyState.Interna ElseIf value.Equals("InternaEgt") Then m_SelState = Key.KeyState.InternaEgt ElseIf value.Equals("InDepositoTrial") Then m_SelState = Key.KeyState.InDepositoTrial ElseIf value.Equals("---ANY---") Then m_SelState = Key.KeyState.ANY End If NotifyPropertyChanged("SelState") End Set End Property Private m_Note As String Public Overloads Property Note As String Get Return m_Note End Get Set(value As String) m_Note = value NotifyPropertyChanged("Note") End Set End Property Private m_Seriale As String Public Overloads Property Seriale As String Get Return m_Seriale End Get Set(value As String) m_Seriale = value NotifyPropertyChanged("Seriale") End Set End Property ' Definizione comandi Private m_cmdNewKey As Command Private m_cmdSearch As Command Private m_cmdCancel As Command Private m_cmdUpdate As Command Private m_cmdDelete As Command #Region "Messages" Public ReadOnly Property SearchKeyMsg As String Get Return "Search key" End Get End Property Public ReadOnly Property NewMsg As String Get Return "New" End Get End Property Public ReadOnly Property SearchMsg As String Get Return "Search" End Get End Property Public ReadOnly Property CancelMsg As String Get Return "Close" End Get End Property Public ReadOnly Property UpdateMsg As String Get Return "Update" End Get End Property Public ReadOnly Property DeleteMsg As String Get Return "Delete" End Get End Property #End Region ' Messages #End Region ' FIELDS & PROPERTIES #Region "CONSTRUCTOR" Sub New() MyBase.New() ' Imposto riferimento nella mappa Map.SetRefSearchKeyPageVM(Me) End Sub #End Region ' CONSTRUCTOR #Region "METHODS" Friend Sub InitSearchKeyPage() MyBase.InitKeyPage() ' Svuoto campi m_Number = 0 NotifyPropertyChanged("Number") ' Nascondo la GeneratedLockID GeneratedLockID_Visibility = Visibility.Collapsed ' m_SearchResult = New ObservableCollection(Of SearchKey)() Dim Query As String = "SELECT * FROM " & DB_KEY & " LEFT JOIN " & DB_CLIENT & " ON " & DB_KEY & "." & DB_CLIENTID & " = " & DB_CLIENT & "." & DB_CLIENTID & " " & " ORDER BY " & DB_NUMBER m_SearchResult = New ObservableCollection(Of SearchKey)(ManageDb.ExecuteSearchKeyQuery(Query)) NotifyPropertyChanged("SearchResult") m_SelState = Key.KeyState.ANY NotifyPropertyChanged("SelState") KeyDate = Nothing NotifyPropertyChanged("KeyDate") End Sub #End Region ' METHODS #Region "COMMANDS" #Region "Search" ' Returns a command that manage the MainWindow_Unloaded command Public ReadOnly Property SearchKey_Command As ICommand Get If m_cmdSearch Is Nothing Then m_cmdSearch = New Command(AddressOf Search) End If Return m_cmdSearch End Get End Property Public Sub Search(ByVal param As Object) ' Cerco nella tabella Key Dim Query As String = "SELECT * FROM " & DB_KEY & " LEFT JOIN " & DB_CLIENT & " ON " & DB_KEY & "." & DB_CLIENTID & " = " & DB_CLIENT & "." & DB_CLIENTID & " " Dim bFirstWhere As Boolean = True If Not String.IsNullOrWhiteSpace(Number) OrElse Not IsNothing(SelClient) OrElse Not IsNothing(SelIsDongle) OrElse Not IsNothing(SelState) OrElse Not IsNothing(KeyDate) OrElse Not IsNothing(Note) OrElse Not IsNothing(Seriale) OrElse Not String.IsNullOrWhiteSpace(LockID) Then Query &= "WHERE " If Not IsNothing(SelIsDongle) And (SelIsDongle = "Hardware" Or SelIsDongle = "Software") Then ' If Not IsNothing(SelIsDongle) Then EvalWhere(bFirstWhere, Query) Query &= DB_ISDONGLE & " = " If (GetSelIsDongle() = 1) Then Query &= 1 & " " Else Query &= 0 & " " End If If m_Number <> 0 Then EvalWhere(bFirstWhere, Query) Query &= DB_NUMBER & " LIKE '%" & Number & "%' " End If If Not String.IsNullOrWhiteSpace(LockID) Then EvalWhere(bFirstWhere, Query) Query &= DB_LOCKID & " LIKE '%" & LockID & "%' " End If If Not IsNothing(SelClient) Then If Not SelClient.Name.Equals("---ANY---") Then EvalWhere(bFirstWhere, Query) Query &= DB_NAME & " LIKE '" & SelClient.Name & "%' " End If End If If Not IsNothing(SelState) And Not SelState.Equals(Key.KeyState.ANY.ToString()) Then ' If Not IsNothing(SelState) Then EvalWhere(bFirstWhere, Query) Query &= DB_STATE & " LIKE '%" & SelState.ToString() & "' " End If If Not IsNothing(KeyDate) Then EvalWhere(bFirstWhere, Query) Query &= DB_DATE & " = '" & Format(KeyDate, "yyyy-MM-dd") & "%' " End If If Not String.IsNullOrWhiteSpace(Note) Then EvalWhere(bFirstWhere, Query) Query &= DB_NOTE & " LIKE '%" & Note.Replace("\", "\\").Replace("'", "\'").Replace("""", "\""").Replace("%", "\%") & "%' " End If If Not String.IsNullOrWhiteSpace(Seriale) Then EvalWhere(bFirstWhere, Query) Query &= DB_SERIALE & " LIKE '%" & Seriale & "%' " End If Query = Query.TrimEnd(","c, " "c) Dim suffixToRemove As String = " WHERE" If Not IsNothing(Query) And Not IsNothing(suffixToRemove) And Query.EndsWith(suffixToRemove) Then Query = Query.Substring(0, Query.Length - suffixToRemove.Length) End If End If Query &= " ORDER BY " & DB_NUMBER m_SearchResult = New ObservableCollection(Of SearchKey)(ManageDb.ExecuteSearchKeyQuery(Query)) If (m_SearchResult.Count = 0) Then MessageBox.Show("Nessun risultato per i filtri impostati") End If NotifyPropertyChanged("SearchResult") End Sub Private Sub EvalWhere(ByRef bFirst As Boolean, ByRef Query As String) If bFirst Then bFirst = False Else Query &= " AND " End If End Sub #End Region ' Search #Region "NewKey" ' Returns a command that manage the MainWindow_Unloaded command Public ReadOnly Property NewKey_Command As ICommand Get If m_cmdNewKey Is Nothing Then m_cmdNewKey = New Command(AddressOf NewKey) End If Return m_cmdNewKey End Get End Property Public Sub NewKey(ByVal param As Object) ' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. Map.refMainWindowVM.SelProjectMode = MainWindowVM.ProjectModeOpt.NEWKEY End Sub #End Region ' NewKey #Region "Cancel" ' Returns a command that manage the MainWindow_Unloaded command Public ReadOnly Property Cancel_Command As ICommand Get If m_cmdCancel Is Nothing Then m_cmdCancel = New Command(AddressOf Cancel) End If Return m_cmdCancel End Get End Property Public Sub Cancel(ByVal param As Object) ' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. Map.refMainWindowVM.SelProjectMode = MainWindowVM.ProjectModeOpt.MAINMENU End Sub #End Region ' Cancel #Region "Update" ' Returns a command that manage the MainWindow_Unloaded command Public ReadOnly Property UpdateKey_Command As ICommand Get If m_cmdUpdate Is Nothing Then m_cmdUpdate = New Command(AddressOf Update) End If Return m_cmdUpdate End Get End Property Public Sub Update(ByVal param As Object) If Not IsNothing(m_SelSearchResult) Then ' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. Map.refUpdateKeyPageVM.Key = m_SelSearchResult Map.refMainWindowVM.SelProjectMode = MainWindowVM.ProjectModeOpt.UPDATEKEY Else MessageBox.Show("Non è stata selezionata nessuna chiave") End If End Sub #End Region ' Update #Region "Delete" ' Returns a command that manage the MainWindow_Unloaded command Public ReadOnly Property DeleteKey_Command As ICommand Get If m_cmdDelete Is Nothing Then m_cmdDelete = New Command(AddressOf Delete) End If Return m_cmdDelete End Get End Property Public Sub Delete(ByVal param As Object) If Not IsNothing(m_SelSearchResult) Then ' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded. If MessageBox.Show("Do you really want to delete this Key?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then MsgBox("Operation cancelled") Exit Sub End If ' Cerco nella tabella Key Dim Query As String = "DELETE FROM " & DB_KEY & " WHERE " & DB_NUMBER & " = " & m_SelSearchResult.Number Dim Result As Integer = ExecuteQuery(Query) If Result <> 0 Then MsgBox("Operation executed") End If ' Map.refMainWindowVM.SelProjectMode = MainWindowVM.ProjectModeOpt.MAINMENU Search(param) Else MessageBox.Show("Non è stata selezionata nessuna chiave") End If End Sub #End Region ' Delete #End Region ' COMMANDS End Class Public Class SearchKey Inherits Key Private m_ClientName As String Public ReadOnly Property ClientName As String Get Return m_ClientName End Get End Property Sub New(ClientReader As MySqlDataReader) MyBase.New(ClientReader) 'm_ClientName = CType(ClientReader(DB_NAME), String) If (Not ClientReader(DB_NAME).Equals(DBNull.Value)) Then m_ClientName = CType(ClientReader(DB_NAME), String) Else m_ClientName = Nothing End If End Sub End Class