Forums

Home » Alloy UI » English

Combination View Flat View Tree View
Threads [ Previous | Next ]
Eric Wright
Display and Refresh Values from the DB
November 16, 2012 5:30 AM
Answer

Eric Wright

Rank: New Member

Posts: 19

Join Date: November 2, 2012

Recent Posts

Hello,

Are there any examples of how to request data using ***LocalServiceUtil, display this value in a porltet, and then have a mechanism in place (Ajax?) to refresh it automatically. E.g. I have a value in my database that stores the current outside temperature, this changes every 5 seconds, and in my portlet I want to display the temperature and have it change dynamically.

Many thanks,
Eric
David H Nebinger
RE: Display and Refresh Values from the DB
November 16, 2012 6:05 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 4496

Join Date: September 1, 2006

Recent Posts

Oops, didn't see this was in the AUI thread, sorry.

Actually I have been thinking about this too. I currently have a timer in place using window.setInterval(), but I'm digging into the AUI code to see if there is a corresponding AUI way to do it.

In the method passed to setInterval(), I'm just calling Liferay.fire() to kick off an event that will trace through to my Vaadin backend code, but I'm betting you could at least trigger the ajax call to the back end either to a service facade that calls XxxLocalServiceUtil or if you have remote services enabled you can call it directly.

Note that if the database is being changed directly, you're going to want to disable caching at the entity level so the service doesn't return stale data.
Eric Wright
RE: Display and Refresh Values from the DB
November 16, 2012 9:45 AM
Answer

Eric Wright

Rank: New Member

Posts: 19

Join Date: November 2, 2012

Recent Posts

Thanks for the response David.

I've come up a little bit of code that uses setInterval as well, it submits a form which has an actionURL as the action and the processAction code behind calls the XxxLocalServiceUtil to update the value (caching has been disabled on entity level). Everything appears to work fine.

However, I'm not sure if calling the actionURL via a form submit is the best way to do things. As I've only got this one portlet on my page it's hard to tell what sort of effect it has, I'm worried that by submitting the form it will refresh other portlets.

 1
 2package com.portlet.test.core;
 3
 4import java.io.IOException;
 5
 6import javax.portlet.ActionRequest;
 7import javax.portlet.ActionResponse;
 8import javax.portlet.PortletConfig;
 9import javax.portlet.PortletContext;
10import javax.portlet.PortletException;
11import javax.portlet.PortletRequestDispatcher;
12import javax.portlet.RenderRequest;
13import javax.portlet.RenderResponse;
14
15import com.portlet.test.service.TestLocalServiceUtil;
16import com.liferay.portal.kernel.exception.PortalException;
17import com.liferay.portal.kernel.exception.SystemException;
18import com.liferay.util.bridges.mvc.MVCPortlet;
19
20public class Weather extends MVCPortlet
21{
22    private String payload;
23   
24    private PortletContext context;
25   
26    @Override
27    public void init(PortletConfig conf) throws PortletException
28    {
29        this.context = conf.getPortletContext();
30    }
31   
32    @Override
33    public void render(RenderRequest req, RenderResponse resp) throws PortletException, IOException
34    {
35        PortletRequestDispatcher rd = this.context.getRequestDispatcher("/view.jsp");
36        rd.include(req, resp);
37    }
38   
39    @Override
40    public void processAction(ActionRequest req, ActionResponse resp) throws PortletException, IOException
41    {
42        try
43        {
44            this.payload = TestLocalServiceUtil.getTest(1).getName();
45        }
46        catch (PortalException e)
47        {
48            //
49        }
50        catch (SystemException e)
51        {
52            //
53        }
54       
55        resp.setRenderParameter("render-param", this.payload);
56    }
57}


 1
 2<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
 3<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui" %>
 4
 5<portlet:defineObjects />
 6
 7<%
 8    String renderParameter = renderRequest.getParameter("render-param");
 9   
10    renderParameter = (renderParameter != null ? renderParameter : "");
11%>
12
13<portlet:actionURL var="actionUrl" />
14
15<aui:script use='aui-io'>
16AUI().ready(function(A)
17{
18    setInterval(function()
19    {
20        A.one('#<portlet:namespace />form').submit();
21
22    }, 3000);
23
24});
25</aui:script>
26
27<form id='<portlet:namespace/>form' action='<%= actionUrl %>' method='post'></form>
28
29<p id='<portlet:namespace/>data'>Value: <%= renderParameter %></p>


I will have a read about Liferay.fire() to see if that will update the portlet data more subtly.

EW
David H Nebinger
RE: Display and Refresh Values from the DB
November 16, 2012 9:56 AM
Answer

David H Nebinger

Rank: Liferay Legend

Posts: 4496

Join Date: September 1, 2006

Recent Posts

Liferay.fire() and Liferay.on() are just browser-side IPC mechanisms. With Vaadin, however, the browser side IPC notification gets automagically handled as a server side event (pretty cool, IMHO).

