0

How can I access the details element of a collection entity which is inside one section of another entity with openxava? For example, in the view of entity A, we have section {S1,S2,S3} and inside section S3 view, we have {collection of entity B}. Now I want to access the detail element of entity B, so that i can fill the element in an action controller. How do I do that?

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85

3 Answers3

1

Get the collection directly from the view, in this way:

Collection myCollection = getView().getSubview("myCollection").getCollectionObjects();

It must work even with oldest OpenXava versions

Dani
  • 3,744
  • 4
  • 27
  • 35
javierpaniza
  • 677
  • 4
  • 10
0

You can do it in several ways. Here you have one, I have used it with some references that I want to modify from inside of an action called by the base module (which should work with your collection):

Query q = XPersistence.getManager().createQuery("JPQL QUERY TO RETRIVE THE COLLECTION WITH :parameterIfNeeded");
q.setParameter("parameterIfNeeded", "value");
List entityBList = q.getResultList();
if (getView().getModelName().equalsIgnoreCase("yourBaseModelViewName")) {
    getView().getSubview("yourSubViewName").setModel(entityBList);
    getView().getSubview("yourSubViewName").refresh();
}

You must to be using OX 4.6 to be able to use setModel(). And remember that the "yourSubViewName" is the name of the property for your collection into the base model.

I have not tested that code with a collection, so make the adjustments according to your needs, maybe you will need to CAST the query result list or something.

Dharman
  • 30,962
  • 25
  • 85
  • 135
HayrolR
  • 761
  • 5
  • 9
  • thanks for the quick answer.Since im using openxava 3.1, when i try the following code: getView().getSubview(); im getting error that i do not have a subview. any idea??? so how can your code work?? getView().getSubview("entityB_view").setModel(enityBlist); – user1943998 Jan 04 '13 at 22:05
  • I started with OX version 4.x, so I don't know if getView().getSubview() was available in your old version. So, what do you mean when you wrote: "im getting error that i do not have a subview"?... do you mean that the .Subview() method is not available for the View class? ... or do you mean that you got a error saying that: the subview you trying to access has not been found?, if your error is the last I mentioned, then maybe it's because the name of the subview you wrote (entityB_view) is maybe the name of the sub section that is part of the base View, not the property name for the collection – HayrolR Jan 06 '13 at 04:51
  • So, try changing "entityB_view" to the name of the property which represent the collection in your base class model (base Entity), that was I tried to explain before. I'm almost sure that "entityB_view" si not a property in your base Entity class (base model). – HayrolR Jan 06 '13 at 04:53
0

Obtain the entity associated to the view and get the collection from it. Since OpenXava 4.3 you can do it in this way:

MyEntity myEntity = (MyEntity) getView().getEntity();   
Collection myCollection = myEntity.getMyCollection();

If you're using an OX previous to 4.3 do it in this way:

Map keyValues = getView().getKeyValuesWithValue();
if (!keyValues.isEmpty()) {
    MyEntity myEntity = (MyEntity) 
        MapFacade.findEntity(getView().getModelName(), keyValues);
    Collection myCollection = myEntity.getMyCollection();
}
Dani
  • 3,744
  • 4
  • 27
  • 35
javierpaniza
  • 677
  • 4
  • 10