Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Dhandapani S
count page view and redirect
November 4, 2009 4:32 AM
Answer

Dhandapani S

Rank: Regular Member

Posts: 163

Join Date: March 24, 2009

Recent Posts

Hi,

In my portal application, if non registered user (mean without signin) viewing any four pages , when clicking fifth time the user need to go registration page or some popup window want to display like "Please Register with us" . If any one crossed like this stuff, please carried me out from this issue.

Thanks in advance

Dhans
Rice Owl
RE: count page view and redirect
November 7, 2009 7:17 PM
Answer

Rice Owl

Rank: Regular Member

Posts: 177

Join Date: April 23, 2009

Recent Posts

I'd be interested in something like this too. Have you checked perhaps writing a hook? Not sure if there is one available that could be called on every request. Another option might be a theme - again, I don't have a whole lot of experience with themes, but I know they can be quite powerful and you might be able to write something that way.

If there is no way to do it intrinsically with LifeRay, you might consider writing a custom portlet. This custom portlet can be invisible - ie, no visible HTML elements or text on the JSP page. This portlet can be put on the 4 pages you talk about. Basically, all the portlet does is in the doDispatch() method, it increments a variable you keep on the session if the user is not logged in. Once the variable hits 5, you can do your pop up or redirect the user to a create account or login page.
Apoorva Prakash
RE: count page view and redirect
November 14, 2010 10:33 PM
Answer

Apoorva Prakash

Rank: Liferay Master

Posts: 659

Join Date: June 15, 2010

Recent Posts

Dhandapani S:
Hi,

In my portal application, if non registered user (mean without signin) viewing any four pages , when clicking fifth time the user need to go registration page or some popup window want to display like "Please Register with us" . If any one crossed like this stuff, please carried me out from this issue.

Thanks in advance

Dhans


Hello Dhandapani,

If I am getting your problem correct, then following may help you...

Create a table with the following schema :

ip (varchar)
counter (int)
lastaccessdate (date)
blockstatus (boolean)
currentdate (date)
lastpageaccessed (int)

(I am not using a few attributes currently, but that may implemented as needed).

Create a class in corresponding package :
 1package com.liferay.portal.action;
 2
 3import javax.servlet.http.HttpServletRequest;
 4import javax.servlet.http.HttpServletResponse;
 5
 6import com.liferay.portal.action.service.VisitorCountLocalServiceUtil;
 7import com.liferay.portal.kernel.log.Log;
 8import com.liferay.portal.kernel.log.LogFactoryUtil;
 9import com.liferay.portal.model.Layout;
10import com.liferay.portal.theme.ThemeDisplay;
11
12/**
13 * <a href="CustomLayoutAction.java.html"><b><i>View Source</i></b></a>
14 *
15 * @author Apoorva Prakash
16 *
17 */
18public class CustomLayoutAction extends LayoutAction {
19
20    protected void includeLayoutContent(
21            HttpServletRequest request, HttpServletResponse response,
22            ThemeDisplay themeDisplay, Layout layout)
23        throws Exception {
24        super.includeLayoutContent(request, response, themeDisplay, layout);
25        String remoteAddr = request.getRemoteAddr();
26        boolean status=VisitorCountLocalServiceUtil.check(remoteAddr);
27        int plid=(int)themeDisplay.getPlid();
28        themeDisplay.getURLSignIn();
29        if(status){
30            VisitorCountLocalServiceUtil.updatePageCounter(remoteAddr,plid);
31        }
32        else{
33            VisitorCountLocalServiceUtil.addNewVisitor(remoteAddr,plid);
34        }
35       
36    }
37    private static Log _log = LogFactoryUtil.getLog(LayoutAction.class);
38
39}


write a service builder as follows:
 1<?xml version="1.0"?>
 2<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 5.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_5_2_0.dtd">
 3
 4<service-builder package-path="com.liferay.portal.action">
 5    <namespace>VisitorCount</namespace>
 6    <entity name="VisitorCount" table="visitorcount" local-service="true" remote-service="true">
 7
 8        <!-- PK fields -->
 9
