« Back

Most Visited Pages Count in Liferay 6.1

General Blogs June 28, 2013 By priti parmar

Liferay 6.1 provides Custom Fileds for Pages , set viewCount as a Custom Field and update it using a servicePreAction Hook.

public class CustomServicePreAction extends Action {

    /**
     * This Method is used for Creating the Map Variable of the Pages
     *
     * @param HttpServletRequest
     * @param HttpServletResponse
     */
    @Override
    public void run(HttpServletRequest req, HttpServletResponse res)
            throws ActionException {
              ThemeDisplay themeDisplay = (ThemeDisplay) req
                .getAttribute(WebKeys.THEME_DISPLAY);
        if (!themeDisplay.getURLCurrent().contains("control_panel")) {
            Layout layout = themeDisplay.getLayout();
            try {
                    setupExpandos(layout);
                } catch (Exception e) {
                  
                }
            }
    
    }

    protected void setupExpandos(Layout layout) throws Exception {
        
        
        ExpandoTable table = null;
        try {
            table = ExpandoTableLocalServiceUtil.addTable(layout.getCompanyId(), Layout.class.getName(),ExpandoTableConstants.DEFAULT_TABLE_NAME);
        } catch (DuplicateTableNameException dtne) {
            table = ExpandoTableLocalServiceUtil.getTable(layout.getCompanyId(), Layout.class.getName(),ExpandoTableConstants.DEFAULT_TABLE_NAME);
        }
         String column = "viewCount";
         ExpandoColumn expcolumn = null;
         try {
              expcolumn = ExpandoColumnLocalServiceUtil.addColumn(table.getTableId(), column,ExpandoColumnConstants.LONG,0L);
         }
         catch (DuplicateColumnNameException dcne)
         {
               expcolumn =        ExpandoColumnLocalServiceUtil.getColumn(layout.getCompanyId(),PortalUtil.getClassNameId(Layout.class.getName()), table.getName(), column);
         }
        long count = Long.valueOf(ExpandoValueLocalServiceUtil.getData(layout.getCompanyId(),Layout.class.getName(),      table.getName(), expcolumn.getName() , layout.getPrimaryKey(),0L));
        ExpandoValueLocalServiceUtil.addValue(layout.getCompanyId(),Layout.class.getName(), table.getName(), expcolumn.getName() , layout.getPrimaryKey(),count+1L);

    }

  
}

Threaded Replies Author Date
[...] 1 Jul 2013 Posted in: Liferay, Tips,... Anonymous July 1, 2013 2:30 AM
This approach may hit the performance. There... Jitendra Rajput July 8, 2013 6:40 AM
TagsAssetLocalServiceUtil.incrementViewCounter(... priti parmar July 10, 2013 4:33 AM

[...] 1 Jul 2013 Posted in: Liferay, Tips, Tutorials By wasimshaikh No Comments Liferay Roundup : version control, most visited page count in liferay 6.1, mobile device detection in liferay Liferay... [...] Read More
Posted on 7/1/13 2:30 AM.
This approach may hit the performance. There should be some thing different. For asset publisher Liferay is using TagsAssetLocalServiceUtil.incrementViewCounter() to track visits for assets. I guess similar we can implement by hooking layout portlet.
Posted on 7/8/13 6:40 AM.
TagsAssetLocalServiceUtil.incrementViewCounter() will use only when we assign some tag to the page. AssetPublisher dosent have any entry for page view count in Database , so we have to save this count somewhere in DB .
Posted on 7/10/13 4:33 AM in reply to Jitendra Rajput.