Creating Items in Sparx EA via Java

Introduction


This short post provides guidance on how to create "things" in a Sparx EA model via the Java API.

Theory


Creating "things" in EA via Java is driven by the Add New method and the Collection class. The first example in the code is adding a "view" to an existing root node. The code below will retrieve the root GUID if the "i" variable is set to 0;


rootPackageID = rep.GetModels().GetAt(i).GetPackageGUID();

After obtaining the root GUID it is possible to create a view under the root node. Note the process of retrieving the packages under the root and placing them in a collection. The Add New process is driven from the Collection.

Importantly, for a view, the code needs to set a flag which indicates a particular icon. This process is only required for views.

org.sparx.Package root = rep.GetPackageByGuid(rootPackageID);  // get the root 

Collection<org.sparx.Package> subPackages = root.GetPackages(); // get the packages in the root

org.sparx.Package theView = subPackages.AddNew(viewName, ""); // create a view under the root 

theView.SetFlags("VICON=4;");   // This is a flag which indicates that the package is a view.
                                            // The different numbers indicate a specific icon.
                                            // The values are from 0 to 5.

theView.Update();  // update the packages

viewPackageGUID = theView.GetPackageGUID();   // store the GUID for the view, use it later

rep.GetModels().Refresh();  // refresh the repo

More Examples

You can find additional examples on GitHub. The code is commented which makes it easy to follow.




Comments

Popular posts from this blog

Module Management - Part 1 - getModule() Pattern

Traversing a EA Model via Java