10        <column name="ip" type="String" primary="true" />
11
12        <!-- Audit fields -->
13
14        <column name="counter" type="int" />
15        <column name="lastaccessdate" type="Date" />
16        <column name="blockstatus" type="boolean"/>
17        <column name="currentdate" type="Date" />
18        <column name="lastpageaccessed" type="int" /> 
19            
20    </entity>
21</service-builder>


Build this service, and in VisitorCountLocalServiceImpl.java, write the following code:
 1package com.liferay.portal.action.service.impl;
 2
 3import java.util.Date;
 4
 5import com.liferay.portal.action.model.VisitorCount;
 6import com.liferay.portal.action.service.base.VisitorCountLocalServiceBaseImpl;
 7import com.liferay.portal.action.service.persistence.VisitorCountUtil;
 8/**
 9 * <a href="VisitorCountLocalServiceBaseImpl.java.html"><b><i>View Source</i></b></a>
10 *
11 * @author Apoorva Prakash
12 *
13 */
14
15public class VisitorCountLocalServiceImpl  extends VisitorCountLocalServiceBaseImpl {
16   
17    public boolean check(String ip){
18        try{
19            VisitorCount vc=VisitorCountUtil.findByPrimaryKey(ip);
20            if(vc!=null){
21                return true;
22            }
23        }
24        catch(Exception e){
25            //e.printStackTrace();
26        }
27        return false;
28    }
29   
30    public boolean updatePageCounter(String ip, int plid){
31        boolean status=false;
32        try{
33
34            VisitorCount vc=VisitorCountUtil.findByPrimaryKey(ip);
35            int counter=vc.getCounter();
36                        if(vc.getBlockStatus)
37                        {
38                               //  write some code for redirection
39                        }
40            int oldPlid=vc.getLastpageaccessed();
41            if(oldPlid!=plid){
42                counter++;
43                vc.setCounter(counter);
44                vc.setLastpageaccessed(plid);
45                if(counter==5){
46                    vc.setBlockstatus(true);
47                }
48                VisitorCountUtil.update(vc,true);
49                status=true;
50            }
51            
52        }
53        catch(Exception e){
54            e.printStackTrace();
55        }
56        return status;
57    }
58   
59    public boolean addNewVisitor (String ip, int plid){
60        boolean status=false;
61        try{
62            VisitorCount vc=VisitorCountUtil.create(ip);
63            vc.setIp(ip);
64            vc.setCounter(1);
65            vc.setBlockstatus(false);
66            vc.setLastpageaccessed(plid);
67            VisitorCountUtil.update(vc,true);
68            status=true;
69        }
70        catch(Exception e){
71            e.printStackTrace();
72        }
73        return status;
74    }
75}
deploy and run...

I hope this will be helpful...

Thanks and Regards...
dave ch
RE: count page view and redirect
June 18, 2012 4:25 AM
Answer

dave ch

Rank: Regular Member

Posts: 164

Join Date: February 6, 2012

Recent Posts

Hi Apoorva,

I am not getting about your LayoutAction() method. Confused about where to implement this method since getting the following error.

