Author Topic: currentPkg.Elements.Count  (Read 156 times)

BruceTOGAF2

  • EA User
  • **
  • Posts: 28
  • Karma: +0/-0
    • View Profile
currentPkg.Elements.Count
« on: November 09, 2017, 08:42:23 pm »
I have a package ‘Element Counter Test’ containing  5 elements. The following Jscript functions perfectly. It reports 5 (currentPkg.Elements.Count) elements

However I have another enormous Jscript that finds packages and processes the elements contained in each package.  It tests the package contents, using the same code as that shown below (currentPkg.Elements.Count), but reports 0 elements.

Any idea?

/* ========================================================================================
' Script Name: js_Element_Counter_Test
' Author: Bruce Singleton
' Purpose: Count elements in selected package
' Date: 07/11/2017
' ========================================================================================*/

 
function main()
{
       
       // Get the currently selected package in the Project Browser
       var currentPkg as EA.Package;
       currentPkg = Repository.GetTreeSelectedPackage();

       if (currentPkg)
       {
              Session.Output("Found package '" + currentPkg.Name + "' with " + currentPkg.Elements.Count + " elements");
       }
       else
       {
              Session.Output( "Package NOT selected.");
       }
       
}

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 7740
  • Karma: +165/-21
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: currentPkg.Elements.Count
« Reply #1 on: November 09, 2017, 08:50:04 pm »
I've seen something similar right after doing a getlatest in code.
I had to get the package again using Repository.GetPackageByGUID before it would report an accurate number of packages.

Geert

BruceTOGAF2

  • EA User
  • **
  • Posts: 28
  • Karma: +0/-0
    • View Profile
Re: currentPkg.Elements.Count
« Reply #2 on: November 09, 2017, 09:47:42 pm »
Geert,

My initial post (currentPkg.Elements.Count) failed to explain the problem ‘currentPkg.Elements.Count’ properly.

I have a main Jscript that finds packages and processes the elements contained in each package.  My main Jscript finds the package using function taggedValueGetPkg() illustrated below.

Function taggedValueGetPkg looks for a particular tagged value ("01-Absolute_Number") and identifies the element containing the search Value in "01-Absolute_Number".

My main Jscript then tests the package contents, using currentPkg.Elements.Count, but reports 0 elements.

The problem is due to function taggedValueGetPkg which declares “var soughtElement as EA.Element”.

My main Jscript is finding each package as follows:

var paragPkg as EA.Package;
paragPkg = taggedValueGetPkg("01-Absolute_Number", Absolute_Number, "paragraph");

I know that function taggedValueGetPkg() works quite well because my main Jscript can test paragPkg.Name successfully.

Function taggedValueGetPkg() works quite well but it returns an Element object NOT a Package object.

SQL query 'select obj.Package_ID AS PKG_ID' will not work because that returns the package containing the package I am seeking.

How do I get function taggedValueGetPkg() to return the Package object associated with soughtElement?


/*-------------------------------------------------------------
' Author:   Bruce Singleton
' Date:     08/11/2017
'------------------------------------------------------------- */
function taggedValueGetPkg(property, value, versionVal)  {
   var soughtElement as EA.Element;
   // sql = "select obj.Package_ID AS PKG_ID from t_objectproperties prop, t_object obj ";
   sql = "select obj.Object_ID AS OBJ_ID from t_objectproperties prop, t_object obj ";   
   sql = sql + "WHERE prop.Object_ID=obj.Object_ID ";
   sql = sql + "AND obj.Object_Type='Package' ";
   sql = sql + "AND prop.Property='" + property + "' ";
   sql = sql + "AND prop.Value='" + value + "'";
   s = Repository.SQLQuery(sql);

   var myIDs = xmlColumn( s, "OBJ_ID");
   for ( var i = 0 ; i < myIDs.length; i++ ) {
      var currentElement as EA.Element;
      currentElement = Repository.GetElementByID(myIDs);
      if (currentElement) {
         // Session.Output("taggedValueGetPkg '" + currentElement.Name + "' " + currentElement.Version)
         if (currentElement.Version == versionVal) {
            soughtElement = currentElement;
            return soughtElement
         }
      }
   }

   return soughtElement;
}

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 7740
  • Karma: +165/-21
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: currentPkg.Elements.Count
« Reply #3 on: November 09, 2017, 09:57:51 pm »
The easiest solution is to use Repository.GetPackageByGUID with the guid of the returned package element.

Sparx doesn't guarantee this, but I've never known it to fail.

Otherwise you'll have to get the package ID from the miscdata (0 I think) and then use that to get the package object.

Geert

BruceTOGAF2

  • EA User
  • **
  • Posts: 28
  • Karma: +0/-0
    • View Profile
Re: currentPkg.Elements.Count
« Reply #4 on: November 09, 2017, 10:06:10 pm »
As usual Geert, your inspiration has helped me fix the problem.  The following function works well.

/*-------------------------------------------------------------
' Author:   Bruce Singleton
' Fixer: Geert Bellekens
' Date:     09/11/2017
         var theToPackage As EA.Package;
         theToPackage = taggedValueGetPkg("01-Absolute_Number", Absolute_Number, "heading");
'------------------------------------------------------------- */
function taggedValueGetPkg(property, value, versionVal)  {
   var soughtPkg as EA.Package;
   sql = "select obj.Object_ID AS OBJ_ID from t_objectproperties prop, t_object obj ";   
   sql = sql + "WHERE prop.Object_ID=obj.Object_ID ";
   sql = sql + "AND obj.Object_Type='Package' ";
   sql = sql + "AND prop.Property='" + property + "' ";
   sql = sql + "AND prop.Value='" + value + "'";
   s = Repository.SQLQuery(sql);

   var myIDs = xmlColumn( s, "OBJ_ID");
   for ( var i = 0 ; i < myIDs.length; i++ ) {
      var currentElement as EA.Element;
      currentElement = Repository.GetElementByID(myIDs);
      if (currentElement) {
         if (currentElement.Version == versionVal) {
            var soughtElement as EA.Element;
            soughtElement = currentElement;
            var soughtGUID = soughtElement.ElementGUID;
            soughtPkg = Repository.GetPackageByGUID(soughtGUID);
            return soughtPkg;
         }
      }
   }

   return soughtPkg;
}

Thank you.