Wednesday, December 3, 2014

Adding tags programatically after blog entry added in Blogs portlet

Adding tags programatically after blog entry added in Blogs portlet


1. create a hook and add portal properties.
2. In portal properties write the portall property as given below:
value.object.listener.com.liferay.portlet.blogs.model.BlogsEntry=com.test.liferay.portlet.blogs.NewBlogEntryListener
    we are doing for  blog content , so we need to create listener for BlogsEntry model.

3. create the above class -NewBlogEntryListener in the package as mentioned above:
4. Write the code below:


NewBlogEntryListener.java
-------------------------------------- (The below code is for creating tags  and also for sending mail when a blog entry created)

package com.test.liferay.portlet.blogs;

import com.liferay.portal.ModelListenerException;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.PrefsPropsUtil;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.ServiceContext;
import com.liferay.portal.util.SubscriptionSender;
import com.liferay.portlet.asset.service.AssetTagLocalServiceUtil;
import com.liferay.portlet.blogs.model.BlogsEntry;
import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
import com.liferay.util.PwdGenerator;

public class NewBlogEntryListener implements ModelListener<BlogsEntry> {

@Override
public void onAfterAddAssociation(Object arg0, String arg1, Object arg2)
throws ModelListenerException {
// TODO Auto-generated method stub
}

@Override
public void onAfterCreate(BlogsEntry entry) throws ModelListenerException {
System.out.println("after create method calling");

String fromName = null;
String fromAddress = null;
String toName = null;
String toAddress = null;
String subject = null;
try {
fromName = PrefsPropsUtil.getString(
entry.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_NAME);
fromAddress = PrefsPropsUtil.getString(
entry.getCompanyId(), PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);

toName = entry.getUserName();
//toAddress = PortalUtil.getUserEmailAddress(entry.getUserId());
toAddress ="nagadevi.nimmagadda@igate.com";

subject = PrefsPropsUtil.getContent(
entry.getCompanyId(), PropsKeys.ADMIN_EMAIL_USER_ADDED_SUBJECT);
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

String body = "sample mail code";

System.out.println("fromName"+fromName);
System.out.println("fromAddress"+fromAddress);
System.out.println("toName"+toName);
System.out.println("toAddress"+toAddress);
System.out.println("subject"+subject);
SubscriptionSender subscriptionSender = new SubscriptionSender();

subscriptionSender.setBody(body);
subscriptionSender.setCompanyId(entry.getCompanyId());
subscriptionSender.setContextAttributes(
"[$USER_ID$]", entry.getUserId(), 
"[$USER_SCREENNAME$]",entry.getUserName());
subscriptionSender.setFrom(fromAddress, fromName);
subscriptionSender.setHtmlFormat(true);
subscriptionSender.setMailId(
"user", entry.getUserId(), System.currentTimeMillis(),
PwdGenerator.getPassword());
//subscriptionSender.setServiceContext(PortalUtil.getuser);
subscriptionSender.setSubject(subject);
subscriptionSender.setUserId(entry.getUserId());

//subscriptionSender.addRuntimeSubscribers(toAddress, toName);

//subscriptionSender.flushNotificationsAsync();
System.out.println("mail sent successfully");
ServiceContext serviceContext = new ServiceContext();
serviceContext.setCompanyId(entry.getCompanyId());
try {
AssetTagLocalServiceUtil.addTag(entry.getUserId(), "tag1", null, serviceContext);
AssetTagLocalServiceUtil.addTag(entry.getUserId(), "tag2", null, serviceContext);
String[] assetTagNames = new String[]{"tag1","tag2"};
System.out.println("tag names are :"+assetTagNames.toString());
long[] assetLinkEntryIds = AssetTagLocalServiceUtil.getTagIds(entry.getGroupId(), assetTagNames);
System.out.println("asset link entry ids :"+assetLinkEntryIds.toString());
BlogsEntryLocalServiceUtil.updateAsset(entry.getUserId(), entry, null, assetTagNames, assetLinkEntryIds);
System.out.println("tag added successfully");
} catch (PortalException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SystemException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Override
public void onAfterRemove(BlogsEntry arg0) throws ModelListenerException {
// TODO Auto-generated method stub
}

@Override
public void onAfterRemoveAssociation(Object arg0, String arg1, Object arg2)
throws ModelListenerException {
// TODO Auto-generated method stub
}

@Override
public void onAfterUpdate(BlogsEntry arg0) throws ModelListenerException {
// TODO Auto-generated method stub
}

@Override
public void onBeforeAddAssociation(Object arg0, String arg1, Object arg2)
throws ModelListenerException {
// TODO Auto-generated method stub
}

@Override
public void onBeforeCreate(BlogsEntry arg0) throws ModelListenerException {
// TODO Auto-generated method stub
}

@Override
public void onBeforeRemove(BlogsEntry arg0) throws ModelListenerException {
// TODO Auto-generated method stub
}

@Override
public void onBeforeRemoveAssociation(Object arg0, String arg1, Object arg2)
throws ModelListenerException {
// TODO Auto-generated method stub
}

@Override
public void onBeforeUpdate(BlogsEntry arg0) throws ModelListenerException {
// TODO Auto-generated method stub
}

}

No comments:

Post a Comment