Book a Demo

Author Topic: Updating TaggedValues  (Read 4189 times)

MaXyM

  • EA User
  • **
  • Posts: 120
  • Karma: +8/-0
    • View Profile
Updating TaggedValues
« on: September 28, 2018, 12:36:57 am »
Hello.
Likely i'm performing some conceptual mistake... but cannot find the root cause.
Originally the need of writing the script comes from EA bug during database RE, resulting duplicating Tagged Values. See image:


Script should copy value from uppercase TV to lowercase one, and then remove the first one from the object.
Here is the code:
Code: [Select]
function main()
{
var tbl as EA.Element;
tbl = GetElementByGuid('{7E0B2D3D-ABB8-4c3b-9B2B-A7A2B38A5CE0}');

        NormalizeDoubledTV(tbl, 'Owner');
NormalizeDoubledTV(tbl, 'Tablespace');
NormalizeDoubledTV(tbl, 'DBVersion');
}


function NormalizeDoubledTV(tbl, tvname)
{
var lwr as EA.TaggedValue;
var upr as EA.TaggedValue;

lwr = tbl.TaggedValues.GetByName(tvname);
upr = tbl.TaggedValues.GetByName(tvname.toUpperCase());

if (lwr !== null && upr !== null)
{
lwr.Value = upr.Value;
lwr.Value = upr.Value;
tbl.TaggedValues.Refresh();
tbl.Update();

DeleteTaggedValue(tbl, tvname.toUpperCase());
}

}


function DeleteTaggedValue(objj, tvname)
{
var obj as EA.Element;
obj = objj;

for (i = 0; i<= obj.TaggedValues.Count; i++)
{
var currentTagVal as EA.TaggedValue;
currentTagVal = obj.TaggedValues.GetAt(i)
if (currentTagVal.Name == tvname)
{
obj.TaggedValues.DeleteAt(i, false);
obj.Update();
obj.TaggedValues.Refresh();
return;
}

}



}

I debugged the issue down to the point, that I know the value of lowercased TV is updated until entering for loop in DeleteTaggedValue. So currentTagVal  variable contains not updated value. I was sure that in javascript references to objects (variables) are being passed.

thanx in advance

qwerty

  • EA Guru
  • *****
  • Posts: 13584
  • Karma: +397/-301
  • I'm no guru at all
    • View Profile
Re: Updating TaggedValues
« Reply #1 on: September 28, 2018, 12:53:55 am »
You should not use GetByName with TVs since they are ambiguous. Use your own operation to iterate the collection.

q.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Updating TaggedValues
« Reply #2 on: September 28, 2018, 01:23:18 am »
I'm pretty sure GetByName is case insensitive, and returns the first (without a defined order) instance it finds.

I too never use that operation.

Geert

MaXyM

  • EA User
  • **
  • Posts: 120
  • Karma: +8/-0
    • View Profile
Re: Updating TaggedValues
« Reply #3 on: September 30, 2018, 02:16:59 am »
OK guys, thanx for notes. The problem did lay between a keyboard and a chair ;)

There was lwr.Update() missing in NormalizeDoubledTV() function.
In addition to this,  tbl.Update() is also not needed. tbl.TaggedValues.Refresh() is enough

Sorry for that, I really thought I explored all possibilities before posting the question.