Author Topic: Get package hierarchy using EA API  (Read 272 times)

ankur

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Get package hierarchy using EA API
« on: April 05, 2019, 02:45:46 am »
Hello,

I am trying to generate a file based on the elements available withing a particular Package. I would like save the generated file in the same windows directory structure as the nested Package structure.

For eg:
Nested Package structure is: Project --> Package A --> Package B --> Package C

The corresponding file should be saved in a path which looks like: C:/*/Project/PackageA/PackageB/PackageC/<Filename>

Is there a possibility in EA API to get the package hierarchy from a given nested Package ?

Thanks in advance.

Regards,
Ankur

PeterHeintz

  • EA User
  • **
  • Posts: 784
  • Karma: +46/-17
    • View Profile
Re: Get package hierarchy using EA API
« Reply #1 on: April 05, 2019, 03:02:15 am »
There is no api feature like getPackagePath. However you can use the api to crawl through the packages and element to build you own getPathFeature.
Best regards,

Peter Heintz

ankur

  • EA User
  • **
  • Posts: 24
  • Karma: +0/-0
    • View Profile
Re: Get package hierarchy using EA API
« Reply #2 on: April 05, 2019, 03:05:34 am »
Hello Peter,

Thanks for your quick response. Yes, I will now try to generate the path using recursion.

Regards,
Ankur

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 9071
  • Karma: +235/-27
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Get package hierarchy using EA API
« Reply #3 on: April 05, 2019, 03:38:09 am »
Element.FQName might be useful as well.

Geert

qwerty

  • EA Guru
  • *****
  • Posts: 10191
  • Karma: +211/-177
  • I'm no guru at all
    • View Profile
Re: Get package hierarchy using EA API
« Reply #4 on: April 05, 2019, 04:17:54 am »
Element.FQName might be useful as well.
as long as your package names do not contain dots :-)

q.

Eamonn John Casey

  • EA User
  • **
  • Posts: 110
  • Karma: +0/-1
    • View Profile
Re: Get package hierarchy using EA API
« Reply #5 on: April 10, 2019, 11:29:03 pm »
I went by using SQL:

Code: [Select]
public List<EA.Package> GetPackagesInPackage(int packageID)
{
String sqlQuery = String.Format("SELECT ea_guid FROM t_package  WHERE Parent_ID = {0}", packageID);

List<EA.Package> elements = new List<EA.Package>();
String strResult = eaRepository.SQLQuery(sqlQuery);
List<Row> rows = EAGuid.Deserialise(strResult);
EA.Package rootPackage = eaRepository.GetPackageByID(packageID);

foreach (Row row in rows) {
EA.Package eaElement = eaRepository.GetPackageByGuid(row.Ea_guid);
elements.Add(eaElement);
}

return elements;
}

Pull out the fields that you need (ea_guid, Name).
The result from  eaRepository.SQLQuery is a JSON object.
Use a free online tool to generate the code to decerialise to an object (EAGuid.Deserialise(strResult)).
Then recurse through ea_guid & Parent_ID until you get 0 results.
Build up the String as you go along. (a -> b -> ...)
Print the string when you are at the bottom of the hierarchy.
« Last Edit: April 10, 2019, 11:30:54 pm by Eamonn John Casey »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 9071
  • Karma: +235/-27
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Get package hierarchy using EA API
« Reply #6 on: April 10, 2019, 11:36:39 pm »
I think the OP wants to do the opposite: go up instead of down.

Geert

PS:
Is the result of Repository.SQLQuery() a JSON object?
Isn't it simply an xml as a string?

I don't see the benefit of using some kind of tool to generate code for something as simple as that.