Author Topic: Element Usage list through API  (Read 423 times)

RWHurra

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Element Usage list through API
« on: October 04, 2017, 07:46:18 pm »
Hello!

I am creating some documentation/reports via script fragments. I would like to show specific diagrams. My first idea was to iterate through the element usage list (ctrl+u) and send diagrams to the report based on the name of the diagram.

However I didn't find anything like ctrl+u in the API, and didn't find it googling/searching the forum either. Does anyone know a method to create a collection with the diagrams like ctrl+u?

Is there a better approach to select specific diagrams?

Thank you!

RWHurra

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: Element Usage list through API
« Reply #1 on: October 04, 2017, 08:46:11 pm »
I know I can use the documentation section

Package: Element: Usage: Diagram

but I can't exclude diagrams from the section. Or can I?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 7671
  • Karma: +156/-21
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Element Usage list through API
« Reply #2 on: October 04, 2017, 09:45:42 pm »
You can put it in fragment and then set a filter on the fragment.

With regular script or SQL fragment you can't output diagram images.

Geert

Uffe

  • EA Practitioner
  • ***
  • Posts: 1048
  • Karma: +80/-5
  • Flutes: 1; Clarinets: 1; Saxes: 5 and counting
    • View Profile
Re: Element Usage list through API
« Reply #3 on: October 04, 2017, 09:53:10 pm »
Hi,

I'm not aware of an API call that does this. I'd say the simplest way is to use Repository.SQLQuery() and parse the resulting XML data.

You need to match t_object.Object_Id or t_object.Classifier to t_diagramobjects.Object_ID, and t_diagramobjects.Diagram_ID to t_diagram.Diagram_ID.

HTH,


/Uffe
My theories are always correct, just apply them to the right reality.

RWHurra

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: Element Usage list through API
« Reply #4 on: October 16, 2017, 07:12:43 pm »
Thank you for your help!

I did as Uffe said; SQL Query and matching the ID's.

I now get my data as following rows:
<Row><CLASSGUID>{xx}</CLASSGUID><CLASSTYPE>Type</CLASSTYPE><Name>Element name</Name><Diagram>Diagram name</Diagram><DiagramID>xx</DiagramID></Row>

My first problem is to parse this data from the XML-format.

I tried to use JScript-XML and the function XMLGetNodeText/XMLGetNodeTextArray with no success.




Also I do not know which of the following solutions are best.

I want to be able to chose which diagrams to include in the report. I believe it is easiest to choose diagram based on their names since we have an abbreviation in the end of the diagram names. However, when sending the diagram to the report I am more interested in the DiagramID.

Should I create a new object? myDiagram.name and .id? Should I have two arrays? One with string one with int? How would you do this?

I appreciate all your help!

EDIT:
I manage to fetch the first entry of DiagramID through

   var DOMDoc = XMLParseXML(SQLresult);
   var DiagramIDs = XMLGetNodeText( DOMDoc, "EADATA/Dataset_0/Data/Row/DiagramID" );

However, I (almost) always has several rows. I can't get XMLGetNodeTextArray to work. Or should I somehow loop XMLGetNodeText?

Dunno why I didn't get this to work before but

   var DOMDoc = XMLParseXML(SQLresult);
   var DiagramIDs = XMLGetNodeTextArray( DOMDoc, "//Row/DiagramID" );

works fine!


I also realized the problem Bellekens pointed out; With regular script or SQL fragment you can't output diagram images.

But if I set this up as Document Script I can choose what to send to the built in section editor?
« Last Edit: October 16, 2017, 08:49:43 pm by RWHurra »

OpenIT Solutions

  • EA User
  • **
  • Posts: 456
  • Karma: +4/-0
    • View Profile
Re: Element Usage list through API
« Reply #5 on: October 17, 2017, 03:08:54 am »
Hi,

