ToolCollection/ToolCollection/ClassDIProfile.vb
Digital Data - Marlon Schreiber 3a25947af0 MS
2019-02-05 18:49:13 +01:00

336 lines
16 KiB
VB.net

Public Class ClassDIProfile
Private Shared _profiles() As ClassDIProfil
'Private Shared _parentClass As ClassDateiimport = ClassDateiimport.GetInstance
Public Shared xmlConfigFile As String
Public Shared aktivesProfil As ClassDIProfil
Public Shared aktivesProfilAusProfileigenschaften As Boolean = False
Public Shared _windream As New ClassWindream_allgemein(CURR_MyLogger)
Public Shared Sub Init()
xmlConfigFile = My.Application.Info.DirectoryPath & "\" & My.Settings.vDIConfigDatei
ClassDIProfile.LoadFromXmlFile()
' 3b. Windream initialisieren (Connection, Session, ... aufbauen)
_windream.Init()
End Sub
Public Shared Sub LoadFromXmlFile()
Dim i As Integer = 0
Dim xml As Xml.XmlDocument
ClassDIProfile._profiles = Nothing
If xmlConfigFile Is Nothing Then Exit Sub
If Not xmlConfigFile = "" Then
If ClassDIProfile.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("profilname") IsNot Nothing And xmlProfil.Attributes("dokumenttyp") IsNot Nothing Then
' ein Element an Items anhängen
ReDim Preserve ClassDIProfile._profiles(i)
' das Profilobjekt erzeugen
ClassDIProfile._profiles(i) = New ClassDIProfil(xmlProfil.Attributes("profilname").Value,
xmlProfil.Attributes("aktiv").Value,
xmlProfil.Attributes("verzoegerung").Value,
_windream.GetObjecttypeByName(xmlProfil.Attributes("dokumenttyp").Value, False),
xmlProfil.Attributes("quellordner").Value,
xmlProfil.Attributes("subdirectories").Value,
xmlProfil.Attributes("zielordner").Value,
xmlProfil.Attributes("backup").Value,
xmlProfil.Attributes("backupordner").Value,
xmlProfil.Attributes("overwrite").Value,
xmlProfil.Attributes("datei_loeschen").Value,
xmlProfil.Attributes("Date_UV").Value,
xmlProfil.Attributes("verzeichnis_loeschen").Value,
xmlProfil.Attributes("intervall").Value,
xmlProfil.Attributes("lastrun").Value,
xmlProfil.Attributes("link2navision").Value,
xmlProfil.Attributes("link2navision_dokart").Value,
xmlProfil.Attributes("link2navision_index").Value,
xmlProfil.Attributes("subdirectoriesloeschen").Value,
xmlProfil.Attributes("dokumenttyp").Value)
' Zähler für Profile erhöhen
i += 1
End If
Next
End If
End If
End If
End Sub
' gibt ein einzelnes Profil-Objekt zurück
Public Shared Function getProfilByName(ByVal profilname As String) As ClassDIProfil
' alle Profile durchlaufen
For Each profil As ClassDIProfil In _profiles
' wenn der aktuelle Profilname mit dem gesuchten Profilnamen übereinstimmt
If profil.Profilname = profilname Then
' das Profil zurückgeben
Return profil
End If
Next
' sonst nichts zurückgeben
Return Nothing
End Function
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
If My.Settings.vLogErrorsonly = False Then MsgBox("System kann nicht auf Konfig-Datei " & vbNewLine & xmlConfigFile & vbNewLine & " zugreifen! (Nicht vorhanden bzw Syntaxfehler)" & vbNewLine & "Datei wird neu erstellt!", MsgBoxStyle.Exclamation, "Achtung:")
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
Public Shared Function Profilname_Exists(ByVal profilname As String)
If ClassDIProfile.Count > 0 Then
For Each profil As ClassDIProfil In ClassDIProfile._profiles
If profil.OriginalProfilname = profilname Then
Return True
End If
Next
End If
Return False
End Function
' erstellt ein neues Profil
Public Shared Sub Add(ByVal profilname As String, ByVal dokumenttyp As String, ByVal quellordner As String, ByVal subdirectories As Boolean, _
ByVal zielordner As String, ByVal backup As Boolean, ByVal backupordner As String, _
ByVal overwrite As Boolean, ByVal datei_loeschen As Boolean, ByVal Date_UV As Boolean, ByVal verzeichnis_loeschen As Boolean, ByVal intervall As Integer, ByVal link2Navision As Boolean, ByVal link2Navision_dokart As String, ByVal link2Navision_Index As String, ByVal UVloeschen As Boolean)
Try
If Not ClassDIProfile.Profilname_Exists(profilname) Then
Dim xml As New Xml.XmlDocument
Dim xmlProfil As Xml.XmlElement
If ClassDIProfile.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.SelectNodes("Settings/Profile") Is Nothing Then xml.AppendChild(xml.CreateElement("Profile"))
' dann ein neues Profil anlegen
xmlProfil = xml.CreateElement("Profil")
' dem Profil die Attribute zuweisen
xmlProfil.SetAttribute("profilname", profilname)
xmlProfil.SetAttribute("aktiv", "True")
xmlProfil.SetAttribute("verzoegerung", 0)
xmlProfil.SetAttribute("dokumenttyp", dokumenttyp)
xmlProfil.SetAttribute("quellordner", quellordner)
xmlProfil.SetAttribute("zielordner", zielordner)
xmlProfil.SetAttribute("subdirectories", subdirectories)
xmlProfil.SetAttribute("subdirectoriesloeschen", UVloeschen)
xmlProfil.SetAttribute("backup", backup)
xmlProfil.SetAttribute("backupordner", backupordner)
xmlProfil.SetAttribute("overwrite", overwrite)
xmlProfil.SetAttribute("datei_loeschen", datei_loeschen)
xmlProfil.SetAttribute("Date_UV", Date_UV)
xmlProfil.SetAttribute("verzeichnis_loeschen", verzeichnis_loeschen)
xmlProfil.SetAttribute("intervall", intervall)
xmlProfil.SetAttribute("lastrun", "01.01.1900")
xmlProfil.SetAttribute("link2navision", link2Navision)
xmlProfil.SetAttribute("link2navision_dokart", link2Navision_dokart)
xmlProfil.SetAttribute("link2navision_index", link2Navision_Index)
' 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 ClassDIProfile._profiles(ClassDIProfile.Count)
' und das neue Profil dem Profilarray anhängen und die Eigenschaften zuweisen
ClassDIProfile._profiles(ClassDIProfile.Count - 1) = New ClassDIProfil(profilname,
True,
0,
_windream.GetObjecttypeByName(dokumenttyp, False),
quellordner,
subdirectories,
zielordner,
backup,
backupordner,
overwrite,
datei_loeschen,
Date_UV,
verzeichnis_loeschen,
intervall,
"01.01.1900", link2Navision, link2Navision_dokart, link2Navision_Index, UVloeschen)
End If
MsgBox("Das Profil: '" & profilname & "' wurde erfolgreich gespeichert!", MsgBoxStyle.Information, "Erfolgsmeldung:")
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Anlegen eines Profils:")
End Try
End Sub
Public Shared Function DeleteProfil(ByVal profilname As String) As Boolean
Try
If ClassDIProfile.Profilname_Exists(profilname) Then
' XML-Objekt initialisieren
Dim xml As New Xml.XmlDocument
' XML-Datei laden
xml.Load(xmlConfigFile)
' ***** Zuerst einmal das Profil heraus löschen ***** '
' XML-Element auswählen
Dim node As Xml.XmlElement = xml.SelectSingleNode("Settings/Profile/Profil[@profilname='" & profilname & "']")
If node IsNot Nothing Then
' Element löschen
node.ParentNode.RemoveChild(node)
' XML-Datei speichern
xml.Save(xmlConfigFile)
End If
' ***** Und im Anschluss das Profil aus allen Benutzern löschen ***** '
'' alle ProfilLinks durchlaufen und Profileintrag entfernen
Dim xmlProfile As Xml.XmlNodeList = xml.SelectSingleNode("//Settings/Profile").ChildNodes
For Each profil As Xml.XmlNode In xmlProfile
Dim xmlProfilLinks As Xml.XmlNodeList = profil.ChildNodes
For Each link As Xml.XmlNode In xmlProfilLinks
If link.Attributes("profilname").Value = profilname Then
link.RemoveChild(link)
xml.Save(xmlConfigFile)
End If
Next
Next
' *** Array umschreiben (Element aus Array entfernen) ***
If ClassDIProfile.Profilname_Exists(profilname) Then
' temporäres Array erzeugen
Dim tempItems() As ClassDIProfil = ClassDIProfile._profiles
ClassDIProfile._profiles = Nothing
Dim i As Integer = 0
' alle Verknüpfungen durchlaufen
For Each item As ClassDIProfil In tempItems
' wenn es sich nicht um das gelöschte handelt
If Not item.OriginalProfilname = profilname Then
' Größe anpassen
ReDim Preserve ClassDIProfile._profiles(i)
' die Verknüpfung in das Items-Array übertragen
ClassDIProfile._profiles(i) = item
' Counter hochzählen
i += 1
End If
Next
Return True
Else
Return False
End If
Else
Return False
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Fehler beim Löschen eines Profils")
Return False
End Try
End Function
Public Shared ReadOnly Property Profile() As ClassDIProfil()
Get
Return ClassDIProfile._profiles
End Get
End Property
Public Shared ReadOnly Property Count() As Integer
Get
' Anzahl der Profile zurückgeben
If ClassDIProfile._profiles IsNot Nothing Then
Return ClassDIProfile._profiles.Length
Else
Return 0
End If
End Get
End Property
End Class