Author Topic: Repository.GetElementByAlias  (Read 2911 times)

ggandhi

  • EA User
  • **
  • Posts: 28
  • Karma: +0/-0
    • View Profile
Repository.GetElementByAlias
« on: May 12, 2014, 07:49:31 pm »
In JScript, is there an equivalent of Repository.GetElementByID() for finding an element by Alias?

I have written the following recursive JScript function getNodeIdByAlias for searching by Alias. The input vars are current package and EID (External ID) that needs to be matched with an Element.Alias. My recursive function searches elements within current package then calls itself (getNodeIdByAlias) to seach all packages within currentPackage to see if any Node has the same Alias as the provided EID (external ID = Alias. Because the function is recursive, it drills down and searches all packages beneath (descendents of) the current package.

Function getNodeIdByAlias is slow, especially when searching a large Sparx SQL Server Repository. Is there a faster method?  


function getNodeIdByAlias(currentPackage, eid)
{
      var foundNodeID = 0;
      var nodes as EA.Collection;
      nodes = currentPackage.Elements;
      
      var nodeQty = currentPackage.Elements.Count;

      for ( var i = 0 ; i < nodeQty ; i++ )
      {
            var currentElement as EA.Element;
            var aliasFound = false;
            currentElement = nodes.GetAt( i );
            if (eid == currentElement.Alias)
            {
                  foundNodeID = currentElement.ElementID;
                  return foundNodeID;
            }
      }
      
      var packages as EA.Collection;
      packages = currentPackage.Packages;
      for ( var k = 0 ; k < packages.Count ; k++ )
      {
            var thisPkg as EA.Package;
            thisPkg = packages.GetAt( k );
            
            if ((thisPkg.Name != "*DO NOT USE*")
                  && (thisPkg.Name != "Templates and References")
                  && (thisPkg.Name != "Application Catalogue")
                  && (thisPkg.Name != "Locations")
                  )
            {
                  foundNodeID = getNodeIdByAlias(thisPkg, eid);
                  if (foundNodeID > 0)
                  {
                        return foundNodeID;
                  }
            }
      }
      return foundNodeID;
}

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +396/-301
  • I'm no guru at all
    • View Profile
Re: Repository.GetElementByAlias
« Reply #1 on: May 12, 2014, 08:14:05 pm »
Quote
In JScript, is there an equivalent of Repository.GetElementByID() for finding an element by Alias?
You even can't find by name since it is not unique (although there are some objects which offer that method).

If you want to search by Alias you will need to write a SQL and use either Repository.SQLQuery of .GetElementSet.

q.