Please see some vbscript I use with Repository.SQLQuery below (so I'm not using JavaScript):

set dom = CreateObject ("MSXML2.DOMDocument")

xml = Repository.SQLQuery (sql)
dom.setProperty "SelectionLanguage", "XPath"   
dom.loadXML (xml)

for each node In dom.SelectNodes ("//Row")
      pid = node.selectSingleNode("./pid").Text
      did = node.selectSingleNode("./did").Text
                 ...
NEXT



RWHurra

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: Element Usage list through API
« Reply #6 on: October 18, 2017, 07:13:10 pm »
I have managed to select diagrams with SQL query, collect the diagramIDs, select diagrams of interest with IF, send it to template with document script.

It works fine, I generate the report I want but I get this strange error code.



My SQL query is:

Code: [Select]
"SELECT t_object.ea_guid AS CLASSGUID, t_object.Object_Type AS CLASSTYPE, t_object.Name AS Name, t_diagram.Name AS Diagram, t_diagram.Diagram_ID AS DiagramID

FROM t_object, t_diagram, t_diagramobjects

WHERE t_object.Object_ID LIKE " + objectID + " AND t_diagramobjects.Object_ID = t_object.Object_ID AND t_diagramobjects.Diagram_ID = t_diagram.Diagram_ID

ORDER BY t_diagram.Name"

I get the correct result, but that annoying error message.

Anyone know why?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 7671
  • Karma: +156/-21
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Element Usage list through API
« Reply #7 on: October 18, 2017, 07:58:19 pm »
I would write it like this:
Code: [Select]
"SELECT o.ea_guid AS CLASSGUID, o.Object_Type AS CLASSTYPE, o.Name AS Name, d.Name AS Diagram, d.Diagram_ID AS DiagramID
FROM t_object o
INNER JOIN t_diagramobjects do ON do.Object_ID = o.Object_ID
INNER JOIN t_diagram d ON d.Diagram_ID = do.Diagram_ID
WHERE o.Object_ID ="  + ObjectID +
ORDER BY Diagram"

I've never had the error you mentioned, maybe the above version doesn't have the issue.

Geert

RWHurra

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: Element Usage list through API
« Reply #8 on: October 18, 2017, 10:38:11 pm »
I would write it like this:
Code: [Select]
"SELECT o.ea_guid AS CLASSGUID, o.Object_Type AS CLASSTYPE, o.Name AS Name, d.Name AS Diagram, d.Diagram_ID AS DiagramID
FROM t_object o
INNER JOIN t_diagramobjects do ON do.Object_ID = o.Object_ID
INNER JOIN t_diagram d ON d.Diagram_ID = do.Diagram_ID
WHERE o.Object_ID ="  + ObjectID +
ORDER BY Diagram"

I've never had the error you mentioned, maybe the above version doesn't have the issue.

Geert



Thank you for your input! Your query works good, although I get a similar error:



The generated report still looks good!

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 7671
  • Karma: +156/-21
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Element Usage list through API
« Reply #9 on: October 18, 2017, 10:53:16 pm »
Look into %appdata%\Sparx Systems\DBerror.txt or turn on the profiler on your SQL server.

That should given you some more info on the error you are getting. It will probably be something other then this actual query.

Geert

RWHurra

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: Element Usage list through API
« Reply #10 on: October 18, 2017, 11:28:24 pm »
Look into %appdata%\Sparx Systems\DBerror.txt or turn on the profiler on your SQL server.

That should given you some more info on the error you are getting. It will probably be something other then this actual query.

Geert

I isolated the problem to the function where I call the SQL-query and I think I know the problem.

I tried to print the result from the query and actually got two outputs:

Code: [Select]
Script Trace - <?xml version="1.0"?>
<EADATA version="1.0" exporter="Enterprise Architect">
</EADATA>

Code: [Select]
Script Trace - <?xml version="1.0"?>
<EADATA version="1.0" exporter="Enterprise Architect">
<Dataset_0><Data><Row><CLASSGUID>{xx}</CLASSGUID><CLASSTYPE>type</CLASSTYPE><Name>name</Name><Diagram>diagram name</Diagram><DiagramID>xx</DiagramID></Row></Data></Dataset_0></EADATA>
   
My guesss is that the first part yields the error since "//Row/DiagramID" can't be found.

Any thoughts about this?

I did not have this kind of output before though..? Don't know what might of happened.

Function:
Code: [Select]
!INC Local Scripts.EAConstants-JScript
!INC EAScriptLib.JScript-XML

function getDiagramOccurences( objectID /* : ElementID */ ) // Output: Array
{
var SQLresult = Repository.SQLQuery("SELECT o.ea_guid AS CLASSGUID, o.Object_Type AS CLASSTYPE, o.Name AS Name, d.Name AS Diagram, d.Diagram_ID AS DiagramID FROM t_object o INNER JOIN t_diagramobjects do ON do.Object_ID = o.Object_ID INNER JOIN t_diagram d ON d.Diagram_ID = do.Diagram_ID WHERE o.Object_ID = " + objectID + " ORDER BY Diagram");

var DOMDoc = XMLParseXML(SQLresult);
var DiagramIDs = XMLGetNodeTextArray( DOMDoc, "//Row/DiagramID" );

return DiagramIDs;
}

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 7671
  • Karma: +156/-21
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Element Usage list through API
« Reply #11 on: October 19, 2017, 12:16:24 am »
The error is a database error, not an XmlDom error, so it should be something on the database side.

EA does some pre-processing with the SQL's we send trough, so it might be related to that. I would really look into the dbError.txt and what you can see on the profiler.

Might not be related, but if you are only interested in the DiagramID, why are you returning all those other fields in your query?

Geert




RWHurra

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: Element Usage list through API
« Reply #12 on: October 19, 2017, 01:48:39 am »
The error is a database error, not an XmlDom error, so it should be something on the database side.

EA does some pre-processing with the SQL's we send trough, so it might be related to that. I would really look into the dbError.txt and what you can see on the profiler.

Might not be related, but if you are only interested in the DiagramID, why are you returning all those other fields in your query?

Geert

Thank you Geert.

I can't access %appdata%, I don't have Sparx Systems folder locally. I will contact the IT-dep.

I just wanted to see what objects I get out, and I know that with the names not the IDs. I can remove them, do you think it will make a difference though?

RWHurra

  • EA User
  • **
  • Posts: 32
  • Karma: +0/-0
    • View Profile
Re: Element Usage list through API
« Reply #13 on: October 30, 2017, 07:31:10 pm »
From DBError.txt:
Code: [Select]
2017-10-30 09:23:22
Microsoft OLE DB Provider for SQL Server [-2147217900]

Invalid column name 'undefined'.

Context:
SELECT o.ea_guid AS CLASSGUID, o.Object_Type AS CLASSTYPE, o.Name AS Name, d.Name AS Diagram, d.Diagram_ID AS DiagramID
FROM t_object o
INNER JOIN t_diagramobjects do ON do.Object_ID = o.Object_ID
INNER JOIN t_diagram d ON d.Diagram_ID = do.Diagram_ID
WHERE o.Object_ID = undefined
ORDER BY Diagram

It seems that objectID = undefined. Although, as I mentioned earlier it seems to run the script twice. Once with objectID = undefined and once with correct objectID, since I get the correct report output.

I have no idea how to backtrack this error. Is it in a script, function, template or fragment? :o

Thanks for your help!

Simon M

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 6181
  • Karma: +45/-5
    • View Profile
Re: Element Usage list through API
« Reply #14 on: October 31, 2017, 08:23:59 am »
"undefined" is what you will get from VBScript from an uninitialized variable.
Simon

support@sparxsystems.com