0
votes

I have many use case diagrams defined in a model in Enterprise Architect. The diagrams are at different levels in the hierarchy. Irrespective of where the diagram is located, is there any way to access all the use case diagrams (any diagram for that matter) present in the model using Enterprise Architect Java API?

1

1 Answers

1
votes

The Java API is nothing more then a layer around the regular API, so I'm answering in general.

You can of course traverse the whole model in code to get diagrams, but that will take ages in any non-trivial model.

So what you want to do is

  1. query the model to get all diagram guid's using EA.Repository.SQLQuery()
  2. loop the diagram guid's and get each diagram using EA.Repository.GetDiagramByGUID()

The SQL Query

The SQL Query you need to get all use case diagram guid's is the following

select d.ea_guid from t_diagram d
where d.Diagram_Type = 'Use Case'

Getting a list of values from the query

EA.Repository.SQLQuery() returns an XML string, so you need to parse that to get a list of values. This example is an operation in VBScript that does exactly that:

function getArrayFromQuery(sqlQuery)
    dim xmlResult
    xmlResult = Repository.SQLQuery(sqlQuery)
    getArrayFromQuery = convertQueryResultToArray(xmlResult)
end function

'converts the query results from Repository.SQLQuery from xml format to a two dimensional array of strings
Public Function convertQueryResultToArray(xmlQueryResult)
    Dim arrayCreated
    Dim i 
    i = 0
    Dim j 
    j = 0
    Dim result()
    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
        arrayCreated = False
        'loop rows and find fields
        For Each rowNode In rowList
            j = 0
            If (rowNode.HasChildNodes) Then
                'redim array (only once)
                If Not arrayCreated Then
                    ReDim result(rowList.Length, rowNode.ChildNodes.Length)
                    arrayCreated = True
                End If
                For Each fieldNode In rowNode.ChildNodes
                    'write f
                    result(i, j) = fieldNode.Text
                    j = j + 1
                Next
            End If
            i = i + 1
        Next
        'make sure the array has a dimension even is we don't have any results
        if not arrayCreated then
            ReDim result(0, 0)
        end if
    end if
    convertQueryResultToArray = result
End Function

Getting the diagram based on the guid's

Loop the resulting query and use Repository.GetDiagramByGUID()

In VBScript that would be something like this (supposing the results of the query are stored in the variable guidResults)

dim diagram as EA.Diagram
dim diagrams
set diagrams = CreateObject("System.Collections.Arraylist")
dim guid

for each guid in guidResults
    set diagram = Repository.GetDiagramByGuid(guid)
    diagrams.Add diagram
next