Menu

How to retrieve session object at the entity's property?

2017-03-23
2017-03-23
  • Brian Cheong

    Brian Cheong - 2017-03-23

    Hi all,

    I am still figuring the way on retrieving session object after photo/file upload.
    I googled on "How to retrieve session object in Java" and it shows me session.getAttribute(...). Is there any way to retrieve the session object in openxava with that concept?

    My scenario is, I have set an id 155 to uId. How can I retrieve the uId 155 and display it at detail mode??

    Thanks in advance.

    Brian

     

    Last edit: Brian Cheong 2017-03-23
  • Brian Cheong

    Brian Cheong - 2017-03-24

    I want to do like this:

    The first time user direct to the detail page, it has the url parameter value of uId=155, which the uId will set into session object. After that, all pages will parse the session object. I tried with the session that I have right now but not working like what I want. Really need some please help on this.

     
  • Shankar

    Shankar - 2017-03-24

    Store the uid in a session object. Over the SaveAction of the entity and inject the session object in your class extending the SaveAction. By this way you will have access to the session object and you can do further processing like save the uid to database

    Thanks

    Shankar

     
  • Brian Cheong

    Brian Cheong - 2017-03-24

    Hi Shankar,

    Thanks for the reply.

    Store the uid in a session object

    I believe that I did store the uid in session object with this:

      public class OnEachRequestFilteredTabAction extends ViewBaseAction {
    
    @Inject @Named("application_uId")
    private String uId;
    
    @Inject
    private Tab tab;
    
    @Override
    public void execute() throws Exception {
    
        uId = getRequest().getParameter("UId");
        //getView().setValue("uId", uId);
        if (!Is.empty(uId)) tab.setBaseCondition("${uId}='" + uId + "'");
        else tab.setBaseCondition("${uId}='ERROR'");
    }
    
    }
    

    Over the SaveAction of the entity and inject the session object in your class extending the SaveAction

    Do you mean that the ViewBaseAction should be replaced by SaveAction?

    Please correct me if I am wrong on this. I am really headache on session part.

    Thanks in advance.

    Brian

     
  • Shankar

    Shankar - 2017-03-24

    I think you are using the above class to filter based on uid. Leava it as it.

    Have another class extending the SaveAction

    public class MySaveAction extends SaveAction {

    @Inject @Named("application_uId")
    private String uId;

    @Override
    public void execute() throws Exception {
    
        getview().setValue("uid",uid);
    
        super.execute();
    }
    

    }

     
  • Brian Cheong

    Brian Cheong - 2017-03-24

    Okay thanks. Now should I change the controller of SaveAction from class="org.openxava.actions.SaveAction" to class="org.survey.actions.MySaveAction"? And I get this error: Impossible to assign values to object of class Randau because Impossible to set object properties because argument type mismatch

     
  • Shankar

    Shankar - 2017-03-25

    Yes you need to add a controller for you saveaction and add it to the module in application.xml

    <controller name="YourSaveAction">
    <extends controller="Typical"/>
    <action name="save" mode="detail" by-default="if-possible" class="org.openxava.org.action.YourSaveAction" image="images/save.gif" keystroke="Control S"/>
    </controller>

    <module name="YourModule">
    <model name="YourModule"/>
    <controller name="YourSaveAction"/>
    </module>

    Not clear from your message when you are getting the exception.

    One other way to do this is to

    1. Inject the session object in the model class
    2. in @PreCreate method set the value of uid by calling the setter method to set the value of uid in the object to be saved.

    @PreCreate
    public void onPrecreate() throws Exception{

    }
    

    Thanks

    Shankar

     
  • Brian Cheong

    Brian Cheong - 2017-03-27

    I added the controller in controller.xml and the module in application.xml. Now i got this error:

    SEVERE: Impossible to set value to property uId of action save
    org.openxava.util.XavaException: Impossible to set value to property uId of action save
    

    At my Randau.java.

    I have the property for uId:

    @Hidden
    @ReadOnly
    @Column(name="USER_ID", length=10)
    private int uId;
    
    ...
    
    getter and setter for uId
    

    Did I miss something for the property?

     

    Last edit: Brian Cheong 2017-03-27
  • Brian Cheong

    Brian Cheong - 2017-03-27

    I solved the error above by changing data type "int" to "String". But the uId is not saved into the database.

    By the way, I would like to try the second method on @PreCreate. Would you mind to give more details on it?

    Inject the session object in the model class

    Okay. My model class is Randau.java, isn't that it is not encouraged to use @Inject on the property?

    in @PreCreate method set the value of uid by calling the setter method to set the value of uid in the object to be saved.

    Is this correct if I do it like this?

    @Hidden
    @ReadOnly
    @Column(name="USER_ID", length=10)
    private String uId;
    
    @PreCreate
    public void onPreCreate() throws Exception{
    
    //Call the setter method here??
    public void setUId(String uId) {
        this.uId = uId;
    }
    
    }
    

    Please guide me as I am not good on this. Thanks alot Shankar.

    Brian

     

    Last edit: Brian Cheong 2017-03-27
  • Shankar

    Shankar - 2017-03-27

    The second option was just a thought not sure if it advisable to do it or not.

    The first option of injectecting the session object in the SaveActionc class and set the value of uid should work.

    You need to debug your code to see it the value is being set in th entity object.

    Thanks

    Shankar

     
  • Brian Cheong

    Brian Cheong - 2017-03-27

    You need to debug your code to see it the value is being set in th entity object.

    The uId value does not set in the column. Do I need to add a line to call the MySaveAction class? Currently, I have:

    @Hidden
    @ReadOnly
    @Column(name="USER_ID", length=10)
    private String uId;
    
     

    Last edit: Brian Cheong 2017-03-27
  • Shankar

    Shankar - 2017-03-27

    You don't need to call the action class explicitly.

    You have to configure an entry in the controller for the SaveAction class and use the controller in the module defintion in application.xml

     
  • Brian Cheong

    Brian Cheong - 2017-03-28

    Hi Shankar,

    I will try to find the solution in other way.

    Thanks.

    Brian

     

    Last edit: Brian Cheong 2017-03-28

Log in to post a comment.