ToolCollection/ToolCollection/ClassMoReKonfiguration.vb
Jonathan Jenne 688f53a74a jj 31.05
2017-05-31 12:17:35 +02:00

518 lines
18 KiB
VB.net

Public Class ClassMoReKonfiguration
Private _originalprofil As String
Private _originalDbArt As String
Private _originalDataSource As String
Private _originalUserId As String
Private _originalPassword As String
Private _originalInitialCatalog As String
Private _profil As String
Private _dbArt As String
Private _dataSource As String
Private _userId As String
Private _password As String
Private _initialCatalog As String
Public Shared xmlConfigFile As String
Private Shared _profile() As ClassMoReKonfiguration
Private _saved As Boolean
''' <remarks></remarks>
Sub New(ByVal vprofil As String, ByVal vdbArt As String, ByVal vdataSource As String, ByVal vuserId As String, _
ByVal vpassword As String, ByVal vinitialCatalog As String)
_profil = vprofil
Me._dbArt = vdbArt
Me._dataSource = vdataSource
Me._userId = vuserId
Me._password = vpassword
Me._initialCatalog = vinitialCatalog
_originalprofil = vprofil
Me._originalDbArt = vdbArt
Me._originalDataSource = vdataSource
Me._originalUserId = vuserId
Me._originalPassword = vpassword
Me._originalInitialCatalog = vinitialCatalog
Me._saved = True
End Sub
Public Shared Sub Init()
xmlConfigFile = My.Application.Info.DirectoryPath & "\" & My.Settings.vMoReBasConfigDatei
LoadFromXmlFile()
End Sub
Public Shared Sub LoadFromXmlFile()
Dim i As Integer = 0
Dim xml As Xml.XmlDocument
_profile = Nothing
If xmlConfigFile Is Nothing Then Exit Sub
If Not xmlConfigFile = "" Then
If IsXmlAccessable() Then
xml = New Xml.XmlDocument()
xml.Load(xmlConfigFile)
' Anzahl der Profilelemente in der XML-Datei zählen
Dim anzProfile = xml.SelectNodes("Settings/Profile/Profil").Count
' wenn mindestens ein Profilelement eingetragen ist
If anzProfile > 0 Then
' alle Profile laden
Dim xmlNodes As Xml.XmlNodeList = xml.SelectNodes("Settings/Profile/Profil")
' alle Profile der XML-Datei durchlaufen
For Each xmlProfil As Xml.XmlNode In xmlNodes
' prüfen ob ein Profilname angegeben wurde
If xmlProfil.Attributes("profil") IsNot Nothing And xmlProfil.Attributes("dbArt") IsNot Nothing And _
xmlProfil.Attributes("dataSource") IsNot Nothing Then
' ein Element an Items anhängen
ReDim Preserve _profile(i)
_profile(i) = New ClassMoReKonfiguration()
Dim initialCatalog As String = Nothing
Dim userId As String = Nothing
Dim password As String = Nothing
If xmlProfil.Attributes("userId") IsNot Nothing Then
userId = xmlProfil.Attributes("userId").Value
End If
If xmlProfil.Attributes("password") IsNot Nothing Then
password = xmlProfil.Attributes("password").Value
End If
If xmlProfil.Attributes("initialCatalog") IsNot Nothing Then
initialCatalog = xmlProfil.Attributes("initialCatalog").Value
End If
' das Profilobjekt erstellen
_profile(i) = New ClassMoReKonfiguration( _
xmlProfil.Attributes("profil").Value, _
xmlProfil.Attributes("dbArt").Value, _
xmlProfil.Attributes("dataSource").Value, _
userId, _
password, _
initialCatalog)
' Zähler für Profile erhöhen
i += 1
End If
Next
End If
End If
End If
End Sub
Public Shared Function IsXmlAccessable()
Dim xml As New Xml.XmlDocument
Try
' XML-Datei laden und speichern um Zugriff zu testen
xml.Load(xmlConfigFile)
xml.Save(xmlConfigFile)
Return True
Catch ex As Exception
Try
Dim fw As System.IO.StreamWriter = New System.IO.StreamWriter(xmlConfigFile)
fw.WriteLine("<?xml version=""1.0"" encoding=""ISO-8859-1"" standalone=""yes""?>")
fw.WriteLine("<Settings>")
fw.WriteLine("<Profile>")
fw.WriteLine("</Profile>")
fw.WriteLine("</Settings>")
fw.Close()
Return True
Catch e2 As Exception
Return False
End Try
End Try
End Function
' erstellt ein neues Profil
Public Shared Sub Add(ByVal vprofil As String, ByVal vdbArt As String, ByVal vdataSource As String, ByVal vuserId As String, _
ByVal vpassword As String, ByVal vinitialCatalog As String)
'Try
If Not ClassMoReKonfiguration.Exists(vprofil) Then
MsgBox("Gibts noch nicht!")
Dim xml As New Xml.XmlDocument
Dim xmlProfil As Xml.XmlElement
If ClassMoReKonfiguration.IsXmlAccessable() Then
' XML-Datei laden
xml.Load(xmlConfigFile)
Try
' versuchen die XML-Beschreibung zu schreiben
xml.InsertBefore(xml.CreateXmlDeclaration("1.0", "ISO-8859-1", "yes"), xml.ChildNodes(0))
Catch ex As Exception
' wenn dies nicht funktioniert, ist die Beschreibung schon vorhanden
End Try
' wenn die Hauptknoten noch nicht eingetragen wurden, sollen diese angelegt werden
If xml.DocumentElement Is Nothing Then xml.AppendChild(xml.CreateElement("Settings"))
If xml.SelectSingleNode("Settings/Profile") Is Nothing Then xml.SelectSingleNode("Settings").AppendChild(xml.CreateElement("Profile"))
' dann ein neues Profil anlegen
xmlProfil = xml.CreateElement("Profil")
' dem Profil die Attribute zuweisen
xmlProfil.SetAttribute("profil", vprofil)
xmlProfil.SetAttribute("dbArt", vdbArt)
xmlProfil.SetAttribute("dataSource", vdataSource)
'xmlProfil.SetAttribute("userID", userId)
'xmlProfil.SetAttribute("password", password)
'xmlProfil.SetAttribute("initialCatalog", initialCatalog)
If vuserId IsNot Nothing Then
xmlProfil.SetAttribute("userId", vuserId)
End If
If vpassword IsNot Nothing Then
xmlProfil.SetAttribute("password", vpassword)
End If
If vinitialCatalog IsNot Nothing Then
xmlProfil.SetAttribute("initialCatalog", vinitialCatalog)
End If
' und in den Hauptknoten 'Profile' einhängen
xml.SelectSingleNode("Settings/Profile").AppendChild(xmlProfil)
' XML-Datei speichern
xml.Save(xmlConfigFile)
' die Liste der Profile im Objekt vergrößern
ReDim Preserve ClassMoReKonfiguration._profile(ClassMoReKonfiguration.Count)
' das Profilobjekt erstellen
ClassMoReKonfiguration._profile(ClassMoReKonfiguration.Count - 1) = New ClassMoReKonfiguration( _
vprofil, _
vdbArt, _
vdataSource, _
vuserId, _
vpassword, _
vinitialCatalog)
MsgBox("Das Profil '" & vprofil & "' wurde erfolgreich angelegt!", MsgBoxStyle.Information, "Erfolgsmeldung:")
End If
End If
'Catch ex As Exception
' MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Anlegen eines Profils")
'End Try
End Sub
Public Shared ReadOnly Property Count() As Integer
Get
' Anzahl der Profile zurückgeben
If _profile IsNot Nothing Then
Return _profile.Length
Else
Return 0
End If
End Get
End Property
Public Shared Function Exists(ByVal profilname As String)
If ClassMoReKonfiguration.Count > 0 Then
For Each profil As ClassMoReKonfiguration In _profile
If profil.OriginalProfil = profilname Then
Return True
End If
Next
End If
Return False
End Function
'' gibt ein einzelnes Profil-Objekt zurück
Public Shared Function getProfilByName(ByVal profilname As String) As ClassMoReKonfiguration
If _profile Is Nothing Then
Return Nothing
Else
'alle Profile durchlaufen
For Each profil As ClassMoReKonfiguration In _profile
' wenn der aktuelle Profilname mit dem gesuchten Profilnamen übereinstimmt
If profil.Profil = profilname Then
' das Profil zurückgeben
Return profil
End If
Next
End If
End Function
''' <summary>
''' Konstruktor ohne Tätigkeit
''' </summary>
''' <remarks>wenn im Konstruktor kein Parameter übergeben wurde soll auch nichts gemacht werden</remarks>
Sub New()
End Sub
''' <summary>
''' Speichert das Profil
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Function Save()
'Try
' wenn das Profil nicht gespeichert ist
If Not Me.IsSaved Then
' wenn ein Profilname für dieses Profil gesetzt ist
If Me._profil IsNot Nothing And Me._originalprofil IsNot Nothing Then
' XML-Datei initialisieren
Dim xml As New Xml.XmlDocument()
' Profil initialisieren
Dim xmlProfil As Xml.XmlElement
' XML-Datei öffnen und laden
xml.Load(My.Settings.vMoReBasConfigDatei)
' den XML-Knoten des Profils laden
xmlProfil = xml.SelectSingleNode("//Settings/Profile/Profil[@profil='" & Me._originalprofil & "']")
' wenn der Knoten NICHT gefunden wurde -> Vorgang abbrechen
If xmlProfil Is Nothing Then Return False
' die neuen Werte den Originalwerten zuweisen
Me._originalprofil = Me._profil
Me._originalDbArt = Me._dbArt
Me._originalDataSource = Me._dataSource
Me._originalUserId = Me._userId
Me._originalPassword = Me._password
' die neuen Werte in die XML-Datei schreiben
xmlProfil.Attributes("profil").Value = Me._profil
xmlProfil.Attributes("dbArt").Value = Me._dbArt
xmlProfil.Attributes("dataSource").Value = Me._dataSource
xmlProfil.Attributes("initialCatalog").Value = Me._initialCatalog
If Me._userId IsNot Nothing Then
xmlProfil.Attributes("userId").Value = Me._userId
If Me._password IsNot Nothing Then
xmlProfil.Attributes("password").Value = Me._password
End If
End If
If Me._userId IsNot Nothing Then
End If
' XML-Datei speichern
xml.Save(My.Settings.vMoReBasConfigDatei)
' Profil auf gespeichert setzen
Me._saved = True
MsgBox("Das Profil '" & Me._originalprofil & "' wurde erfolgreich gespeichert.", MsgBoxStyle.Information, "Profil gespeichert")
Return True
Else
MsgBox("Es wurde kein Profilname für das Profil definiert.", MsgBoxStyle.Information, "Profil konnte nicht gespeichert werden")
Return False
End If
End If
Return False
'Catch ex As Exception
' MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Speichern eines Profils")
' Return False
'End Try
End Function
''' <summary>
''' Setzt im aktuellen Profil alle Einstellungen auf die Originalwerte zurück.
''' </summary>
''' <remarks></remarks>
Public Sub SetBackToOriginal()
Me._profil = Me._originalprofil
Me._dbArt = Me._originalDbArt
Me._dataSource = Me._originalDataSource
Me._initialCatalog = Me._originalInitialCatalog
Me._userId = Me._originalUserId
Me._password = Me._originalPassword
Me._originalprofil = Me._profil
End Sub
''' <summary>
''' Setzt das Profil auf geändert.
''' </summary>
''' <remarks></remarks>
Public Sub setChanged()
' Profil auf geändert setzen
Me._saved = False
End Sub
''' <summary>
''' Setzt das Profil auf gespeichert.
''' </summary>
''' <remarks></remarks>
Public Sub setSaved()
' Profil auf gespeichert setzen
Me._saved = True
End Sub
Public ReadOnly Property HasChanges() As Boolean
Get
If Me._profil = Me._originalprofil And _
Me._dbArt = Me._originalDbArt And _
Me._dataSource = Me._originalDataSource And _
Me._userId = Me._originalUserId And _
Me._password = Me._originalPassword Then
Return False
Else
Return True
End If
End Get
End Property
Public ReadOnly Property IsSaved() As Boolean
Get
Return Me._saved
End Get
End Property
Public Property OriginalProfil() As String
Get
Return Me._originalprofil
End Get
Set(ByVal value As String)
Me._originalprofil = value
End Set
End Property
Public Property Profil() As String
Get
Return Me._profil
End Get
Set(ByVal value As String)
Me._profil = value
' Profil auf geändert setzen
Me.setChanged()
End Set
End Property
Public Property OriginalDbArt() As String
Get
Return Me._originalDbArt
End Get
Set(ByVal value As String)
Me._originalDbArt = value
End Set
End Property
Public Property DbArt() As String
Get
Return Me._dbArt
End Get
Set(ByVal value As String)
Me._dbArt = value
Me.setChanged()
End Set
End Property
Public Property OriginalDataSource() As String
Get
Return Me._originalDataSource
End Get
Set(ByVal value As String)
Me._originalDataSource = value
End Set
End Property
Public Property DataSource() As String
Get
Return Me._dataSource
End Get
Set(ByVal value As String)
Me._dataSource = value
Me.setChanged()
End Set
End Property
Public Property OriginalUserId() As String
Get
Return Me._originalUserId
End Get
Set(ByVal value As String)
Me._originalUserId = value
End Set
End Property
Public Property UserId() As String
Get
Return Me._userId
End Get
Set(ByVal value As String)
Me._userId = value
Me.setChanged()
End Set
End Property
Public Property OriginalPassword() As String
Get
Return Me._originalPassword
End Get
Set(ByVal value As String)
Me._originalPassword = value
End Set
End Property
Public Property Password() As String
Get
Return Me._password
End Get
Set(ByVal value As String)
Me._password = value
Me.setChanged()
End Set
End Property
Public Property OriginalInitalCatalog() As String
Get
Return Me._originalInitialCatalog
End Get
Set(ByVal value As String)
Me._originalInitialCatalog = value
End Set
End Property
Public Property InitialCatalog() As String
Get
Return Me._initialCatalog
End Get
Set(ByVal value As String)
Me._initialCatalog = value
Me.setChanged()
End Set
End Property
End Class