Files
LicenceManager/UpdateLicencePage/UpdateLicencePageVM.vb
T
Renzo Lanza 08237de2d8 LicenceManager 2.1c6:
- In UpdateLicencePage viene mostrato il nome del Prodotto della Licenza che si sta aggiornando 
- Ogni pulsante "Close" fa ritornare alla pagina precedente (Correzione simile per i vari pulsanti Update e New, mentre Delete rimane nella stessa pagina)
- Correzione pulsanti del Main Menu (non hanno più la scritta "Cerca...")
- I DataGrid non tagliano più l'ultima riga visibile ma si aggiustano in base al numero intero di righe visibili (implementato per tutte le pagine Search tranne che per Product per via di errori che non riesco a risolvere
2019-03-25 08:58:31 +00:00

621 lines
23 KiB
VB.net

Imports System.Collections.ObjectModel
Imports System.IO
Imports EgtWPFLib5
Public Class UpdateLicencePageVM
Inherits VMBase
#Region "FIELDS & PROPERTIES"
Private m_Licence As Licence
Public Property Licence As Licence
Get
Return m_Licence
End Get
Set(value As Licence)
m_Licence = value
End Set
End Property
Private m_Overwrite As Boolean
Public Property Overwrite As Boolean
Get
Return m_Overwrite
End Get
Set(value As Boolean)
m_Overwrite = value
End Set
End Property
Private m_ProductName As String
Public Property ProductName As String
Get
Return m_ProductName
End Get
Set(value As String)
m_ProductName = value
NotifyPropertyChanged("ProductName")
End Set
End Property
Private m_VersionList As List(Of Version)
Public ReadOnly Property VersionList As List(Of Version)
Get
Return m_VersionList
End Get
End Property
Private m_SelVersion As Version
Public Property SelVersion As Version
Get
Return m_SelVersion
End Get
Set(value As Version)
m_SelVersion = value
NotifyPropertyChanged("SelVersion")
End Set
End Property
Private m_ProductLevelList As New List(Of Integer)({1, 5, 10})
Public ReadOnly Property ProductLevelList As List(Of Integer)
Get
Return m_ProductLevelList
End Get
End Property
Private m_SelProductLevel As Integer
Public Property SelProductLevel As Integer
Get
Return m_SelProductLevel
End Get
Set(value As Integer)
m_SelProductLevel = value
NotifyPropertyChanged("SelProductLevel")
End Set
End Property
Private m_ProductDeadline As Nullable(Of Date)
Public Property ProductDeadline As Nullable(Of Date)
Get
Return m_ProductDeadline
End Get
Set(value As Nullable(Of Date))
m_ProductDeadline = value
End Set
End Property
Private m_OptionDeadline As Nullable(Of Date)
Public Property OptionDeadline As Nullable(Of Date)
Get
Return m_OptionDeadline
End Get
Set(value As Nullable(Of Date))
m_OptionDeadline = value
End Set
End Property
Private m_FileName As String
Public Property FileName As String
Get
Return m_FileName
End Get
Set(value As String)
m_FileName = value
NotifyPropertyChanged("FileName")
End Set
End Property
Private m_Option1 As New ObservableCollection(Of KeyOption)
Public ReadOnly Property Option1 As ObservableCollection(Of KeyOption)
Get
Return m_Option1
End Get
End Property
Private m_Option2 As New ObservableCollection(Of KeyOption)
Public ReadOnly Property Option2 As ObservableCollection(Of KeyOption)
Get
Return m_Option2
End Get
End Property
Private m_LicenseDate As Nullable(Of Date)
Public Property LicenseDate As Nullable(Of Date)
Get
Return m_LicenseDate
End Get
Set(value As Nullable(Of Date))
m_LicenseDate = value
End Set
End Property
' Definizione comandi
Private m_cmdUpdate As Command
Private m_cmdCancel As Command
#End Region
#Region "Messages"
Public ReadOnly Property UpdateLicenceMsg As String
Get
Return "Update licence"
End Get
End Property
Public ReadOnly Property ProductNameMsg As String
Get
Return "Product name"
End Get
End Property
Public ReadOnly Property ProductVersionMsg As String
Get
Return "Product version"
End Get
End Property
Public ReadOnly Property ProductLevelMsg As String
Get
Return "Product level"
End Get
End Property
Public ReadOnly Property ProductDeadlineMsg As String
Get
Return "Product deadline"
End Get
End Property
Public ReadOnly Property Option1Msg As String
Get
Return "Option 1"
End Get
End Property
Public ReadOnly Property Option2Msg As String
Get
Return "Option 2"
End Get
End Property
Public ReadOnly Property OptionDeadlineMsg As String
Get
Return "Option deadline"
End Get
End Property
Public ReadOnly Property FileNameMsg As String
Get
Return "File name"
End Get
End Property
Public ReadOnly Property LicenseDateMsg As String
Get
Return "License date"
End Get
End Property
Public ReadOnly Property UpdateMsg As String
Get
Return "Update"
End Get
End Property
Public ReadOnly Property CancelMsg As String
Get
Return "Close"
End Get
End Property
#End Region ' Messages
#Region "CONSTRUCTOR"
Sub New()
' Imposto riferimento nella mappa
Map.SetRefUpdateLicencePageVM(Me)
End Sub
#End Region ' CONSTRUCTOR
#Region "METHODS"
Friend Sub InitUpdateLicencePage()
' Carico lista ProductVersion
Dim Query As String = "SELECT * FROM " & DB_VERSION & " WHERE " & DB_PRODUCTID & " = " & Licence.ProductID
m_VersionList = ManageDb.ExecuteVersionQuery(Query)
NotifyPropertyChanged("VersionList")
' Carico elemento della lista ProductVersion selezionato precedentemente
Dim selQuery As String = "SELECT * FROM " & DB_LICENCE &
" INNER JOIN " & DB_VERSION & " ON " & DB_LICENCE & "." & DB_PRODUCTID & " = " &
DB_VERSION & "." & DB_PRODUCTID & " AND " & DB_LICENCE & "." & DB_PRODUCTVERSION & " = " &
DB_VERSION & "." & DB_VERSIONNUMBER &
" WHERE " & DB_LICENCEID & " LIKE " & Licence.LicenceID
SelVersion = m_VersionList.FirstOrDefault(Function(vers) vers.VersionNumber.Equals(ManageDb.ExecuteVersionQuery(selQuery)(0).VersionNumber))
NotifyPropertyChanged("SelVersion")
SelProductLevel = Licence.ProductLevel
NotifyPropertyChanged("SelProductLevel")
FileName = Licence.File ' String.Empty
NotifyPropertyChanged("File")
Dim ProdQuery As String = "SELECT * FROM " & DB_PRODUCT & " WHERE " & DB_PRODUCTID & " = " & Licence.ProductID
ProductName = ManageDb.ExecuteProductQuery(ProdQuery)(0).ProductName
NotifyPropertyChanged("ProductName")
'' Carico elemento della lista Version selezionato precedentemente
'Dim selQuery As String = "SELECT " & DB_RESELLERID & " FROM " & DB_CLIENT &
'" INNER JOIN " & DB_RESELLER & " ON " & DB_CLIENT & "." & DB_CLIENTID & " = " &
'DB_RESELLER & "." & DB_RESELLERID &
'" WHERE " & DB_CLIENTID & " LIKE " & IdToUpdate
''m_SelReseller = ManageDb.ExecuteResellerQuery(rQuery)(0)
''m_SelReseller = m_ResellerList(2)
'm_SelVersion = m_VersionList.FirstOrDefault(Function(vers) vers.VersionNumber.Equals(ManageDb.ExecuteVersionQuery(selQuery)(0)))
'NotifyPropertyChanged("SelReseller")
' Carico lista ProductLevel
'Dim plQuery As String = "SELECT " & DB_PRODUCTLEVEL & " FROM " & DB_LICENCE & " GROUP BY " & DB_PRODUCTLEVEL
'm_ProductLevelList = ManageDb.ExecuteIntegerQuery(plQuery, DB_PRODUCTLEVEL)
'm_ProductLevelList.Sort(Function(x, y) CInt(y).CompareTo(CInt(x)))
'NotifyPropertyChanged("ProductLevelList")
m_ProductDeadline = Licence.ProductDeadline
NotifyPropertyChanged("ProductDeadline")
m_OptionDeadline = Licence.OptionDeadline
NotifyPropertyChanged("OptionDeadline")
m_LicenseDate = Licence.LicenseDate
NotifyPropertyChanged("LicenseDate")
' Inizializzo liste opzioni
LoadOptions(1, m_Option1)
LoadOptions(2, m_Option2)
' m_FileName = Licence.File
End Sub
Private Sub LoadOptions(nIndex As Integer, OptionList As ObservableCollection(Of KeyOption))
'Cerco ProductName associat a ProductID
Dim Query As String = "SELECT " & DB_PRODUCTNAME & " FROM " & DB_PRODUCT & " WHERE " & DB_PRODUCTID & " = " & Licence.ProductID
m_ProductName = ManageDb.ExecuteStringQuery(Query, DB_PRODUCTNAME)(0)
' Cancello opzioni
OptionList.Clear()
' Carico opzioni
Dim OptionIndex As Integer = 1
Dim OptionName As String = String.Empty
GetMainPrivateProfileString(m_ProductName & " - Option" & nIndex, "Option" & OptionIndex, "", OptionName)
While Not String.IsNullOrWhiteSpace(OptionName)
OptionList.Add(New KeyOption(False, True, OptionName))
OptionIndex += 1
GetMainPrivateProfileString(m_ProductName & " - Option" & nIndex, "Option" & OptionIndex, "", OptionName)
End While
' Se ho caricato delle opzioni
If OptionList.Count > 0 Then
' Verifico se ci sono opzioni del prodotto
Dim ProductOption As Integer = If(nIndex = 1, Licence.Option1, Licence.Option2)
Dim nBinaryIndex As Integer = 1
For I = 0 To OptionList.Count - 1
If (ProductOption And nBinaryIndex) <> 0 Then
OptionList(I).IsChecked = True
OptionList(I).IsEnabled = True
End If
If nIndex = 2 Then
If (OptionList(I).Msg.Equals("XXX")) Then
OptionList(I).IsChecked = False
OptionList(I).IsEnabled = False
End If
End If
nBinaryIndex *= 2
Next
End If
End Sub
Private Function CalcOptionDec(OptionList As ObservableCollection(Of KeyOption)) As Integer
Dim nDecOption As Integer = 0
Dim nBinaryIndex As Integer = 1
For I As Integer = 0 To OptionList.Count() - 1
If OptionList(I).IsChecked Then
nDecOption += nBinaryIndex
End If
nBinaryIndex *= 2
Next
Return nDecOption
End Function
#End Region ' METHODS
#Region "Update"
' Returns a command that manage the MainWindow_Unloaded command
Public ReadOnly Property Update_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)
Dim Query As String = String.Empty
If Overwrite Then
Dim LicQuery As String = "SELECT * FROM " & DB_LICENCE & " WHERE " & DB_LICENCEID & " = " & Licence.LicenceID
Dim LockID As String = ManageDb.ExecuteStringQuery(LicQuery, DB_LOCKID)(0)
Dim ProductID As Integer = ManageDb.ExecuteIntegerQuery(LicQuery, DB_PRODUCTID)(0)
Dim FilePath As String = ManageDb.ExecuteStringQuery(LicQuery, DB_FILE)(0)
Dim FileNameOnly As String = Path.GetFileNameWithoutExtension(FilePath)
Dim ProdQuery As String = "SELECT * FROM " & DB_PRODUCT & " WHERE " & DB_PRODUCTID & " = " & ProductID
Dim ProductNumber As Integer = ManageDb.ExecuteIntegerQuery(ProdQuery, DB_PRODUCTNUMBER)(0)
' Calcolo valore decimale opzione1
Dim nDecOption1 As Integer = CalcOptionDec(m_Option1)
Dim nDecOption2 As Integer = CalcOptionDec(m_Option2)
' Data
Dim startDate As New DateTime(1970, 1, 1)
Dim DayProductDeadline As Integer
DayProductDeadline = (ProductDeadline - startDate).Value.Days
Dim DayOptionDeadline As Integer
DayOptionDeadline = (OptionDeadline - startDate).Value.Days
' Nome licenza
Dim sLicName As String = Path.GetFileNameWithoutExtension(FilePath)
' Nome file
' Dim fileName As String = Map.refMainWindowVM.MainWindowM.sKeygenDataDir & "\" & sLicName & ".Kge"
' Scrivo il file
Dim StringFile As New List(Of String)
StringFile.Add(";")
StringFile.Add("[Index]")
StringFile.Add("Last=1")
StringFile.Add("[Licence1]")
StringFile.Add("Data=" & Format(Now, "yyyy/MM/dd HH:mm:ss"))
StringFile.Add("Customer=" & sLicName)
StringFile.Add("LockId=" & LockID)
StringFile.Add("ClearLockId=")
StringFile.Add("Product=" & ProductNumber)
StringFile.Add("Ver=" & SelVersion.VersionNumber)
StringFile.Add("Lev=" & SelProductLevel)
StringFile.Add("ExpDays=" & DayProductDeadline)
StringFile.Add("Opt1=" & nDecOption1)
StringFile.Add("Opt2=" & nDecOption2)
StringFile.Add("OptExpDays=" & DayOptionDeadline)
Try
IO.File.WriteAllLines(FilePath, StringFile, Text.Encoding.UTF8)
Catch ex As Exception
MessageBox.Show("Errore nella scrittura del file dati per licenza", "KeyGenerator Error")
Return
End Try
' Lancio il generatore di licenza
Dim sParams As String = FilePath & " 1"
Dim proc As Process = Process.Start(Map.refMainWindowVM.MainWindowM.sKeygenPath, sParams)
' Attendo il termine del processo
While Not proc.HasExited
proc.Refresh()
System.Threading.Thread.Sleep(50)
End While
' Errore nella generazione della licenza
If proc.ExitCode <> 0 Then
Dim sOut As String = "La Licenza non è stata generata correttamente (" & proc.ExitCode.ToString() & ")"
MessageBox.Show(sOut, "KeyGenerator Error")
Return
End If
Dim textLic As String = File.ReadAllText(Path.ChangeExtension(FilePath.Replace("\", "\\"), ".lic"))
'' Calcolo valore decimale opzione1
'Dim nDecOption1 As Integer = CalcOptionDec(m_Option1)
'Dim nDecOption2 As Integer = CalcOptionDec(m_Option2)
' Cerco nella tabella Licence
Query = "UPDATE " & DB_LICENCE
Dim bFirstWhere As Boolean = True
If Not IsNothing(m_SelVersion) OrElse
Not String.IsNullOrWhiteSpace(m_SelProductLevel) OrElse
Not IsNothing(ProductDeadline) OrElse
Not nDecOption1 = 0 OrElse
Not nDecOption2 = 0 OrElse
Not IsNothing(OptionDeadline) OrElse
Not IsNothing(LicenseDate) OrElse
Not String.IsNullOrWhiteSpace(FileName) Then ' (File)
Query &= " SET "
If Not IsNothing(m_SelVersion) Then
EvalWhere(bFirstWhere, Query)
Query &= DB_PRODUCTVERSION & " = '" & m_SelVersion.VersionNumber & "' "
End If
If Not IsNothing(m_SelProductLevel) Then
EvalWhere(bFirstWhere, Query)
Query &= DB_PRODUCTLEVEL & " = '" & m_SelProductLevel & "' "
End If
If Not IsNothing(ProductDeadline) Then
EvalWhere(bFirstWhere, Query)
Query &= DB_PRODUCTDEADLINE & " = '" & Format(ProductDeadline, "yyyy-MM-dd") & "' "
End If
If Not nDecOption1 = 0 Then
EvalWhere(bFirstWhere, Query)
Query &= DB_OPTION1 & " = '" & nDecOption1 & "' "
End If
If Not nDecOption2 = 0 Then
EvalWhere(bFirstWhere, Query)
Query &= DB_OPTION2 & " = '" & nDecOption2 & "' "
End If
If Not IsNothing(OptionDeadline) Then
EvalWhere(bFirstWhere, Query)
Query &= DB_OPTIONDEADLINE & " = '" & Format(OptionDeadline, "yyyy-MM-dd") & "' "
End If
If Not IsNothing(LicenseDate) Then
EvalWhere(bFirstWhere, Query)
Query &= DB_DATE & " = '" & Format(LicenseDate, "yyyy-MM-dd") & "' "
End If
'If Not String.IsNullOrWhiteSpace(File) Then
' EvalWhere(bFirstWhere, Query)
' Query &= DB_FILE & " = '" & File & "' "
'End If
If Not String.IsNullOrWhiteSpace(textLic) Then
EvalWhere(bFirstWhere, Query)
Query &= DB_LICFILE & " = '" & textLic & "' "
End If
Query &= "WHERE " & DB_LICENCEID & " = " & Licence.LicenceID
Query = Query.TrimEnd(","c, " "c)
End If
Else
' Verifico presenza di tutti i dati
If IsNothing(m_Option1) OrElse
IsNothing(m_Option2) OrElse
IsNothing(m_SelVersion) OrElse
IsNothing(m_SelProductLevel) OrElse
IsNothing(m_ProductDeadline) OrElse
IsNothing(m_OptionDeadline) OrElse
IsNothing(m_LicenseDate) Then
MessageBox.Show("Completare i campi presenti")
Return
End If
Dim LicQuery As String = "SELECT * FROM " & DB_LICENCE & " WHERE " & DB_LICENCEID & " = " & Licence.LicenceID
Dim LockID As String = ManageDb.ExecuteStringQuery(LicQuery, DB_LOCKID)(0)
Dim ProductID As Integer = ManageDb.ExecuteIntegerQuery(LicQuery, DB_PRODUCTID)(0)
Dim FilePath As String = ManageDb.ExecuteStringQuery(LicQuery, DB_FILE)(0)
Dim FileNameOnly As String = Path.GetFileNameWithoutExtension(FilePath)
Dim fileNumber As Integer = 1
If FileNameOnly.Contains("_") Then
Do
If IO.File.Exists(Path.GetDirectoryName(FilePath) & "\\" & FileNameOnly.Split("_")(0) & "_" & fileNumber.ToString & ".Kge") Then
'The file does exist, so increment and try the next one
fileNumber = fileNumber + 1
Else
'The file does not exist, do something..
FilePath = FilePath.Replace((fileNumber - 1).ToString & ".Kge", "") & fileNumber.ToString & ".Kge"
Exit Do
End If
Loop
Else
FilePath = FilePath.Replace(".Kge", "") & "_1.Kge"
End If
Dim ProdQuery As String = "SELECT * FROM " & DB_PRODUCT & " WHERE " & DB_PRODUCTID & " = " & ProductID
Dim ProductNumber As Integer = ManageDb.ExecuteIntegerQuery(ProdQuery, DB_PRODUCTNUMBER)(0)
' Calcolo valore decimale opzione1
Dim nDecOption1 As Integer = CalcOptionDec(m_Option1)
Dim nDecOption2 As Integer = CalcOptionDec(m_Option2)
' Data
Dim startDate As New DateTime(1970, 1, 1)
Dim DayProductDeadline As Integer
DayProductDeadline = (ProductDeadline - startDate).Value.Days
Dim DayOptionDeadline As Integer
DayOptionDeadline = (OptionDeadline - startDate).Value.Days
' Nome licenza
Dim sLicName As String = Path.GetFileNameWithoutExtension(FilePath)
' Nome file
' Dim fileName As String = Map.refMainWindowVM.MainWindowM.sKeygenDataDir & "\" & sLicName & ".Kge"
' Scrivo il file
Dim StringFile As New List(Of String)
StringFile.Add(";")
StringFile.Add("[Index]")
StringFile.Add("Last=1")
StringFile.Add("[Licence1]")
StringFile.Add("Data=" & Format(Now, "yyyy/MM/dd HH:mm:ss"))
StringFile.Add("Customer=" & sLicName)
StringFile.Add("LockId=" & LockID)
StringFile.Add("ClearLockId=")
StringFile.Add("Product=" & ProductNumber)
StringFile.Add("Ver=" & SelVersion.VersionNumber)
StringFile.Add("Lev=" & SelProductLevel)
StringFile.Add("ExpDays=" & DayProductDeadline)
StringFile.Add("Opt1=" & nDecOption1)
StringFile.Add("Opt2=" & nDecOption2)
StringFile.Add("OptExpDays=" & DayOptionDeadline)
Try
IO.File.WriteAllLines(FilePath, StringFile, Text.Encoding.UTF8)
Catch ex As Exception
MessageBox.Show("Errore nella scrittura del file dati per licenza", "KeyGenerator Error")
Return
End Try
' Lancio il generatore di licenza
Dim sParams As String = FilePath & " 1"
Dim proc As Process = Process.Start(Map.refMainWindowVM.MainWindowM.sKeygenPath, sParams)
' Attendo il termine del processo
While Not proc.HasExited
proc.Refresh()
System.Threading.Thread.Sleep(50)
End While
' Errore nella generazione della licenza
If proc.ExitCode <> 0 Then
Dim sOut As String = "La Licenza non è stata generata correttamente (" & proc.ExitCode.ToString() & ")"
MessageBox.Show(sOut, "KeyGenerator Error")
Return
End If
Dim textLic As String = File.ReadAllText(Path.ChangeExtension(FilePath.Replace("\", "\\"), ".lic"))
' Aggiungo una licenza al Db
Query = "INSERT INTO " & DB_LICENCE & " (" & DB_PRODUCTID & ", " & DB_PRODUCTVERSION & ", " & DB_PRODUCTLEVEL & ", " &
DB_PRODUCTDEADLINE & ", " & DB_OPTION1 & ", " & DB_OPTION2 & ", " &
DB_OPTIONDEADLINE & ", " & DB_LOCKID & ", " & DB_FILE & ", " &
DB_DATE & ", " & DB_LICFILE & ")" &
" VALUES ('" & ProductID & "', " &
"'" & m_SelVersion.VersionNumber & "', " &
"'" & m_SelProductLevel & "', " &
"Date('" & (String.Format("{0:yyyy-MM-dd}", ProductDeadline)) & "'), " &
"'" & nDecOption1 & "', " &
"'" & nDecOption2 & "', " &
"Date('" & (String.Format("{0:yyyy-MM-dd}", OptionDeadline)) & "'), " &
"'" & LockID & "', " &
"'" & FilePath.Replace("\", "\\") & "', " &
"'" & Format(LicenseDate, "yyyy-MM-dd") & "', " &
"'" & textLic & "')"
End If
ManageDb.ExecuteQuery(Query)
' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
Map.refMainWindowVM.SelProjectMode = MainWindowVM.ProjectModeOpt.SEARCHLICENCE
End Sub
Private Sub EvalWhere(ByRef bFirst As Boolean, ByRef Query As String)
If bFirst Then
bFirst = False
Else
Query &= ", "
End If
End Sub
#End Region ' Search
#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.SEARCHLICENCE
End Sub
#End Region ' Cancel
End Class