Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: bilon on May 03, 2018, 12:02:22 am

Title: Problem with Update method
Post by: bilon on May 03, 2018, 12:02:22 am
I have simple script changing single property of elements attribute and subsequently iterating through elements attributes to display the new value. But the old value is displayed. Here is the script:

Sub Main
   Dim tab AS EA.Element
   Dim col AS EA.Attribute
   For Each tab In Repository.GetTreeSelectedElements
      For Each col In tab.Attributes
         If col.Name = "TABLE_NAME" Then
            Select Case col.Type 
               Case "varchar"
               col.Type = "char"
               col.Update
               Session.Output "1: " & col.Name & " " &  col.Type
               '1: TABLE_NAME char
            Case Else   
            End Select
         End If      
      Next
      'tab.Attributes.Refresh
      For Each col In tab.Attributes
         If col.Name = "TABLE_NAME" Then
            Session.Output "2: " & col.Name & " " &  col.Type
            '2: TABLE_NAME varchar
         End If
      Next
   Next
End Sub

The new value is correctly saved to model, so why it is not displayed in the script?
Title: Re: Problem with Update method
Post by: qwerty on May 03, 2018, 12:17:46 am
You need a Refresh of the collection in between the 2 loops.

q.
Title: Re: Problem with Update method
Post by: bilon on May 03, 2018, 07:41:56 pm
As you can see, Refresh is already there. But commented, because it doesn't help.
Title: Re: Problem with Update method
Post by: Uffe on May 03, 2018, 08:47:32 pm
I think you need to
    tab.Refresh()
as well.

Quote from: User Guide link=https://www.sparxsystems.com/enterprise_architect_user_guide/13.5/automation/element2.html
Notes: Refreshes the element features in the Project Browser.

Usually called after adding or deleting attributes or methods, when the user interface is required to be updated as well.

/U
Title: Re: Problem with Update method
Post by: qwerty on May 03, 2018, 10:39:12 pm
I might be wrong, but the refresh.line seems to be commented out.

q.
Title: Re: Problem with Update method
Post by: Uffe on May 04, 2018, 05:12:04 pm
Yes, but he's refreshing the Collection, not the Element. According to the API documentation, you need to refresh the element after adding attributes.

/U
Title: Re: Problem with Update method
Post by: qwerty on May 04, 2018, 06:46:50 pm
I shouldn't lean in too much without testing, but refresh is to update a collection. He's changing elements of a collection and you need to tell EA to get the updates since it's using a cache of the old values. Since the refresh is commented in his code, this must be the culprit.

q.
Title: Re: Problem with Update method
Post by: bilon on May 04, 2018, 11:31:00 pm
The reason why tab.Attributes.Refresh is commented, is, that tested it but it didn't work, so I commented it. Even tab.Refresh doesn't work!
Title: Re: Problem with Update method
Post by: qwerty on May 05, 2018, 01:14:57 am
Well, I just ran your script and it worked with NO issues.

q.
Title: Re: Problem with Update method
Post by: Uffe on May 07, 2018, 08:00:28 pm
I shouldn't lean in too much without testing, but refresh is to update a collection.
I know, but it's implemented by other classes as well, and the user guide (as quoted in my first post) explicitly states that you need to Refresh() the Element itself after adding attributes.

Well, I just ran your script and it worked with NO issues.

.... and that's the end of this argument. :)

/U
Title: Re: Problem with Update method
Post by: bilon on May 10, 2018, 10:22:59 pm
What do you mean with "worked with no issues"? Does it mean, that the output looked like this:
1: TABLE_NAME char
2: TABLE_NAME char
Title: Re: Problem with Update method
Post by: qwerty on May 11, 2018, 12:20:40 am
Lucky you, it was still in my output window:
Quote
1: TABLE_NAME char   
2: TABLE_NAME varchar   
1: TABLE_NAME char   
2: TABLE_NAME varchar   

(output from 2 runs).

q.
Title: Re: Problem with Update method
Post by: bilon on May 16, 2018, 01:01:18 am
In this case, it isn't definitely without issues. The issue is, that after setting datatype to "char" in step 1, it is displayed as "varchar" in step 2. They should be same.
Title: Re: Problem with Update method
Post by: qwerty on May 16, 2018, 04:36:44 am
Ah, now I see. Unfortunately you reply here in long distances so I'm out each time. I see that the 2nd loop shows the non-updated values. But EA is doing the change (this is why I posted that it works correctly) since afterwards the types ARE changed. So the question is: why does the 2nd loop show the wrong values. Hmm. Tricky indeed.

q.
Title: Re: Problem with Update method
Post by: qwerty on May 16, 2018, 04:55:39 am
Replacing the 2nd loop like this
Code: [Select]
     dim tab1 as EA.Element
     set tab1 = Repository.GetElementByID(tab.ElementID)
     For Each col In tab1.Attributes
         If col.Name = "TABLE_NAME" Then
            Session.Output "2: " & col.Name & " " &  col.Type
            '2: TABLE_NAME varchar
         End If
      Next
fixed the issue. Honestly, I never ever used a collection more than once. This Refresh-thingy is just fishy in my opinion. And obviously it's broken too. Feel free to send a bug report.

q.

P.S. Probably the Refresh has only an effect if you added or removed elements from it. In any case it's broken.
Title: Re: Problem with Update method
Post by: cjcrystal on May 17, 2018, 07:41:58 pm
in relation to Element.Update ,

How exactly does this work?
Updates the Element in the database and the model?

What happens when the Element is locked. Does it force Update?

Thank you.
Title: Re: Problem with Update method
Post by: Geert Bellekens on May 17, 2018, 08:43:44 pm
Update save the changed properties of your EA.Element instance in memory to the database.

If the element is read-only because of locking you'll get an exception.

Geert
Title: Re: Problem with Update method
Post by: cjcrystal on May 22, 2018, 02:20:50 pm
Update save the changed properties of your EA.Element instance in memory to the database.

If the element is read-only because of locking you'll get an exception.

Geert

thank you Geert  , much appreciated
Title: Re: Problem with Update method
Post by: bilon on May 23, 2018, 10:05:02 pm
Yes, this way it is described in the documentation: "Should be called after adding a new item or after deleting an item".
Title: Re: Problem with Update method
Post by: qwerty on May 23, 2018, 10:53:17 pm
Most times the question is "what does the documentation not say?". So it does not tell that altered elements of the collection still are shown unaltered unless you re-fetch the whole element (and its collections) anew.

q.
Title: Re: Problem with Update method
Post by: bilon on July 25, 2018, 05:41:37 pm
I've contacted sparx, they said it's an issue, it should be fixed sometimes :-) in the future.