0

By default, OpenXava uses the same view for creating a new entity and for updating an existing one. Is there a way to have a different view for each case?

javierpaniza
  • 677
  • 4
  • 10

1 Answers1

0

Obiously you must define a view for creating and another for updating, thus:

@View(name="Create", members=" ... ")
@View(name="Update", members=" ... ")
public class MyEntity { ...

Now you have to refine the new action for chosing the Create view and the searching action for chosing the Update view. First, define your own controller, in your controllers.xml, thus:

<controller name="MyTypical">
    <extends controller="Typical"/>
    
    <action  name="new" 
        class="com.mycompany.myapplication.actions.MyNewAction"
        image="new.gif"
        icon="library-plus"
        keystroke="Control N"
        loses-changed-data="true">
        <set property="restoreModel" value="true"/>         
    </action>

    <action name="search" hidden="true"         
        class="com.mycompany.myapplication.actions.MySearchAction"/>
        
</controller>

And now assign this controller to your module, and define the searching action for the module. Write your module in the application.xml in this way:

<module name="MyModule">
    <env-var name="XAVA_SEARCH_ACTION" value="MyTypical.search"/>
    <model name="MyEntity"/>
    <controller name="MyTypical"/>
</module>

And now only remains to refine the logic of your actions. For MySearchAction you write (NOTE: selected entity index values have to be captured (1) so that it might be restored (3) after the reset performed by setViewName(...) method (2)):

public class MySearchAction extends SearchByViewKeyAction {

    public void execute() throws Exception {
        Map mapIndexValues = getView().getKeyValuesWithValue();   //1
        getView().setViewName("Update");                          //2
        getView().setValues(mapIndexValues);                      //3
        super.execute();
    }

}

And for MyNewAction:

public class MyNewAction extends NewAction {

    public void execute() throws Exception {
        getView().setViewName("Create");
        super.execute();
    }

}
javierpaniza
  • 677
  • 4
  • 10
  • in my experience, after restoring the values from mapIndexValues I had to add a: setView().setValue("changed-field-name",value); – Massimo Coletti Aug 29 '23 at 17:06