Author Topic: Set element stereotype by script  (Read 4743 times)

PhilippW

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Set element stereotype by script
« on: March 30, 2021, 12:59:07 am »
Hi,

I wanted to change the stereotype of all elements inside a package by using a jScript. In the script I'm trying to set a MDG provided stereotype by Element.Stereotype ="MDG::Stereotype". And here comes the point were I'm struggling - most probably with user permissions.

I know the script is executed since some session outputs are shown. But the stereotype is not set.
Are there any special user permissions needed to set a stereotype in this way? - Doing the step manually, means setting the stereotype in the properties window of each element works fine.
Is it a problem, that the stereotype comes from a MDG and can therefore not be set by script?

I was thinking about, if setting an stereotype by simply assigning a string - like in the script - is only possible with the "configure stereotype" user permission?
Otherwise, instead of setting a hard string as stereotype - maybe there is a way to select MDG defined stereotypes in the script?

Unfortunately I cannot change the permissions to test different settings.

I know this is a lot of unknown details, but maybe one can help me in identifying the real problem.

btw I'm using EA version 15.2

Regards,
Philipp

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Set element stereotype by script
« Reply #1 on: March 30, 2021, 01:02:47 am »
Philipp,

You need to use the StereotypeEx property to set fully qualified stereotype.

Element.StereotypeEx ="MDG::Stereotype"

