7
« on: July 28, 2025, 04:28:53 pm »
'[path=\Projects\Project A\Search Scripts]
'[group=Search Scripts]
'GB_SearchInLinkedVB.vbs Util functions of Geert, added by SJ
option explicit
!INC Local Scripts.EAConstants-VBScript
' !INC Wrappers.Include
'
' This code has been included from the default Search Script template.
' If you wish to modify this template, it is located in the Config\Script Templates
' directory of your EA install path.
'
' Script Name: In Linked Documents
' Author: Geert Bellekens
' Purpose: Search for a certain string in the linked documents
' Date: 2021-06-25
'
' TODO 1: Define your search specification:
' The columns that will apear in the Model Search window
dim SEARCH_SPECIFICATION
SEARCH_SPECIFICATION = "<ReportViewData>" &_
"<Fields>" &_
"<Field name=""CLASSGUID""/>" &_
"<Field name=""CLASSTYPE"" />" &_
"<Field name=""Name"" />" &_
"<Field name=""Type"" />" &_
"<Field name=""Stereotype"" />" &_
"<Field name=""Description"" />" &_
"<Field name=""Path"" />" &_
"</Fields>" &_
"<Rows/>" &_
"</ReportViewData>"
'
' Search Script main function
'
sub OnSearchScript()
'get the search term
dim searchTerm
searchTerm = InputBox( "Please enter term to search for", "search term" )
'ask if we should look only under selected branch
dim response
response = MsgBox("Search only in selected package branch?", vbYesNo+vbQuestion, "Search scope")
dim package as EA.Package
if response = vbYes then
set package = Repository.GetTreeSelectedPackage
else
set package = nothing
end if
'get the linked documents
dim allLinkedDocuments
set allLinkedDocuments = getAllLinkedDocuments(package)
' Create a DOM object to represent the search tree
dim xmlDOM
set xmlDOM = CreateObject( "MSXML2.DOMDocument.6.0" )
xmlDOM.validateOnParse = false
xmlDOM.async = false
' Load the search template
if xmlDOM.loadXML( SEARCH_SPECIFICATION ) = true then
dim rowsNode
set rowsNode = xmlDOM.selectSingleNode( "//ReportViewData//Rows" )
' TODO 2: Gather the required data from the repository
dim element as EA.Element
for each element in allLinkedDocuments.Keys
dim ldText
ldText = allLinkedDocuments(element)
if instr(1,ldText, searchTerm, 1) > 0 then
AddRow xmlDOM, rowsNode, element
end if
next
' 'debug
' dim debugFile
' set debugFile = new TextFile
' debugFile.Contents = xmlDOM.xml
' 'save the debug file
' debugFile.FullPath = "I:\temp\scriptDebug.xml"
' debugFile.Save
' 'end debug
' Fill the Model Search window with the results
Repository.RunModelSearch "", "", "", xmlDOM.xml
else
Session.Prompt "Failed to load search xml", promptOK
end if
end sub
'
' TODO 3: Modify this function signature to include all information required for the search
' results. Entire objects (such as elements, attributes, operations etc) may be passed in.
'
' Adds an entry to the xml row node 'rowsNode'
'
sub AddRow( xmlDOM, rowsNode, element)
' Create a Row node
dim row
set row = xmlDOM.createElement( "Row" )
' Add the Model Search row data to the DOM
AddField xmlDOM, row, "CLASSGUID", element.elementGUID
AddField xmlDOM, row, "CLASSTYPE", element.Type
AddField xmlDOM, row, "Name", element.Name
AddField xmlDOM, row, "Type", element.Type
AddField xmlDOM, row, "Stereotype", element.Stereotype
AddField xmlDOM, row, "Description", element.Notes
AddField xmlDOM, row, "Path", element.FQName
' Append the newly created row node to the rows node
rowsNode.appendChild( row )
end sub
'
' Adds an Element to the DOM called Field which makes up the Row data for the Model Search window.
' <Field name "" value ""/>
'
sub AddField( xmlDOM, row, name, value )
dim fieldNode
set fieldNode = xmlDOM.createElement( "Field" )
' Create first attribute for the name
dim nameAttribute
set nameAttribute = xmlDOM.createAttribute( "name" )
nameAttribute.value = name
fieldNode.attributes.setNamedItem( nameAttribute )
if len(value) > 0 then
' Create second attribute for the value
dim valueAttribute
set valueAttribute = xmlDOM.createAttribute( "value" )
valueAttribute.value = value
fieldNode.attributes.setNamedItem( valueAttribute )
end if
' Append the fieldNode
row.appendChild( fieldNode )
end sub
'returns a dictionary of all elements that have a linked document as key and the text of the linked document as value.
function getAllLinkedDocuments(package)
dim queryString
queryString = "select o.object_ID from t_document d " & vbNewLine & _
" inner join t_object o on d.ElementID = o.ea_guid " & vbNewLine & _
" where d.ElementType = 'ModelDocument' "
if not package is nothing then
'get package tree id string
dim packageTreeIDString
packageTreeIDString = getPackageTreeIDString(package)
queryString = queryString & vbNewLine & _
" and o.package_ID in (" & packageTreeIDString & ")"
end if
dim elementsWithLinkedDocument
set elementsWithLinkedDocument = getElementsFromQuery(queryString)
dim linkedDocumentsDictionary
set linkedDocumentsDictionary = CreateObject("Scripting.Dictionary")
dim element as EA.Element
'loop the elements and add element and its linked document to the dictionary
for each element in elementsWithLinkedDocument
dim linkedDocumentText
linkedDocumentText = getLinkedDocumentContent(element, "TXT")
linkedDocumentsDictionary.Add element, linkedDocumentText
next
set getAllLinkedDocuments = linkedDocumentsDictionary
end function
'--
'get the package id string of the given package tree
function getPackageTreeIDString(package)
dim allPackageTreeIDs
set allPackageTreeIDs = CreateObject("System.Collections.ArrayList")
dim parentPackageIDs
set parentPackageIDs = CreateObject("System.Collections.ArrayList")
if not package is nothing then
parentPackageIDs.Add package.PackageID
end if
'get the actual package ids
getPackageTreeIDsFast allPackageTreeIDs, parentPackageIDs
'return
getPackageTreeIDString = Join(allPackageTreeIDs.ToArray,",")
end function
function getPackageTreeIDsFast(allPackageTreeIDs, parentPackageIDs)
if parentPackageIDs.Count = 0 then
if allPackageTreeIDs.Count = 0 then
'make sure there is at least a 0 in the allPackageTreeIDs
allPackageTreeIDs.Add "0"
end if
'then exit
exit function
end if
'add the parent package ids
allPackageTreeIDs.AddRange(parentPackageIDs)
'get the child package IDs
dim sqlGetPackageIDs
sqlGetPackageIDs = "select p.Package_ID from t_package p where p.Parent_ID in (" & Join(parentPackageIDs.ToArray, ",") & ")"
dim queryResult
set queryResult = getVerticalArrayListFromQuery(sqlGetPackageIDs)
if queryResult.Count > 0 then
dim childPackageIDs
set childPackageIDs = queryResult(0)
'call recursive function with child package id's
getPackageTreeIDsFast allPackageTreeIDs, childPackageIDs
end if
end function
function getVerticalArrayListFromQuery(sqlQuery)
dim xmlResult
xmlResult = Repository.SQLQuery(sqlQuery)
set getVerticalArrayListFromQuery = convertQueryResultToVerticalArrayList(xmlResult)
end function
function getSingleValueFromQuery(sqlQuery)
dim singleValue
singleValue = ""
dim result
set result = getArrayListFromQuery(sqlQuery)
dim row
for each row in result
dim column
for each column in row
singleValue = column
exit for
next
exit for
next
getSingleValueFromQuery = singleValue
end function
Function convertQueryResultToVerticalArrayList(xmlQueryResult)
Dim result
set result = CreateObject("System.Collections.ArrayList")
Dim xDoc
Set xDoc = CreateObject( "MSXML2.DOMDocument" )
'load the resultset in the xml document
If xDoc.LoadXML(xmlQueryResult) Then
'select the rows
Dim rowList
Set rowList = xDoc.SelectNodes("//Row")
Dim rowNode
Dim fieldNode
dim firstRow
firstRow = true
'loop rows and find fields
For Each rowNode In rowList
if firstRow then
For Each fieldNode In rowNode.ChildNodes
'add an arraylist for each column
result.Add CreateObject("System.Collections.ArrayList")
next
end if
'loop the field nodes
dim i
i = 0
For Each fieldNode In rowNode.ChildNodes
'add the contents to the correct column arraylist
result(i).Add fieldNode.Text
i = i + 1
Next
Next
end if
set convertQueryResultToVerticalArrayList = result
end function
'returns an ArrayList with the elements accordin tot he ObjectID's in the given query
function getElementsFromQuery(sqlQuery)
dim elements
set elements = Repository.GetElementSet(sqlQuery,2)
dim result
set result = CreateObject("System.Collections.ArrayList")
dim element
for each element in elements
result.Add Element
next
set getElementsFromQuery = result
end function
'gets the content of the linked document in the given format (TXT, RTF or EA)
function getLinkedDocumentContent(element, format)
dim linkedDocumentRTF
dim linkedDocumentEA
dim linkedDocumentPlainText
linkedDocumentRTF = element.GetLinkedDocument()
if format = "RTF" then
getLinkedDocumentContent = linkedDocumentRTF
else
linkedDocumentEA = Repository.GetFieldFromFormat("RTF",linkedDocumentRTF)
if format = "EA" then
getLinkedDocumentContent = linkedDocumentEA
else
linkedDocumentPlainText = Repository.GetFormatFromField("TXT",linkedDocumentEA)
getLinkedDocumentContent = linkedDocumentPlainText
end if
end if
end function
'--
OnSearchScript()