Fórumok

count page view and redirect

thumbnail
Dhandapani Shanmugam, módosítva 14 év-val korábban

count page view and redirect

Regular Member Bejegyzések: 176 Csatlakozás dátuma: 2009.03.24. Legújabb bejegyzések
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, módosítva 14 év-val korábban

RE: count page view and redirect

Regular Member Bejegyzések: 177 Csatlakozás dátuma: 2009.04.24. Legújabb bejegyzések
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.
thumbnail
Apoorva Prakash, módosítva 13 év-val korábban

RE: count page view and redirect

Liferay Master Bejegyzések: 658 Csatlakozás dátuma: 2010.06.15. Legújabb bejegyzések
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) [PRI]
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 :
package com.liferay.portal.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.liferay.portal.action.service.VisitorCountLocalServiceUtil;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.model.Layout;
import com.liferay.portal.theme.ThemeDisplay;

/**
 * <a href="CustomLayoutAction.java.html"><b><i>View Source</i></b></a>
 *
 * @author Apoorva Prakash
 *
 */
public class CustomLayoutAction extends LayoutAction {

	protected void includeLayoutContent(
			HttpServletRequest request, HttpServletResponse response,
			ThemeDisplay themeDisplay, Layout layout)
		throws Exception {
		super.includeLayoutContent(request, response, themeDisplay, layout);
		String remoteAddr = request.getRemoteAddr();
		boolean status=VisitorCountLocalServiceUtil.check(remoteAddr);
		int plid=(int)themeDisplay.getPlid();
		themeDisplay.getURLSignIn();
		if(status){
			VisitorCountLocalServiceUtil.updatePageCounter(remoteAddr,plid);
		}
		else{
			VisitorCountLocalServiceUtil.addNewVisitor(remoteAddr,plid);
		}
		
	}
	private static Log _log = LogFactoryUtil.getLog(LayoutAction.class);

}


write a service builder as follows:
<!--?xml version="1.0"?-->


<service-builder package-path="com.liferay.portal.action">
	<namespace>VisitorCount</namespace>
	<entity name="VisitorCount" table="visitorcount" local-service="true" remote-service="true">

		<!-- PK fields -->

		<column name="ip" type="String" primary="true" />

		<!-- Audit fields -->

		<column name="counter" type="int" />
		<column name="lastaccessdate" type="Date" />
		<column name="blockstatus" type="boolean" />
		<column name="currentdate" type="Date" />
		<column name="lastpageaccessed" type="int" />  
			
	</entity>
</service-builder>


Build this service, and in VisitorCountLocalServiceImpl.java, write the following code:
package com.liferay.portal.action.service.impl;

import java.util.Date;

import com.liferay.portal.action.model.VisitorCount;
import com.liferay.portal.action.service.base.VisitorCountLocalServiceBaseImpl;
import com.liferay.portal.action.service.persistence.VisitorCountUtil;
/**
 * <a href="VisitorCountLocalServiceBaseImpl.java.html"><b><i>View Source</i></b></a>
 *
 * @author Apoorva Prakash
 *
 */

public class VisitorCountLocalServiceImpl  extends VisitorCountLocalServiceBaseImpl {
	
	public boolean check(String ip){
		try{
			VisitorCount vc=VisitorCountUtil.findByPrimaryKey(ip);
			if(vc!=null){
				return true;
			}
		}
		catch(Exception e){
			//e.printStackTrace();
		}
		return false;
	}
	
	public boolean updatePageCounter(String ip, int plid){
		boolean status=false;
		try{

			VisitorCount vc=VisitorCountUtil.findByPrimaryKey(ip);
			int counter=vc.getCounter();
                        if(vc.getBlockStatus)
                        {
                               //  write some code for redirection
                        }
			int oldPlid=vc.getLastpageaccessed();
			if(oldPlid!=plid){
				counter++;
				vc.setCounter(counter);
				vc.setLastpageaccessed(plid);
				if(counter==5){
					vc.setBlockstatus(true);
				}
				VisitorCountUtil.update(vc,true);
				status=true;
			}
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return status;
	}
	
	public boolean addNewVisitor (String ip, int plid){
		boolean status=false;
		try{
			VisitorCount vc=VisitorCountUtil.create(ip);
			vc.setIp(ip);
			vc.setCounter(1);
			vc.setBlockstatus(false);
			vc.setLastpageaccessed(plid);
			VisitorCountUtil.update(vc,true);
			status=true;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return status;
	}
}
deploy and run...

I hope this will be helpful...

Thanks and Regards...
thumbnail
dave ch, módosítva 11 év-val korábban

RE: count page view and redirect

Regular Member Bejegyzések: 161 Csatlakozás dátuma: 2012.02.07. Legújabb bejegyzések
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:
[javac] Compiling 1 source file to E:\Liferay 6.1\liferay-plugins-sdk-6.1.0\portlets\countvisitors-portlet\docroot\WEB-INF\classes
[javac] ----------
[javac] 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)
[javac] public class CustomLayoutAction extends LayoutAction {
[javac] ^^^^^^^^^^^^
[javac] LayoutAction cannot be resolved to a type
[javac] ----------
[javac] 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)
[javac] super.includeLayoutContent(request, response, themeDisplay, layout);
[javac] ^^^^^
[javac] LayoutAction cannot be resolved to a type
[javac] ----------
[javac] 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)
[javac] private static Log _log = LogFactoryUtil.getLog(LayoutAction.class);
[javac] ^^^^^^^^^^^^
[javac] LayoutAction cannot be resolved to a type
[javac] ----------
[javac] 3 problems (3 errors)

BUILD FAILED
thumbnail
shivam aggarwal, módosítva 11 év-val korábban

RE: count page view and redirect

Regular Member Bejegyzések: 122 Csatlakozás dátuma: 2012.01.18. Legújabb bejegyzések
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

Caused by: java.lang.NoClassDefFoundError: com/liferay/portal/action/LayoutAction

Caused by: java.lang.ClassNotFoundException: com.liferay.portal.action.LayoutAction
	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
thumbnail
Apoorva Prakash, módosítva 11 év-val korábban

RE: count page view and redirect

Liferay Master Bejegyzések: 658 Csatlakozás dátuma: 2010.06.15. Legújabb bejegyzések
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

Caused by: java.lang.NoClassDefFoundError: com/liferay/portal/action/LayoutAction

Caused by: java.lang.ClassNotFoundException: com.liferay.portal.action.LayoutAction
	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
thumbnail
shivam aggarwal, módosítva 11 év-val korábban

RE: count page view and redirect

Regular Member Bejegyzések: 122 Csatlakozás dátuma: 2012.01.18. Legújabb bejegyzések
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
thumbnail
Hari Dobbala, módosítva 11 év-val korábban

RE: count page view and redirect

Junior Member Bejegyzések: 29 Csatlakozás dátuma: 2012.11.18. Legújabb bejegyzések
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.
thumbnail
Apoorva Prakash, módosítva 11 év-val korábban

RE: count page view and redirect

Liferay Master Bejegyzések: 658 Csatlakozás dátuma: 2010.06.15. Legújabb bejegyzések
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...
thumbnail
Mika Koivisto, módosítva 11 év-val korábban

RE: count page view and redirect

Liferay Legend Bejegyzések: 1519 Csatlakozás dátuma: 2006.08.07. Legújabb bejegyzések
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.
public class MyCustomAction extends LayoutAction {
...
}


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.