javascriptenterprise-architect

Enterprise Architect: Adding a label to a Legend (in JavaScript)


I was inspired by Enterprise Architect scripting with Java - add CustomProperty

I have no issues adding the legend to the diagram, but the entries in the legend aren't being added. Any ideas why this might not be working are welcome. (Version 15.2.1554)

function out() {
    let string = "";
    for (let i = 0; i < arguments.length; i++) {
        string += JSON.stringify(arguments[i], null, 2) + " ";
    }
    Session.Output(string);
}

function addLegendEntries(legendGUID, arr=[{ 'BackgroundColor': -1, 'Stereotype' : 'Stereotype'}]){
    let description = "";
    let customPropertyIndex = 0;
    arr.forEach( ({BackgroundColor, Stereotype}) => {
        description += "@PROP=@NAME="+Stereotype+"@ENDNAME;@TYPE=LEGEND_OBJECTSTYLE@ENDTYPE;@VALU=#Back_Ground_Color#="+BackgroundColor+";#Pen_Color#=16777215;#Pen_Size#=1;#Legend_Type#=LEGEND_OBJECTSTYLE;@ENDVALU;@PRMT="+customPropertyIndex++ +"@ENDPRMT;@ENDPROP;"
    });
        description += "@PROP=@NAME=Legend@ENDNAME;@TYPE=LEGEND_STYLE_SETTINGS@ENDTYPE;@VALU=@ENDVALU;@PRMT=@ENDPRMT;@ENDPROP;"
    const sqlInsertStmt="INSERT INTO t_xref "
            + "(" 
                + "Client,"
                + "XrefID,"
                + "Type,"
                + "Name,"
                + "Visibility,"
                + "`Partition`,"
                + "Supplier,"
                + "Description"
            + ") "
            + " VALUES ("
                +"'"+legendGUID+ "',"
                + "'{"+  generateGUID() +"}',"
                + "'element property',"
                + "'CustomProperties',"
                + "'Public',"
                + "'0',"
                + "'&lt;none&gt;',"
                + "'"+description+"'"
            + ");"
            
            out(sqlInsertStmt);
    out(Repository.SQLQuery(sqlInsertStmt));
}

function addLegend(diagramID, arr=[{ 'BackgroundColor': -1, 'Stereotype': 'Stereotype'}])
{
    Repository.EnsureOutputVisible("Script");    
    Repository.ClearOutput("Script");
    var pkg as EA.Package; 
    pkg = Repository.GetTreeSelectedPackage();   
    elements = pkg.Elements;
    var legend = elements.AddNew('Diagram Legend', 'Text');
    legend.Subtype = 76;    
    legend.Update();
    var diagram as EA.Diagram;
    diagram = Repository.GetDiagramByID(diagramID);
    Session.Output(diagram.Name);
    diagramObjects = diagram.DiagramObjects;

    diagramObject = diagramObjects.AddNew("l=100; r=100; t=100; b=500;", "");
    addLegendEntries(legend.ElementGUID, arr);
    diagramObject.ElementID = legend.ElementID;
    diagramObject.Update();
    diagram.Update();
}
    Repository.EnsureOutputVisible( "Script" );
    Repository.ClearOutput("Script"); 
    const diagram = Repository.GetDiagramByGuid("{4FA59731-72DF-402c-AF60-1C3381BC2052}");       // Get the current diagram
    out(diagram.DiagramID);
    addLegend(diagram.DiagramID, collectColorsInDiagram(diagram));
    Repository.ReloadDiagram(diagram.DiagramID);
    out("Done");

Here's the SQL statement that is generated:

INSERT INTO t_xref 
    (Client,XrefID,Type,Name,Visibility,Partition,Supplier,Description)  
    VALUES 
    ('{9CCF46B8-D221-41b1-B1B9-EF7072547F74}','{cb940358-f76f-9fdd-dfca-d4537143e394}','element property','CustomProperties','Public','0','&lt;none&gt;',
'@PROP=@NAME=ArchiMate_Grouping@ENDNAME;@TYPE=LEGEND_OBJECTSTYLE@ENDTYPE;@VALU=#Back_Ground_Color#=13458026;#Pen_Color#=16777215;#Pen_Size#=1;#Legend_Type#=LEGEND_OBJECTSTYLE;@ENDVALU;@PRMT=0@ENDPRMT;@ENDPROP;@PROP=@NAME=ArchiMate_Capability@ENDNAME;@TYPE=LEGEND_OBJECTSTYLE@ENDTYPE;@VALU=#Back_Ground_Color#=15528442;#Pen_Color#=16777215;#Pen_Size#=1;#Legend_Type#=LEGEND_OBJECTSTYLE;@ENDVALU;@PRMT=1@ENDPRMT;@ENDPROP;@PROP=@NAME=ArchiMate_Capability@ENDNAME;@TYPE=LEGEND_OBJECTSTYLE@ENDTYPE;@VALU=#Back_Ground_Color#=16777184;#Pen_Color#=16777215;#Pen_Size#=1;#Legend_Type#=LEGEND_OBJECTSTYLE;@ENDVALU;@PRMT=2@ENDPRMT;@ENDPROP;@PROP=@NAME=ArchiMate_Capability@ENDNAME;@TYPE=LEGEND_OBJECTSTYLE@ENDTYPE;@VALU=#Back_Ground_Color#=16443110;#Pen_Color#=16777215;#Pen_Size#=1;#Legend_Type#=LEGEND_OBJECTSTYLE;@ENDVALU;@PRMT=3@ENDPRMT;@ENDPROP;@PROP=@NAME=ArchiMate_Grouping@ENDNAME;@TYPE=LEGEND_OBJECTSTYLE@ENDTYPE;@VALU=#Back_Ground_Color#=13434880;#Pen_Color#=16777215;#Pen_Size#=1;#Legend_Type#=LEGEND_OBJECTSTYLE;@ENDVALU;@PRMT=4@ENDPRMT;@ENDPROP;@PROP=@NAME=@ENDNAME;@TYPE=LEGEND_OBJECTSTYLE@ENDTYPE;@VALU=#Back_Ground_Color#=-1;#Pen_Color#=16777215;#Pen_Size#=1;#Legend_Type#=LEGEND_OBJECTSTYLE;@ENDVALU;@PRMT=5@ENDPRMT;@ENDPROP;@PROP=@NAME=Legend@ENDNAME;@TYPE=LEGEND_STYLE_SETTINGS@ENDTYPE;@VALU=@ENDVALU;@PRMT=@ENDPRMT;@ENDPROP;');

The SQL statement appears not to be working and there is nothing in DBError.txt.


Solution

  • You can't execute update or insert statements using Repository.SQLQuery()

    You can use the undocumented and unsupported method Repository.Execute(), but you better be damn sure of what you are doing. This has the potential to seriously corrupt your model.