Plugin Description
A nice utility that helps "clean up" your definitions, including these optional actions:
- Remove all Notes and/or Remarked elements
- Check for duplicate element IDs, log results to file
- Check for tokens missing @ or ~ chars, log results to file
- "Touch" feature changes modified date and username to new values
Original definitions are not changed in any way! ElementLog.txt and modified definitions are saved to new "Cleaned" folder.
* Requires Logi Info v9.0 or later. Plug-in was built with v10.0.479 rdPlugin module - may not work with all earlier versions of Logi Info.
Plug-In Installation Instructions: Click Here
View Code Snippet
Imports System.IO
Imports System.Xml
Imports System.Web
Imports System.Collections
Imports System.Math
Public Class Plugin
' ** REMEMBER: You need to stop/restart web server before creating a new build of this plug-in **
Public Sub ProcessDefinition(ByRef rdObjects As rdPlugin.rdServerObjects)
' define XML objects
Dim xmlDoc As New XmlDocument()
Dim objNodeList As XmlNodeList
Dim objNode As XmlNode
Dim objEle As XmlElement
' get the parameters passed to plugin
Dim lgxFilename As String = rdObjects.PluginParameters("lgxFilename").ToString()
Dim DelNotes As String = rdObjects.PluginParameters("DelNotes").ToString()
Dim DelRemarked As String = rdObjects.PluginParameters("DelRemarked").ToString()
Dim CheckDupeIDs As String = rdObjects.PluginParameters("DupeIDs").ToString()
Dim CheckTokens As String = rdObjects.PluginParameters("CheckTokens").ToString()
Dim DoTouch As String = rdObjects.PluginParameters("Touch").ToString()
Dim TouchDate As String = rdObjects.PluginParameters("TouchDate").ToString()
Dim TouchUser As String = rdObjects.PluginParameters("TouchUser").ToString()
' create a new file system folder beneath the original definition folder
Dim defInfo As System.IO.FileInfo = My.Computer.FileSystem.GetFileInfo(lgxFilename)
Dim defPath As String = defInfo.DirectoryName & "\Cleaned"
Dim defName As String = defInfo.Name
If Not System.IO.Directory.Exists(defPath) Then
My.Computer.FileSystem.CreateDirectory(defPath)
End If
If lgxFilename <> "" And System.IO.File.Exists(lgxFilename) Then
Try
' load the definition as an XML document
xmlDoc.Load(lgxFilename)
Catch
' clean up XML objects
objEle = Nothing
objNode = Nothing
objNodeList = Nothing
xmlDoc = Nothing
Exit Sub
End Try
' delete Notes and/or remarked elements
If DelNotes = "1" Or DelRemarked = "1" Then
If DelNotes = "1" Then
' get a nodelist for all "Note" nodes (records) in XML doc
objNodeList = xmlDoc.SelectNodes("//Note")
For Each objNode In objNodeList
objNode.ParentNode.RemoveChild(objNode) ' delete the node
Next
End If
If DelRemarked = "1" Then
' get a nodelist for all "Remark" nodes (records) in XML doc
objNodeList = xmlDoc.SelectNodes("//Remark")
For Each objNode In objNodeList
objNode.RemoveAll() ' delete all child nodes of this node
objNode.ParentNode.RemoveChild(objNode) ' delete the node itself
Next
End If
' write the modified XML document to the Cleaned folder
xmlDoc.Save(defPath & "\" & defName)
End If
' log element IDs and types, note any duplicate IDs or broken tokens
If CheckDupeIDs = "1" Or CheckTokens = "1" Then
' build array of all IDs and names
objNodeList = xmlDoc.SelectNodes("//*") ' all nodes
Dim arrIDs(objNodeList.Count, 3) As String
Dim i, n As Integer
For i = 0 To objNodeList.Count - 1
objEle = objNodeList(i)
If objEle.GetAttribute("ID") = "" Then
arrIDs(i, 0) = "--"
Else
arrIDs(i, 0) = objEle.GetAttribute("ID")
End If
arrIDs(i, 1) = objEle.Name
Next
' iterate array looking for duplicate IDs
If CheckDupeIDs = "1" Then
For i = LBound(arrIDs) To UBound(arrIDs) - 1
For n = i + 1 To UBound(arrIDs)
If arrIDs(i, 0) <> "--" And arrIDs(i, 0) = arrIDs(n, 0) Then
arrIDs(n, 2) = "DUPLICATE ID "
End If
Next
Next
End If
' look for tokens missing the @ or ~ characters (caveat: does not account for two tokens in one attribute value)
If CheckTokens = "1" Then
For i = 0 To objNodeList.Count - 1
objEle = objNodeList(i)
If objEle.HasAttributes Then
For n = 0 To objEle.Attributes.Count - 1
If Left(LTrim(objEle.Attributes(n).Value), 1) = "@" And InStr(objEle.Attributes(n).Value, "~") = 0 Then
arrIDs(i, 2) = arrIDs(i, 2) & "TOKEN MISSING ~"
ElseIf Right(RTrim(objEle.Attributes(n).Value), 1) = "~" And InStr(objEle.Attributes(n).Value, "@") = 0 Then
arrIDs(i, 2) = arrIDs(i, 2) & "TOKEN MISSING @"
End If
Next
End If
Next
End If
' write to element log file
Dim outFile As String = defPath & "\ElementLog.txt"
Dim FS As FileStream
Dim objWriter As StreamWriter
Dim bAppendFile = True
If System.IO.File.Exists(outFile) Then
FS = New FileStream(outFile, FileMode.Append, FileAccess.Write)
Else
FS = New FileStream(outFile, FileMode.Create, FileAccess.Write)
bAppendFile = False
End If
objWriter = New StreamWriter(FS)
' put title in file, if first time
If bAppendFile = False Then
objWriter.WriteLine(ControlChars.NewLine & "LOGI APPLICATION ELEMENT LOG" & Space(10) & Now())
End If
' write element array to file
objWriter.WriteLine(ControlChars.NewLine)
objWriter.WriteLine("=== " & defName & " ===" & ControlChars.NewLine)
objWriter.WriteLine("Element ID" & ControlChars.Tab & ControlChars.Tab & ControlChars.Tab & "Element Type" & ControlChars.NewLine)
For i = 0 To arrIDs.GetLength(0) - 1
n = Max(25 - Len(arrIDs(i, 0)), 2) ' make IDs uniform length so tab chars work in log file
objWriter.WriteLine(arrIDs(i, 0) & Space(n) & ControlChars.Tab & arrIDs(i, 1) _
& ControlChars.Tab & ControlChars.Tab & ControlChars.Tab & arrIDs(i, 2))
Next
'clean up file writer objects
objWriter.Close()
FS.Close()
End If
' update user and time stamp on this definition
If DoTouch = "1" And TouchDate <> "" And TouchUser <> "" Then
objEle = xmlDoc.SelectSingleNode("//Report")
objEle.SetAttribute("SavedBy", TouchUser)
objEle.SetAttribute("SavedAt", TouchDate)
' write the modified XML document to Cleaned folder
xmlDoc.Save(defPath & "\" & defName)
End If
' clean up XML objects
objEle = Nothing
objNode = Nothing
objNodeList = Nothing
xmlDoc = Nothing
End If
End Sub
End Class
Further Reading
How To Create a .NET Plug-In: Click Here
How To Create A Java Plug-In: Click Here
Logi Plug-Ins: Click Here