I have a script search that can be used to search in Linked Documents (regardless whether that is a semantically correct description of the concept)
'[path=\Projects\Project A\Search Scripts]
'[group=Search Scripts]
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
OnSearchScript()
Geert