Buildfile: E:\Liferay 6.1\liferay-plugins-sdk-6.1.0\portlets\countvisitors-portlet\build.xml
compile:
merge:
compile-java:
Compiling 1 source file to E:\Liferay 6.1\liferay-plugins-sdk-6.1.0\portlets\countvisitors-portlet\docroot\WEB-INF\classes
----------
1. ERROR in E:\Liferay 6.1\liferay-plugins-sdk-6.1.0\portlets\countvisitors-portlet\docroot\WEB-INF\src\com\liferay\portal\action\CustomLayoutAction.java (at line 18)
public class CustomLayoutAction extends LayoutAction {
^^^^^^^^^^^^
LayoutAction cannot be resolved to a type
----------
2. ERROR in E:\Liferay 6.1\liferay-plugins-sdk-6.1.0\portlets\countvisitors-portlet\docroot\WEB-INF\src\com\liferay\portal\action\CustomLayoutAction.java (at line 24)
super.includeLayoutContent(request, response, themeDisplay, layout);
^^^^^
LayoutAction cannot be resolved to a type
----------
3. ERROR in E:\Liferay 6.1\liferay-plugins-sdk-6.1.0\portlets\countvisitors-portlet\docroot\WEB-INF\src\com\liferay\portal\action\CustomLayoutAction.java (at line 37)
private static Log _log = LogFactoryUtil.getLog(LayoutAction.class);
^^^^^^^^^^^^
LayoutAction cannot be resolved to a type
----------
3 problems (3 errors)

BUILD FAILED
Shivam Aggarwal
RE: count page view and redirect
December 12, 2012 10:24 PM
Answer

Shivam Aggarwal

Rank: Junior Member

Posts: 61

Join Date: January 17, 2012

Recent Posts

Hi Apoorva,
I was trying to implement the solution you provided.Regarding the CustomLayoutAction class,I used a hook for it.
But on deployment,following exception is thrown
1
2Caused by: java.lang.NoClassDefFoundError: com/liferay/portal/action/LayoutAction
3
4Caused by: java.lang.ClassNotFoundException: com.liferay.portal.action.LayoutAction
5    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1688)


Kindly elaborate on the same.Is it fine to implement the same class using hook or are we supposed to extend any subclass of the same
Apoorva Prakash
RE: count page view and redirect
December 13, 2012 9:14 AM
Answer

Apoorva Prakash

Rank: Liferay Master

Posts: 659

Join Date: June 15, 2010

Recent Posts

shivam aggarwal:
Hi Apoorva,
I was trying to implement the solution you provided.Regarding the CustomLayoutAction class,I used a hook for it.
But on deployment,following exception is thrown
1
2Caused by: java.lang.NoClassDefFoundError: com/liferay/portal/action/LayoutAction
3
4Caused by: java.lang.ClassNotFoundException: com.liferay.portal.action.LayoutAction
5    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1688)


Kindly elaborate on the same.Is it fine to implement the same class using hook or are we supposed to extend any subclass of the same



Hello Shivam,,

It seems it is unable to find the class..

Try implementing the same solution using this approach.

Else you can try the same with EXT -plugin also.

Hope this will help,

Thanks and Regards,
Apoorva Prakash
Mika Koivisto
RE: count page view and redirect
December 13, 2012 10:14 AM
Answer

Mika Koivisto

LIFERAY STAFF

Rank: Liferay Legend

Posts: 1505

Join Date: August 7, 2006

Recent Posts

Struts action hooks can't extend any struts action classes from the portal. For example following won't work because LayoutAction is not visible to the hook.
1public class MyCustomAction extends LayoutAction {
2...
3}


You need to think that the struts action hook is more like a Aspect that wraps the call to the original struts action. It is not a real struts action and won't have all the features of a real struts action.
Shivam Aggarwal
RE: count page view and redirect
December 13, 2012 8:15 PM
Answer

Shivam Aggarwal

Rank: Junior Member

Posts: 61

Join Date: January 17, 2012

Recent Posts

Hi Apoorva,
So If am getting it right,what you suggest is that LayoutAction class which is not available to the class loader for a hook,will be available to be extended in an ext-plugin
Hari Dobbala
RE: count page view and redirect
December 13, 2012 9:15 PM
Answer

Hari Dobbala

Rank: Junior Member

Posts: 29

Join Date: November 17, 2012

Recent Posts

Hi Shivam,

Yes. LayoutAction class would not available to the class loader for a hook. But it would be available if you use extension environment i.e ext-plugin.
Apoorva Prakash
RE: count page view and redirect
December 13, 2012 11:55 PM
Answer

Apoorva Prakash

Rank: Liferay Master

Posts: 659

Join Date: June 15, 2010

Recent Posts

shivam aggarwal:
Hi Apoorva,
So If am getting it right,what you suggest is that LayoutAction class which is not available to the class loader for a hook,will be available to be extended in an ext-plugin



Yes, Shivan, try implementing through EXT-plugin...