Foren

Event Listener and NOT a Model Listener

thumbnail
MANOVINAYAK AYYAPPAN, geändert vor 12 Jahren.

Event Listener and NOT a Model Listener

Regular Member Beiträge: 131 Beitrittsdatum: 13.06.11 Neueste Beiträge
Hi All,

I am currently working on event listeners for my project.
One requirement that I have is that of detecting Login and Logout and recording each of these events time stamps.

I tried to create a Hook by oevrriding "login.events.post" Event Class with that of mine.
But was unsuccessful due to the following reasons:
1. I cloned LoginPostAction and tried to add my own methods to record the Login time stamp.
2. But when I deployed I got many errors such as PropsValues NOT a defined symbol and this was due to its presence in the portal-impl and you can't reach those classes from your plugins.

Now I do not know how to capture the Login and Logout time stamps. Although the Login Time Stamp is available in user_ table but where can I find the logout timestamp or how do I record it.

Any ideas on this would be of great help to meemoticon.

Thanks and Regards,
Mano
thumbnail
Ravi Kumar Gupta, geändert vor 12 Jahren.

RE: Event Listener and NOT a Model Listener

Liferay Legend Beiträge: 1302 Beitrittsdatum: 24.06.09 Neueste Beiträge
You can use propsutil from portal-kernel.jar
thumbnail
MANOVINAYAK AYYAPPAN, geändert vor 12 Jahren.

RE: Event Listener and NOT a Model Listener

Regular Member Beiträge: 131 Beitrittsdatum: 13.06.11 Neueste Beiträge
Thanks a lot Ravi emoticon!!!

But I have many such classes which are not reachable from a hook.

Below is a set of classes that could NOT be imported during build and as a result build fails.

import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.PrefsPropsUtil;
import com.liferay.portal.util.PropsValues;
import org.apache.struts.Globals;

It would be great, If we have a clean solution something like a Listener for the event, instead or cloning and re-writing the event and messing up liferay code.

Thanks and Regards,
Mano
thumbnail
Ravi Kumar Gupta, geändert vor 12 Jahren.

RE: Event Listener and NOT a Model Listener

Liferay Legend Beiträge: 1302 Beitrittsdatum: 24.06.09 Neueste Beiträge
When we are writing code for hook/custom portlet/listeners no need to import portal-impl.. all the required classes are available in portal-kernel..

If you are using IDE then just remove these imports and include the one from portal-kernel..
For org.apache.struts.globals.. you might need to include the jar in lib folder if not available in global lib..

HTH
thumbnail
MANOVINAYAK AYYAPPAN, geändert vor 12 Jahren.

RE: Event Listener and NOT a Model Listener

Regular Member Beiträge: 131 Beitrittsdatum: 13.06.11 Neueste Beiträge
Hi Ravi,

Thanks a lot emoticon !!!

I am able to find "import com.liferay.portal.util.PrefsPropsUtil;" in portal.kernel but not the others mentioned below:

import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.PropsValues;
import org.apache.struts.Globals;

Looks like I need to find a another way to detect Login and Logout of User and register timestamp.

Once again thanks a lot for your time emoticon and quick response.

Thanks and Regards,
Mano
thumbnail
Ravi Kumar Gupta, geändert vor 12 Jahren.

RE: Event Listener and NOT a Model Listener

Liferay Legend Beiträge: 1302 Beitrittsdatum: 24.06.09 Neueste Beiträge
Can you please write the complete code here so I can analyze it better...
thumbnail
MANOVINAYAK AYYAPPAN, geändert vor 12 Jahren.

RE: Event Listener and NOT a Model Listener

Regular Member Beiträge: 131 Beitrittsdatum: 13.06.11 Neueste Beiträge
Hi Ravi,

Thank you once again. I am trying to copy paste the whole LoginPostAction.java in my hook and then create another method in this class to make note of User Login by calling a service to my custom table.

When you copy paste this code in a freshly created hook and try deploying it, the build fails with the errors pointing to portal.Util classes.

I have changed the name to CustomLoginPostAction from LoginPostAction and included the below line in the portal.properties file:
login.events.post=com.ncs.oneplace.portal.events.OneplaceLoginPostAction.

The errors are mainly with below mentioned imports:
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.PrefsPropsUtil;
import com.liferay.portal.util.PropsValues;


Below is the code for you reference:

package com.liferay.portal.events;