But I don't think this will help w/ your AJAX-based issue. I'd first solve the AJAX request/response process and, once you have that going, add in the timed refresh.
Hitoshi Ozawa
RE: Display and Refresh Values from the DB
November 16, 2012 10:03 PM
Answer

Hitoshi Ozawa

Rank: Liferay Legend

Posts: 8000

Join Date: March 23, 2010

Recent Posts

If you're going to need near realtime values, saving values into database tables and **LocalServiceUtil may not be a good option. Better to save values in beans to write you own service interface to retrieve values.
Eric Wright
RE: Display and Refresh Values from the DB
November 17, 2012 10:49 AM
Answer

Eric Wright

Rank: New Member

Posts: 19

Join Date: November 2, 2012

Recent Posts

Hitoshi, I really want to try and make use of the XxxLocalServiceUtil for this task.

OK - so with Ajax I can now use an actionURL without refreshing the whole page. However I'm coming back to that same problem where the variable from the MVC portlet code is not being retrieved in the jsp. However, I can see that the value changes with some console outputs in the MVC portlet code.

 1package com.portlet.test.core;
 2
 3import java.io.IOException;
 4
 5import javax.portlet.ActionRequest;
 6import javax.portlet.ActionResponse;
 7import javax.portlet.PortletConfig;
 8import javax.portlet.PortletContext;
 9import javax.portlet.PortletException;
10import javax.portlet.PortletRequestDispatcher;
11import javax.portlet.RenderRequest;
12import javax.portlet.RenderResponse;
13
14import  com.portlet.test.service.TestLocalServiceUtil;
15import com.liferay.portal.kernel.exception.PortalException;
16import com.liferay.portal.kernel.exception.SystemException;
17import com.liferay.util.bridges.mvc.MVCPortlet;
18
19public class Testextends MVCPortlet
20{
21    private PortletContext context;
22   
23    private String value;
24   
25    @Override
26    public void init(PortletConfig conf) throws PortletException
27    {
28        this.context = conf.getPortletContext();
29    }
30   
31    @Override
32    public void render(RenderRequest req, RenderResponse resp) throws PortletException, IOException
33    {
34        System.out.println("render phase: " + req.getAttribute("value-attr"));
35 
36                PortletRequestDispatcher rd = this.context.getRequestDispatcher("/view.jsp");
37                rd.include(req, resp);
38    }
39   
40    @Override
41    public void processAction(ActionRequest req, ActionResponse resp) throws PortletException, IOException
42    {
43        try
44        {
45            this.value = TestLocalServiceUtil.getTest(1).getName();
46        }
47        catch (Exception e)
48        {
49            //
50        }
51       
52        req.setAttribute("value-attr", value);
53       
54        System.out.println("action phase: " + this.value);
55       
56    }
57}


 1<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
 2<%@page import="com.liferay.portal.kernel.portlet.LiferayWindowState"%>
 3
 4<portlet:defineObjects />
 5
 6<portlet:actionURL var="action"  windowState="<%= LiferayWindowState.EXCLUSIVE.toString()%>" />
 7
 8<script type="text/javascript">
 9
10    setInterval(function()
11    {
12        $.ajax(
13        { 
14          type: "POST", 
15          url: '<%= action %>',
16          success: function()
17          { 
18            $('#<portlet:namespace/>data').html("Value: " + "<%= renderRequest.getAttribute("value-attr") %>");
19          } 
20        }); 
21       
22        return false;
23       
24    }, 2000);
25
26</script>
27
28<p id='<portlet:namespace/>data'>Value: </p>


Am I doing something wrong in the processAction / render methods?
Mohamed Rizwanuzaman
RE: Display and Refresh Values from the DB
December 10, 2012 3:09 AM
Answer

Mohamed Rizwanuzaman

Rank: New Member

Posts: 17

Join Date: December 16, 2009

Recent Posts

Hi Eric,
Instead of using action / render method, You can use serveResource method for Ajax call using

<portlet:resourceURL var="ajaxUrl"/>

and you can get the response in in success function of ajax call
 1
 2 A.io.request('<%=renderResponse.encodeURL(ajaxUrl.toString())%>', {
 3          method: 'post',
 4          dataType: 'json',
 5          data: {
 6             groupId: groupId
 7               },
 8          on: { 
 9              success: function() {
10              container.empty()
11                
12                 var url =this.get('responseData').userPhotoUrl ;
13                   var title= this.get('responseData').userFullname;
14                 var dispUrl = this.get('responseData').userdisplayUrl;
15                A.Node.create("<a href='"+dispUrl+"'><img class='picture' src='"+url+"' title='"+title+"'></a>").appendTo(container);
16            
17                 }
18           }
19      });

In action class
1
2    public void serveResource(ResourceRequest resourceRequest,
3            ResourceResponse resourceResponse) throws IOException,
4            PortletException {
5             //logic for getting thevalue from db using XXXLocalServiceUtil.java
6}


after getting the response back you can replace the div to show the latest content from the DB.

Regards,
Rizwan