Files
Dario Sassi 49ba669707 LicenceManager :
- piccole correzioni
- aggiunta colonna scadenza licenza in SearchLicence.
2026-01-07 16:46:59 +01:00

1006 lines
39 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_ReturnFromUpdate As Boolean
Public Property ReturnFromUpdate As Boolean
Get
Return m_ReturnFromUpdate
End Get
Set(value As Boolean)
m_ReturnFromUpdate = value
End Set
End Property
Private m_LockId As String
Public Property LockId As String
Get
Return m_LockId
End Get
Set(value As String)
m_LockId = value
NotifyPropertyChanged("LockId")
End Set
End Property
Private m_SerialNumber As String
Public Property SerialNumber As String
Get
Return m_SerialNumber
End Get
Set(value As String)
m_SerialNumber = value
NotifyPropertyChanged("SerialNumber")
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
If Not IsNothing(m_SelVersion) Then
If m_SelVersion.VersionNumber < 22 Then
'Se la Versione è minore di 22 setto la CmBx a " " e la disabilito
SelSubVersion = m_SubVersionList(12)
SubVersion_IsEnabled = False
Else
'Altrimenti abilito la CmBx e la setto al mese corrente
SubVersion_IsEnabled = True
SelSubVersion = SubVersionList(DateTime.Now.Month - 1)
End If
End If
NotifyPropertyChanged("SelVersion")
End Set
End Property
Private m_SubVersionList As New List(Of String)({"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", String.Empty})
Public ReadOnly Property SubVersionList As List(Of String)
Get
Return m_SubVersionList
End Get
End Property
Private m_SelSubVersion As String
Public Property SelSubVersion As String
Get
Return m_SelSubVersion
End Get
Set(value As String)
m_SelSubVersion = value
NotifyPropertyChanged("SelSubVersion")
End Set
End Property
Private m_SubVersion_IsEnabled As Boolean
Public Property SubVersion_IsEnabled As Boolean
Get
Return m_SubVersion_IsEnabled
End Get
Set(value As Boolean)
m_SubVersion_IsEnabled = value
NotifyPropertyChanged("SubVersion_IsEnabled")
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
Private m_Note As String
Public 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_NumberList As List(Of Key)
Public ReadOnly Property NumberList As List(Of Key)
Get
Return m_NumberList
End Get
End Property
Private m_SelNumber As Key
Public Property SelNumber As Key
Get
Return m_SelNumber
End Get
Set(value As Key)
m_SelNumber = value
NotifyPropertyChanged("SelNumber")
If Not IsNothing(value) Then
' Quando viene scelto un KeyNumber viene modificato automaticamente anche il FilePath sostituendo il KeyNumber e
' calcolando il nome che il file dovrà avere senza o con '_' e con quale numero dopo)
Dim LicQuery As String = "SELECT * FROM " & DB_LICENCE & " WHERE " & DB_LICENCEID & " = " & Licence.LicenceID
Dim m_FilePathDict As Dictionary(Of String, String) = ManageDb.ExecuteStringDictionaryQuery(LicQuery)
Dim FilePath As String = m_FilePathDict(DB_FILE)
Dim KeyQuery As String = "SELECT * FROM " & DB_KEY & " WHERE " & DB_LOCKID & " = '" & Licence.Number & "' "
FilePath = FilePath.Replace("-" & ManageDb.ExecuteKeyQuery(KeyQuery)(0).Number.ToString("D6") & "-",
"-" & SelNumber.Number.ToString("D6") & "-")
' In base al tipo delle chiave (Hardware o Software) aggiusto il nome del file
If SelNumber.IsDongle = 0 Then
FilePath = ReplaceSubstring(FilePath, "Key", "Soft")
End If
If SelNumber.IsDongle = 1 Then
FilePath = ReplaceSubstring(FilePath, "Soft", "Key")
End If
m_FileName = ManageFile.ComputeFileName(FilePath, Path.GetFileNameWithoutExtension(FilePath))
NotifyPropertyChanged("FileName")
End If
End Set
End Property
Private m_KeyNumberTxBl_Visibility As Visibility
Public Property KeyNumberTxBl_Visibility As Visibility
Get
Return m_KeyNumberTxBl_Visibility
End Get
Set(value As Visibility)
m_KeyNumberTxBl_Visibility = value
NotifyPropertyChanged("KeyNumberTxBl_Visibility")
End Set
End Property
Private m_KeyNumberCmBx_Visibility As Visibility
Public Property KeyNumberCmBx_Visibility As Visibility
Get
Return m_KeyNumberCmBx_Visibility
End Get
Set(value As Visibility)
m_KeyNumberCmBx_Visibility = value
NotifyPropertyChanged("KeyNumberCmBx_Visibility")
End Set
End Property
Private m_NestKey As String
Public Overloads Property NestKey As String
Get
Return m_NestKey
End Get
Set(value As String)
m_NestKey = value
NotifyPropertyChanged("NestKey")
End Set
End Property
Private m_NestDeadline As Nullable(Of Date)
Public Property NestDeadline As Nullable(Of Date)
Get
Return m_NestDeadline
End Get
Set(value As Nullable(Of Date))
m_NestDeadline = 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 SubVersionMsg As String
Get
Return "Sub 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 NoteMsg As String
Get
Return "Note"
End Get
End Property
Public ReadOnly Property NestKeyMsg As String
Get
Return "Nesting encrypted string"
End Get
End Property
Public ReadOnly Property NestDeadlineMsg As String
Get
Return "Nesting deadline"
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
Public ReadOnly Property KeyNumberMsg As String
Get
Return "Key number"
End Get
End Property
Public ReadOnly Property LockIdMsg As String
Get
Return "Lock ID"
End Get
End Property
Public ReadOnly Property SerialNumberMsg As String
Get
Return "Serial number"
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 & " WHERE " & DB_LICENCEID & " = " & Licence.LicenceID
Dim m_VersNumberDict As Dictionary(Of String, String) = ManageDb.ExecuteStringDictionaryQuery(selQuery)
Dim VersNumber As String = m_VersNumberDict(DB_PRODUCTVERSION)
m_SelVersion = m_VersionList.FirstOrDefault(Function(vers) vers.VersionNumber.Equals(CInt(VersNumber.Substring(0, 2))))
NotifyPropertyChanged("SelVersion")
' Carico elemento della lista SubVersion selezionato precedentemente
If (VersNumber.Length > 2) Then
m_SelSubVersion = m_SubVersionList.FirstOrDefault(Function(vers) vers.Equals(VersNumber.Substring(2, 2)))
Else
m_SelSubVersion = m_SubVersionList(12) 'Imposto l'elemento String.Empty
End If
NotifyPropertyChanged("SelSubVersion")
If m_SelVersion.VersionNumber < 22 Then
SubVersion_IsEnabled = False
Else
SubVersion_IsEnabled = True
End If
SelProductLevel = Licence.ProductLevel
NotifyPropertyChanged("SelProductLevel")
FileName = Licence.File ' String.Empty
NotifyPropertyChanged("File")
' Carico LockId della Licenza selezionata da aggiornare
LockId = Licence.Number
NotifyPropertyChanged("LockId")
' Carico Seriale della Chiave che contiene la Licenza selezionata da aggiornare
Dim nQuery As String = "SELECT * FROM " & DB_KEY & " WHERE " & DB_LOCKID & " = '" & Licence.Number & "'"
'SerialNumber = ManageDb.ExecuteKeyQuery(nQuery).FirstOrDefault().Seriale
Dim keyDummy As Key = ManageDb.ExecuteKeyQuery(nQuery).FirstOrDefault()
SerialNumber = If(IsNothing(keyDummy), Nothing, keyDummy.Seriale)
NotifyPropertyChanged("SerialNumber")
' Carico ProductName della Licenza selezionata da aggiornare
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")
If Overwrite Then
KeyNumberTxBl_Visibility = Visibility.Collapsed
KeyNumberCmBx_Visibility = Visibility.Collapsed
Else
KeyNumberTxBl_Visibility = Visibility.Visible
KeyNumberCmBx_Visibility = Visibility.Visible
' Carico lista KeyNumber
Dim kQuery As String = "SELECT * FROM " & DB_KEY
m_NumberList = ManageDb.ExecuteKeyQuery(kQuery)
NotifyPropertyChanged("NumberList")
' Carico elemento della lista KeyNumber selezionato precedentemente
Dim selKQuery As String = "SELECT * FROM " & DB_LICENCE &
" INNER JOIN " & DB_KEY & " ON " & DB_LICENCE & "." & DB_LOCKID & " = " & DB_KEY & "." & DB_LOCKID &
" WHERE " & DB_LICENCEID & " = " & Licence.LicenceID
Dim selNum As Integer = ManageDb.ExecuteKeyQuery(selKQuery)(0).Number
SelNumber = m_NumberList.FirstOrDefault(Function(key) key.Number.Equals(selNum))
NotifyPropertyChanged("SelNumber")
End If
m_ProductDeadline = Licence.ProductDeadline
NotifyPropertyChanged("ProductDeadline")
m_OptionDeadline = Licence.OptionDeadline
NotifyPropertyChanged("OptionDeadline")
If Overwrite Then
m_LicenseDate = Licence.LicenseDate
Else
m_LicenseDate = DateTime.Now
End If
NotifyPropertyChanged("LicenseDate")
' Resetto i campi NestKey e NestDeadline e carico i valori grazie al LicenceID associato
m_NestKey = String.Empty
m_NestDeadline = Date.Now
Dim NestQuery As String = "SELECT * FROM " & DB_LICENCE & " WHERE " & DB_LICENCEID & " = " & Licence.LicenceID
Dim m_NestKeyDict As Dictionary(Of String, String) = ManageDb.ExecuteStringDictionaryQuery(NestQuery)
m_NestKey = m_NestKeyDict(DB_NESTKEY)
Dim sNestDl As String = m_NestKeyDict(DB_NESTDEADLINE)
Dim splittedNestDl() As String = Split(sNestDl)
If Not String.IsNullOrWhiteSpace(splittedNestDl(0)) Then
m_NestDeadline = Date.ParseExact(splittedNestDl(0), "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
End If
NotifyPropertyChanged("NestKey")
NotifyPropertyChanged("NestDeadline")
' Inizializzo liste opzioni
LoadOptions(1, m_Option1)
LoadOptions(2, m_Option2)
' m_FileName = Licence.File
m_Note = Licence.Note
NotifyPropertyChanged("Note")
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
Dim m_ProductNameDict As Dictionary(Of String, String) = ManageDb.ExecuteStringDictionaryQuery(Query)
m_ProductName = m_ProductNameDict(DB_PRODUCTNAME)
' 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
Private Function ReplaceSubstring(yourString As String, wordToRemove As String, newWord As String) As String
Dim wordIndex As Integer = yourString.IndexOf(wordToRemove)
If wordIndex >= 0 Then
yourString = yourString.Remove(wordIndex, wordToRemove.Length)
yourString = yourString.Insert(wordIndex, newWord)
End If
Return yourString
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 m_UpdateLicDict As Dictionary(Of String, String) = ManageDb.ExecuteStringDictionaryQuery(LicQuery)
Dim LockID As String = m_UpdateLicDict(DB_LOCKID)
Dim ProductID As Integer = CInt(m_UpdateLicDict(DB_PRODUCTID))
Dim FilePath As String = m_UpdateLicDict(DB_FILE)
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("Date=" & 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 & SelSubVersion)
StringFile.Add("Lev=" & SelProductLevel)
StringFile.Add("ExpDays=" & DayProductDeadline)
StringFile.Add("Opt1=" & nDecOption1)
StringFile.Add("Opt2=" & nDecOption2)
StringFile.Add("OptExpDays=" & DayOptionDeadline)
' Se il campo NestKey non è vuoto aggiungo anche quello al file
If Not String.IsNullOrWhiteSpace(NestKey) Then
StringFile.Add("NestKey=" & NestKey)
End If
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
Dim proc As Process = Nothing
Try
' Lancio il generatore di licenza
Dim sParams As String = """" & FilePath & """ 1"
proc = 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
Catch
If File.Exists(Map.refMainWindowVM.MainWindowM.sKeygenPath) Then
MessageBox.Show("Errore nell'esecuzione di KeyGenerator", "LicenceManager Error")
Else
MessageBox.Show("Il percorso di KeyGenerator specificato nel file INI non esiste", "LicenceManager Error")
End If
Return
End Try
' 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
Else
' Se la licenza è stata generata correttamente aggiungo la stringa NestKey (se prevista).
' L'aggiunta della string NestKey al file .lic è necessaria perchè viene sì scritta nel .Kge ma viene ignorata dal KeyGenerator
Try
Dim sLicFile As String = Path.ChangeExtension(FilePath.Replace("\", "\\"), ".lic")
If (File.Exists(sLicFile)) And Not String.IsNullOrWhiteSpace(NestKey) Then
My.Computer.FileSystem.WriteAllText(sLicFile, "NestKey=" & NestKey & vbNewLine, True, Text.Encoding.UTF8)
End If
Catch ex As Exception
MessageBox.Show("Errore nell'aggiunta della stringa criptata per il Nesting al file .lic", "LicenceManager Error")
Return
End Try
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.ToString()) 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) OrElse
Not String.IsNullOrWhiteSpace(LockID)) Then
Query &= " SET "
If Not IsNothing(m_SelVersion) Then
EvalWhere(bFirstWhere, Query)
Query &= DB_PRODUCTVERSION & " = '" & m_SelVersion.VersionNumber & m_SelSubVersion & "' "
End If
If Not IsNothing(m_SelProductLevel) Then
EvalWhere(bFirstWhere, Query)
Query &= DB_PRODUCTLEVEL & " = '" & m_SelProductLevel.ToString() & "' "
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(LockID) Then
EvalWhere(bFirstWhere, Query)
Query &= DB_LOCKID & " = '" & LockID & "' "
End If
If Not String.IsNullOrWhiteSpace(FileName) Then
EvalWhere(bFirstWhere, Query)
Query &= DB_FILE & " = '" & FileName.Replace("\", "\\") & "' "
End If
If Not String.IsNullOrWhiteSpace(textLic) Then
EvalWhere(bFirstWhere, Query)
Query &= DB_LICFILE & " = '" & textLic & "' "
End If
If Not IsNothing(Note) Then
EvalWhere(bFirstWhere, Query)
Query &= DB_NOTE & " = '" & m_Note.Replace("\", "\\").Replace("'", "\'").Replace("""", "\""").Replace("%", "\%") & "' "
End If
' Alla fine in ogni caso aggiorniamo anche NestKey e NestDeadline
EvalWhere(bFirstWhere, Query)
If String.IsNullOrWhiteSpace(NestKey) Then
Query &= DB_NESTKEY & " = NULL, " & DB_NESTDEADLINE & " = NULL "
Else
Query &= DB_NESTKEY & " = '" & m_NestKey & "', " & DB_NESTDEADLINE & " = '" & Format(NestDeadline, "yyyy-MM-dd") & "' "
End If
Query &= "WHERE " & DB_LICENCEID & " = " & Licence.LicenceID
Query = Query.TrimEnd(","c, " "c)
End If
Else
' Altrimenti siamo nel caso Update (New)
' 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 KeyQuery As String = "SELECT * FROM " & DB_KEY & " WHERE " & DB_LOCKID & " = '" & Licence.Number & "' " ' NON C'ERA
' Recupero il LockId dal DB in base al numero di chiave selezionato
Dim SelKeyQuery As String = "SELECT * FROM " & DB_KEY & " WHERE " & DB_NUMBER & " = '" & SelNumber.Number & "' "
Dim LockId As String = ManageDb.ExecuteKeyQuery(SelKeyQuery)(0).LockID
Dim ProductID As Integer = ManageDb.ExecuteIntegerQuery(LicQuery, DB_PRODUCTID)(0)
Dim FilePath As String = m_FileName
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("Date=" & 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 & SelSubVersion)
StringFile.Add("Lev=" & SelProductLevel)
StringFile.Add("ExpDays=" & DayProductDeadline)
StringFile.Add("Opt1=" & nDecOption1)
StringFile.Add("Opt2=" & nDecOption2)
StringFile.Add("OptExpDays=" & DayOptionDeadline)
' Se il campo NestKey non è vuoto aggiungo anche quello al file
If Not String.IsNullOrWhiteSpace(NestKey) Then
StringFile.Add("NestKey=" & NestKey)
End If
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
Dim proc As Process = Nothing
Try
' Lancio il generatore di licenza
Dim sParams As String = """" & FilePath & """ 1"
proc = 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
Catch
If File.Exists(Map.refMainWindowVM.MainWindowM.sKeygenPath) Then
MessageBox.Show("Errore nell'esecuzione di KeyGenerator", "LicenceManager Error")
Else
MessageBox.Show("Il percorso di KeyGenerator specificato nel file INI non esiste", "LicenceManager Error")
End If
Return
End Try
' 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
Else
' Se la licenza è stata generata correttamente: prima aggiungo la stringa NestKey (se prevista) poi, in ogni caso, aggiungo la licenza al DB.
' L'aggiunta della string NestKey al file .lic è necessaria perchè viene sì scritta nel .Kge ma viene ignorata dal KeyGenerator
Try
Dim sLicFile As String = Path.ChangeExtension(FilePath.Replace("\", "\\"), ".lic")
If (File.Exists(sLicFile)) And Not String.IsNullOrWhiteSpace(NestKey) Then
My.Computer.FileSystem.WriteAllText(sLicFile, "NestKey=" & NestKey & vbNewLine, True, Text.Encoding.UTF8)
End If
Catch ex As Exception
MessageBox.Show("Errore nell'aggiunta della stringa criptata per il Nesting al file .lic", "LicenceManager Error")
Return
End Try
' Ora aggiungo la licenza al DB
Try
Dim textLic As String = File.ReadAllText(Path.ChangeExtension(FilePath.Replace("\", "\\"), ".lic"))
If String.IsNullOrWhiteSpace(NestKey) Then
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 & ", " & DB_NOTE & ")" &
" VALUES ('" & ProductID & "', " &
"'" & m_SelVersion.VersionNumber & m_SelSubVersion & "', " &
"'" & 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 & "', " &
"'" & m_Note.Replace("\", "\\").Replace("'", "\'").Replace("""", "\""").Replace("%", "\%") & "')"
Else
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 & ", " & DB_NOTE & ", " &
DB_NESTKEY & ", " & DB_NESTDEADLINE & ")" &
" VALUES ('" & ProductID & "', " &
"'" & m_SelVersion.VersionNumber & m_SelSubVersion & "', " &
"'" & 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 & "', " &
"'" & m_Note.Replace("\", "\\").Replace("'", "\'").Replace("""", "\""").Replace("%", "\%") & "', " &
"'" & m_NestKey & "', " &
"Date('" & (String.Format("{0:yyyy-MM-dd}", NestDeadline)) & "')" & ")"
End If
Catch ex As Exception
MessageBox.Show("Errore nella lettura dei dati per la scrittura su DB", "LicenceManager Error")
Return
End Try
End If
End If
ManageDb.ExecuteQuery(Query)
If Map.refMainMenuVM.UpdateFromExpLic Then
Map.refMainMenuVM.UpdateFromExpLic = False
Map.refMainWindowVM.SelProjectMode = MainWindowVM.ProjectModeOpt.MAINMENU
Else
m_ReturnFromUpdate = True
' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
Map.refMainWindowVM.SelProjectMode = MainWindowVM.ProjectModeOpt.SEARCHLICENCE
End If
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)
If Map.refMainMenuVM.UpdateFromExpLic Then
Map.refMainMenuVM.UpdateFromExpLic = False
Map.refMainWindowVM.SelProjectMode = MainWindowVM.ProjectModeOpt.MAINMENU
Else
m_ReturnFromUpdate = True
' Manage the MainWindow_Unloaded event. This method is invoked by the cmdMainWindow_Unloaded.
Map.refMainWindowVM.SelProjectMode = MainWindowVM.ProjectModeOpt.SEARCHLICENCE
End If
End Sub
#End Region ' Cancel
End Class