Book a Demo

Author Topic: Object Model : java help  (Read 4216 times)

Orinoco

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Object Model : java help
« on: March 06, 2008, 06:12:30 pm »
Hi folks

I'm making first steps into using the object model with java. I've got the repository opening and have successfully iterated through the namespace of my project file.
I can't seem to make changes though. Here's a simple example, root is the top Package of the model.
Code: [Select]
public static void nameChange(){
            
            String PIMid = findPIMGUID(root);
            System.out.println("elem name: " +                                                    repos.GetElementByGuid(PIMid).GetName() +
                      "\nGUID:" +
                       PIMid
                       + "\n");
            
            repos.GetElementByGuid(PIMid).SetName("newName");
            repos.GetModels().Refresh();
            
            System.out.println("elem name: " +
                        repos.GetElementByGuid(PIMid).GetName() +
                        "\nGUID:" +
                        PIMid);
            

      }
      

this is the output:

elem name: PIM
GUID:{495F584B-862E-4868-9453-486E88B49308}

elem name: PIM
GUID:{495F584B-862E-4868-9453-486E88B49308}

I was expecting the second elem name to be "newName"

any thoughts much appreciated

cheers

Mark
« Last Edit: March 06, 2008, 06:16:55 pm by markalanhopkins »

paphko

  • EA Novice
  • *
  • Posts: 15
  • Karma: +0/-0
    • View Profile
Re: Object Model : java help
« Reply #1 on: March 06, 2008, 09:29:02 pm »
Hi Mark,

you need to call Update() on the element you changed. Try this (untested):

Code: [Select]
...
Element e = repos.GetElementByGuid(PIMid);
e.SetName("newName");
e.Update();
...
« Last Edit: March 06, 2008, 09:30:16 pm by paphko »

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Object Model : java help
« Reply #2 on: March 06, 2008, 11:35:06 pm »
Hi Mark,

Yes, that's what was missing.

[NB: I refer to elements below. The concepts are valid for connectors and other 'things' within EA.]

Update() causes changes to an element (or a newly created element in its entirety) to be written to the schema. Until you do this any changes you've made are unavailable. You can test this be exiting from the changes before calling Update() and then reading the element. No changes (or no element at all if it was newly created).

You also used the Refresh() method. This is related but different. Once you call Update() the element is updated in the repository - the underlying EA database. In the case of a newly created element, you can now reference it by GUID, ID or whatever. However, if the element (or whatever) is part of a collection, you won't be able to find it there. Trying to iterate through the collection will fail, as if the element was never added. The Refresh() method updates the collection, so that the new element is added, the element count is updated, and methods that target the collection can 'see' the element. The same holds true for delete operations. If you change the element name you might need to Refresh() the collection, depending on whether you want to retrieve the element by name.

Remember that you call Update() on the element, but Refresh() on the collection that holds the element (i.e. this collection is an attribute of the element's parent).

Hopefully this clarifies things more than it clouds them.

David
No, you can't have it!

Orinoco

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Object Model : java help
« Reply #3 on: March 07, 2008, 12:35:37 pm »
Thanks guys

I go the first bit working and successfully changed the name (visible when   elem called by GUID) using elem.update(). As you predicted, iterating through shows the old name.

I can't get the refresh working though. Tried with a more extreme example too and all the refreshes I could think of:

Code: [Select]
public static void deletePSM1(){
      
      listMDA();
      
      Package MDA = repos.GetPackageByGuid(findMDAGUID(root));
      Package PSM1 = repos.GetPackageByGuid(findPSM1GUID(MDA));
      
      PSM1.destroy();
        PSM1.update();
      
      MDA.GetPackages().Refresh();
      repos.GetModels().Refresh();
      MDA.GetElements().Refresh();
      root.GetPackages().Refresh();
      root.GetElements().Refresh();
      MDA.Update();
      
      listMDA();
}

result:

PIM
PSM1
PSM2

PIM
PSM1
PSM2


A note on my setup: I'm using Eclipse and calling the static methods from main, not deploying these java files as an add-in. Don't know if this makes a difference? Having the referenced EA file running/not running doesn't seem to make a difference.

cheers

Mark

« Last Edit: March 07, 2008, 12:41:54 pm by markalanhopkins »

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Object Model : java help
« Reply #4 on: March 07, 2008, 03:58:21 pm »
I don't know about the destroy function, but I'm pretty sure it's not part of the EA automation interface.  (General java object function maybe?)

To remove a package you need to call RemoveAt() from the parent package Packages collection.

Orinoco

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Object Model : java help
« Reply #5 on: March 07, 2008, 04:23:44 pm »
Thanks all

Got it working:

Code: [Select]
public static void renamePSM1(){
      
      listMDA();
      
      Package MDA = repos.GetPackageByGuid(findMDAGUID(root));
      Package PSM1 = repos.GetPackageByGuid(findPSM1GUID(MDA));
      
      PSM1.SetName("newName");
      PSM1.Update();
      
      //MDA.GetPackages().Refresh();
      //repos.GetModels().Refresh();
      //MDA.GetElements().Refresh();
      //root.GetPackages().Refresh();
      //root.GetElements().Refresh();
      //MDA.Update();
      
      listMDA();
}

looks like the update is all that is needed in this case.

thanks again

Mark