Will work (don't forget to Update() in order to save your changes)

Geert

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Set element stereotype by script
« Reply #2 on: March 30, 2021, 01:05:47 am »
There was (or maybe/probably still is) a bug however that you need the "configure stereotypes" permission for some of this stuff to work.

Had the problem when trying to create «master document» packages by script. Worked for me (admin) but not for my coworkers that didn't have that permission.

Geert

PhilippW

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: Set element stereotype by script
« Reply #3 on: March 30, 2021, 01:23:18 am »
Hi Geert,

thanks for the fast reply.

I now tried the StereotypeEx option and used the GetLastError method.
It really seems to be a permission problem. The GetLastError method shows:

You have not been granted permission to 'Configure new Stereotypes'.

Please contact your model administrator.


I would have thought setting the fully qualified stereotype from the MDG works without this permission. Do you know if this is a known bug? Does it make sense to report it then?

Philipp

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Set element stereotype by script
« Reply #4 on: March 30, 2021, 01:35:57 am »
Yes it's a known bug (I reported it a while ago) and yes it makes sense to report it.

The easiest solution is to add that permission.
Possible workarounds is to update the database directly, however that is a pain to get it right.

Geert

Sunshine

  • EA Practitioner
  • ***
  • Posts: 1324
  • Karma: +121/-10
  • Its the results that count
    • View Profile
Re: Set element stereotype by script
« Reply #5 on: March 30, 2021, 07:10:08 am »
Geert is right easiest way is to set permissions to allow it.
I've not come across this before. I guess the other thing to try is another script. I know this worked for me but I'm the admin and gave myself all the admin rights. Here is a JScript that converts stereotypes
Code: [Select]
!INC Local Scripts.EAConstants-JScript
// Replace the file below with another element list to reuse this script.
//!INC Conversion Scripts.ConversionTable

function TypeConversion(sourceObject,sourceStereotype,targetObject, targetStereotype)
{
    this.sourceObject = sourceObject;
    this.sourceStereotype = sourceStereotype;
    this.targetObject = targetObject;
    this.targetStereotype = targetStereotype;
}

var Conversions = new Array();

//Convert standards to MDG standard
Conversions[0] = new TypeConversion("Class", "Standard", "Class", "Motivation::Standard");

// =================================================================================
// Name: Element Conversion
// Converts elements type and stereotype as defined in the element conversation table.
// The table is included as a file so it may be changed for other mappings
// Navigates from selected package and recursively modifies each element
// NOTE: Requires a package to be selected in the Project Browser
//
// Related APIs
// =================================================================================
// Element API - http://www.sparxsystems.com/enterprise_architect_user_guide/10/automation_and_scripting/element2.html
// Repository API http://www.sparxsystems.com/enterprise_architect_user_guide/10/automation_and_scripting/repository3.html
// Tagged Value API http://www.sparxsystems.com/enterprise_architect_user_guide/10/automation_and_scripting/taggedvalue.html

function StartWithSelectedPackage()
{
    // Show the script output window
    Repository.EnsureOutputVisible( "Script" );

    Session.Output( "JScript TypeConverstion" );
    Session.Output( "===========================" );

    var thePackage as EA.Package;
    thePackage = Repository.GetTreeSelectedPackage();
   
    if ( thePackage != null && thePackage.ParentID != 0 )
    {
        NavigatePackage( "", thePackage );
    }
    else
    {
        Session.Prompt( "This script requires a package to be selected in the Project Browser.\n" +
            "Please select a package in the Project Browser and try again.", promptOK );
    }
   
    Session.Output( "Done!" );
}

//
// Outputs the packages name and elements, and then recursively processes any child
// packages
//
// Parameters:
// - indent A string representing the current level of indentation
// - thePackage The package object to be processed
//
function NavigatePackage( indent, thePackage )
{
    // Cast thePackage to EA.Package so we get intellisense
    var currentPackage as EA.Package;
    currentPackage = thePackage;
   
    // Add the current package's name to the list
    Session.Output( indent + currentPackage.Name + " (PackageID=" +
        currentPackage.PackageID + ")" );
   
    // Convert the elements this package contains
    ConvertElementsInPackage( indent + "    ", currentPackage );
   
    // Recursively process any child packages
    var childPackageEnumerator = new Enumerator( currentPackage.Packages );
    while ( !childPackageEnumerator.atEnd() )
    {
        var childPackage as EA.Package;
        childPackage = childPackageEnumerator.item();
        NavigatePackage( indent + "    ", childPackage );
       
        childPackageEnumerator.moveNext();
    }
}

//
// Converts the elements of the provided package to the Script output window
//
// Parameters:
// - indent A string representing the current level of indentation
// - thePackage The package object to be processed
//
function ConvertElementsInPackage( indent, thePackage )
{
    // Cast thePackage to EA.Package so we get intellisense
    var currentPackage as EA.Package;
    currentPackage = thePackage;
   
    // Iterate through all elements and add them to the list
    var elementEnumerator = new Enumerator( currentPackage.Elements );
    while ( !elementEnumerator.atEnd() )
    {
        var currentElement as EA.Element;
        currentElement = elementEnumerator.item();
        ConvertElements(indent+"    ",currentElement );

        elementEnumerator.moveNext();
    }
}

function ConvertElements( indent, theElement )
{
    // Cast theElement to EA.Element so we get intellisense
    var currentElement as EA.Element;
    currentElement = theElement;
    currentElement.ObjectType
    ConvertElement(indent+"    ",currentElement );
    // Iterate through all embedded elements and add them to the list
    var elementEnumerator = new Enumerator( currentElement.Elements );
    while ( !elementEnumerator.atEnd() )
    {
        var currentElement as EA.Element;
        currentElement = elementEnumerator.item();
        ConvertElements(indent+"    ",currentElement );
        elementEnumerator.moveNext();
    }
}

// Converts the element from BSIF to
//
// Parameters:
// - indent A string representing the current level of indentation
// - theElement The element object to be processed
function ConvertElement( indent, theElement )
{
// Debug Comment out when run for real
    //Session.Output( indent + "CALLED: ConvertElement with " + theElement.Name + " [" + theElement.Type + ", " + theElement.Stereotype + " )" );

    for ( var i = 0 ; i < Conversions.length ; i++ )
    {
        // If want to limit to stereotype that matches source list then convert
 //       if ( (theElement.Stereotype == Conversions[i].sourceStereotype) && (theElement.Type == Conversions[i].sourceObject ))
        {
            Session.Output( indent + "CONVERTED: " + theElement.Name + " (" + theElement.Type + ", " + theElement.Stereotype + ")" + "=>" +"("+Conversions[i].targetObject+","+Conversions[i].targetStereotype+")" );
             theElement.Type = Conversions[i].targetObject;
            //Overright the stereotype list to have only one stereotype
            theElement.StereotypeEx = Conversions[i].targetStereotype;
            theElement.Update();
            break; // once found cease iterating through for-loop.
        }
    }
}

StartWithSelectedPackage();

Hope that helps
Happy to help
:)

PhilippW

  • EA Novice
  • *
  • Posts: 7
  • Karma: +0/-0
    • View Profile
Re: Set element stereotype by script
« Reply #6 on: March 31, 2021, 12:17:05 am »
Hi,
thanks for your feedback. It has helped me a lot :)
I'll go for the "add permission" solution.

Philipp