import com.liferay.portal.kernel.events.Action;
import com.liferay.portal.kernel.events.ActionException;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.messaging.DestinationNames;
import com.liferay.portal.kernel.messaging.MessageBusUtil;
import com.liferay.portal.kernel.servlet.HttpHeaders;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.PrefsPropsUtil;
import com.liferay.portal.util.PropsValues;

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

import org.apache.struts.Globals;

/**
* @author Brian Wing Shun Chan
*/
public class CustomLoginPostAction extends Action {

public void run(HttpServletRequest request, HttpServletResponse response)
throws ActionException {

try {
if (_log.isDebugEnabled()) {
_log.debug("Running " + request.getRemoteUser());
}

HttpSession session = request.getSession();

long companyId = PortalUtil.getCompanyId(request);
long userId = PortalUtil.getUserId(request);

// Language

session.removeAttribute(Globals.LOCALE_KEY);

// Live users

if (PropsValues.LIVE_USERS_ENABLED) {
String sessionId = session.getId();
String remoteAddr = request.getRemoteAddr();
String remoteHost = request.getRemoteHost();
String userAgent = request.getHeader(HttpHeaders.USER_AGENT);

JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

jsonObject.put("command", "signIn");
jsonObject.put("companyId", companyId);
jsonObject.put("userId", userId);
jsonObject.put("sessionId", sessionId);
jsonObject.put("remoteAddr", remoteAddr);
jsonObject.put("remoteHost", remoteHost);
jsonObject.put("userAgent", userAgent);

MessageBusUtil.sendMessage(
DestinationNames.LIVE_USERS, jsonObject.toString());
}

if (PrefsPropsUtil.getBoolean(
companyId, PropsKeys.ADMIN_SYNC_DEFAULT_ASSOCIATIONS)) {

UserLocalServiceUtil.addDefaultGroups(userId);
UserLocalServiceUtil.addDefaultRoles(userId);
UserLocalServiceUtil.addDefaultUserGroups(userId);
}
}
catch (Exception e) {
throw new ActionException(e);
}
}

private static Log _log = LogFactoryUtil.getLog(LoginPostAction.class);

}


Thanks and Regards,
Mano
thumbnail
MANOVINAYAK AYYAPPAN, geändert vor 12 Jahren.

RE: Event Listener and NOT a Model Listener

Regular Member Beiträge: 131 Beitrittsdatum: 13.06.11 Neueste Beiträge
Hi Ravi,

I found a solution to my problem.

Here is what it is:

If we want to override an event's action like the PostLoginAction.java which was trying to override. Actually there is nothing like an override for such events.

If i create a run() method within my custom Action CustomPostLoginAction.java and mentioning the same in portal.properties, my custom class does not override the Standard PostLoginAction.java

But,
Standard PostLoginAction.java is executed first and then CustomPostLoginAction.java is executed next.

So all I need to do is create a run() method and put my custom code there so that it would get executed after the Standard PostLoginAction.java

So no more hassles of finding alternative of the portal-impl classes in portal-kernelemoticon

Thanks for your interest my post, I am very happy to get your reply on my problem emoticon

Thanks and Regards,
Mano
thumbnail
Ravi Kumar Gupta, geändert vor 12 Jahren.

RE: Event Listener and NOT a Model Listener

Liferay Legend Beiträge: 1302 Beitrittsdatum: 24.06.09 Neueste Beiträge
Thanks for sharing the info.. that will certainly help other members. emoticon
thumbnail
jelmer kuperus, geändert vor 12 Jahren.

RE: Event Listener and NOT a Model Listener

Liferay Legend Beiträge: 1191 Beitrittsdatum: 10.03.10 Neueste Beiträge
You might actually want to look at / use the auditing plugin

http://svn.liferay.com/repos/public/plugins/incubation/hooks/audit-hook

* use guest as login
thumbnail
MANOVINAYAK AYYAPPAN, geändert vor 12 Jahren.

RE: Event Listener and NOT a Model Listener

Regular Member Beiträge: 131 Beitrittsdatum: 13.06.11 Neueste Beiträge
Hi Jelmer,

Thanks lot for sharing this implementation. I will follow this implementation for my Event Listener as it parallels my development work here.

Thanks a lot for your reply, happy to get interesting development worksemoticon.

Sorry for the Late Reply, I got stuck with the Service Builder today emoticon


Thanks and Regards,
Mano