Wednesday, December 17, 2014

Liferay Portal Properties Hook Plugin

Liferay Hooks are used to customize the liferay portal. We have many kinds of hook plugins one of the hook is Portal Properties hook.

Introduction to Liferay Hook Plugins 

Portal properties hook overriding the existing portal properties and Portal properties hook can modify the some of the portal properties not all the portal properties. The modifiable list of properties are specified in liferay-hook_6_x_x.dtd file. 

If the properties are not listen in the liferay-hook_6_x_x.dtd file then we cannot modify it from portal properties hooks. Assume we want to modify login.events.pre ,login.events.post properties then we will use Portal Properties Hooks. 

If the portal properties are not listed in above then its not possible to override from portal properties hook plugins then we have to use portal-ext.properties file.

Existed Behavior of Liferay portal Create Account Page

In the liferay portal create account page we cannot find password field that user can create his own password at the time of registration.

After registration  user will get mail with auto generated password to login into portal and then he/she can change the password from his/her my account page password section.

Existed User Create Account Page in Liferay



Customer Expected Behavior of Create Account Page

Now assume we want create account page so that user can choose his/her own password at time of registration

The following are basic steps we should consider

Here our requirement is customize the existed liferay portal so best choice is hook plugins.

We have many kind of hook plugins so first we need to check with any portal property that can full fill out requirement.

Need to search in  liferay-hook_6_x_x.dtd   for appropriate portal property that can solve our problem.

If we find relevant portal property then we will choose portal  properties hook Plugin to implement it otherwise we should go for other type of hook Plugin.

In our case “login.create.account.allow.custom.password” is full fill our requirement. “login.create.account.allow.custom.password” is portal property that will allow user to choose custom password at the time of registration means it will accommodate two more user input fields in the create account page.

Implement Solution using Portal Properties Hook

Direct Steps

Step:1

Create Liferay Hook Project using below Article

Liferay Hook Plugin Development with Liferay IDE

Step:2

Configure portal properties file name in “liferay-hook.xml”  its in hook Plugin WEB-INF directory (/Sample-hook/docroot/WEB-INF/liferay-hook.xml) 

Add following configuration in “liferay-hook.xml

<hook> 
<portal-properties>portal.properties</portal-properties> 
</hook>

Step:3

Create “portal.properites” file in Plugin Hook src directory (/Sample-hook/docroot/WEB-INF/src/portal.properties)

Step:4

Add overridden property name and its value in “portal.properites” file that is  Plugin Hook src directory and possible overridden portal properties are available in “liferay-hook_6_x_x.dtd”

Add Overridden Portal Property in hook “portal.properties” files as follows

login.create.account.allow.custom.password=true

Step:5

Once we done all above steps deploy the Hook Plugin by double click on deploy target in the ANT view of eclipse or you can use “ant deploy” target from command prompt.

Once Portal Properties Hook Plugin successfully deployed ,now go to create account page then you can see password fileds in the page

The following screen shows you password filed




Crete Portal properties Hook using Liferay IDE Graphical User Interface

First we need to create Liferay Hook Plugin please follow the article and Create liferay hook Plugin

Liferay Hook Plugin Development with Liferay IDE

Now select the created hook Plugin in the project explorer

Go to File--> New Liferay Hook Configuration

now you can see dialog window  select portal properties check box then click on next


Now you can see another dialog there you can see two sections in the bottom section is for adding portal properties. Now click on Add button 



Now you will get another window there you can see property name and value input fields . For property name you can see “select” button


Now click on “select” button it will show another dialog to select possible portal properties.

Choose required property then click on OK


Now selected property will be appear in the property filed and enter appropriate values for the property in the values filed.


Click the OK and finally click on the Finish button. Now the portal properties file will be created in the hook src directory (“/Sample-hook/docroot/WEB-INF/src/portal.properties”).

The overridden properties and values are present in the “portal.properties” file



Overriding More Portal Properties

In the project explored go to /Sample-hook/docroot/WEB-INF  and double click on “liferay-hook.xml” file then it will be open in editor and we have two views “source” view and “overview”, default view is “overview”

The following is “overview” of “liferay-hook.xml” 



Note:

All hook related configuration are placed in “liferay-hook.xml” 

In the “overview” you can find “Portal properties”  icon with text ,click on that and check the “Customize Liferay Portal Properties” As soon as you check the box then you can see “portal properties” input field and “portal properties overrides” that can have two sections “Name” and “Value”

The following screen show you required input fields


Now we can see Add icon(+)  click on Add icon then you can see Name and Value fields in the bottom of “Portal Properties Overrides” input field.


Now in the name field you can see browse button. You can click on browse button then you can see dialog window there you can see all possible portal properties that can be overridden and you can have search field to search properties.


Now select required property and click on OK  then it will be appear  in the “Override Name” field and enter appropriate value in “Override Value” field


If we want we can also add more values or you can remove values using Add and Remove icons.

Once we done then save the file (Ctrl+S).

In the source view you can see the “liferay-hook.xml” as follows 


Ass soon as we save the file then portal property name and overridden value will be written in “portal.proerties” file that is in hook Plugin src directory



Once we done all above steps deploy the Hook Plugin by double click on deploy target in the ANT view of eclipse or you can use “ant deploy” target from command prompt.

Once Portal Properties Hook Plugin successfully deployed  then you can see respective changes in the liferay portal.

The following are possible portal properties that we can override using hook plugins

admin.default.group.names 
admin.default.role.names 
admin.default.user.group.names 
asset.publisher.asset.entry.query.processors 
asset.publisher.display.styles 
asset.publisher.query.form.configuration 
auth.forward.by.last.path 
auth.public.paths 
auto.deploy.listeners 
application.startup.events 
auth.failure 
auth.max.failures 
auth.token.ignore.actions 
auth.token.ignore.portlets 
auth.token.impl 
auth.pipeline.post 
auth.pipeline.pre 
auto.login.hooks 
captcha.check.portal.create_account 
captcha.engine.impl 
com.liferay.portal.servlet.filters.*Filter 
company.default.locale 
company.default.time.zone 
company.settings.form.authentication 
company.settings.form.configuration 
company.settings.form.identification 
company.settings.form.miscellaneous 
control.panel.entry.class.default 
convert.processes 
default.landing.page.path 
default.regular.color.scheme.id 
default.regular.theme.id 
default.wap.color.scheme.id 
default.wap.theme.id 
dl.file.entry.drafts.enabled 
dl.file.entry.processors 
dl.repository.impl 
dl.store.antivirus.impl 
dl.store.impl 
dockbar.add.portlets 
field.enable.com.liferay.portal.model.Contact.birthday 
field.enable.com.liferay.portal.model.Contact.male 
field.enable.com.liferay.portal.model.Organization.status 
hot.deploy.listeners 
javascript.fast.load 
journal.article.form.add 
journal.article.form.translate 
journal.article.form.update 
layout.form.add 
layout.form.update 
layout.set.form.update 
layout.static.portlets.all 
layout.template.cache.enabled 
layout.types 
layout.user.private.layouts.auto.create 
layout.user.private.layouts.enabled 
layout.user.private.layouts.power.user.required 
layout.user.public.layouts.auto.create 
layout.user.public.layouts.enabled 
layout.user.public.layouts.power.user.required 
ldap.attrs.transformer.impl 
locales 
locales.beta 
locales.enabled 
lock.listeners 
login.create.account.allow.custom.password 
login.events.post 
login.events.pre 
login.form.navigation.post 
login.form.navigation.pre 
logout.events.post 
logout.events.pre 
mail.hook.impl 
my.sites.show.private.sites.with.no.layouts 
my.sites.show.public.sites.with.no.layouts 
my.sites.show.user.private.sites.with.no.layouts 
my.sites.show.user.public.sites.with.no.layouts 
organizations.form.add.identification 
organizations.form.add.main 
organizations.form.add.miscellaneous 
passwords.passwordpolicytoolkit.generator 
passwords.passwordpolicytoolkit.static 
passwords.toolkit 
phone.number.format.impl 
phone.number.format.international.regexp 
phone.number.format.usa.regexp 
portlet.add.default.resource.check.enabled 
portlet.add.default.resource.check.whitelist 
portlet.add.default.resource.check.whitelist.actions 
rss.feeds.enabled 
sanitizer.impl 
servlet.session.create.events 
servlet.session.destroy.events 
servlet.service.events.post 
servlet.service.events.pre 
session.max.allowed 
session.phishing.protected.attributes 
session.store.password 
sites.form.add.advanced 
sites.form.add.main 
sites.form.add.seo 
sites.form.update.advanced 
sites.form.update.main 
sites.form.update.seo 
social.activity.sets.bundling.enabled 
social.activity.sets.enabled 
social.activity.sets.selector 
social.bookmark.* 
terms.of.use.required 
theme.css.fast.load 
theme.images.fast.load 
theme.jsp.override.enabled 
theme.loader.new.theme.id.on.import 
theme.portlet.decorate.default 
theme.portlet.sharing.default 
theme.shortcut.icon 
time.zones 
upgrade.processes 
user.notification.event.confirmation.enabled 
users.email.address.generator 
users.email.address.required 
users.email.address.validator 
users.form.add.identification 
users.form.add.main 
users.form.add.miscellaneous 
users.form.my.account.identification 
users.form.my.account.main 
users.form.my.account.miscellaneous 
users.form.update.identification 
users.form.update.main 
users.form.update.miscellaneous 
users.full.name.generator 
users.full.name.validator 
users.image.max.height 
users.image.max.width 
users.screen.name.always.autogenerate 
users.screen.name.generator 
users.screen.name.validator 
value.object.listener.*
Author

Wednesday, December 10, 2014

Liferay Hook Plugin Development with Liferay IDE

Liferay hook Plugin is one of the Plugin type in liferay to customize liferay portal.
Hook have many capabilities so that we can customize liferay portal and its hot deployable plugins. Liferay Plugins SDK have support to develop hook plugins.

Please follow the articles before start this development.



We have many choices to start liferay development




In this article we are going to see hook development with Liferay IDE. Before start development we need to be ready with development environment that is using Liferay IDE.

Please follow the below articles to setup Liferay Development Environment with Liferay IDE



Liferay Hook Plugin Project Creation

Start your eclipse

go to File-->New-->Liferay Plugin Project

Once we click on new project it will open New Liferay Project Plugin Dialog window

Provides the Information as follows

Project Name: Sample

Project Display Name: Sample Hook

Check the Use Default Location Check box.

Build Type: ANT

Plugins SDK: Select your Plugins SDK( you already configured this when you setup development environment)

Liferay Runtime: Select your run time ( you already configured this when you setup development environment)

Plugin Type: Hook

The following screen shows the New Liferay Project Dialog



Once you fill the all the above information then click on Finish button then your Sample-hook project will be created.

The following screen shows Sample-hook project in the project explorer


Note:

Project Name always appended with Plugin type when we create any liferay project. In our case we just given Sample but finally project will be created with name Sample-hook. Now we just created Liferay Hook project and its just have project skeleton.

Deployment of Hook Plugin

As soon as we created hook project then respective build file will be appeared in the Eclipse ANT view. There we can see many ANT targets. Simply we have to double click on deploy target or we can simply double click on Project Name in the ANT view then it will execute deploy target then hook Plugin will be deployed into server. deploy is the default ant target for hook Plugin.

The following screen shows sample-hook ant targets in the Eclipse ANT view


Once we deploy the hook Plugin then we can see following message in server log view. When we see below message then hook is successfully deployed.

10:11:20,647 INFO [localhost-startStop-2][HookHotDeployListener:687] Registering hook for Sample-hook
10:11:20,651 INFO [localhost-startStop-2][HookHotDeployListener:814] Hook for Sample-hook is available for use

Note:

In General ant deploy is target for deploy the hook project.

Each liferay hook Plugin project have it own project ANT build file called build.xml in the root directory of project there all build process will be defined.

Anatomy of the Liferay Hook Project

When we created hook plugin through Liferay IDE it will create hook project with all required configuration files and its folder sturcture from here we can start our development as for our needs.

The following is general liferay hook plugin files and its structure.

Sample-hook/
   docroot/WEB-INF/src/
   docroot/
      META-INF/
        MANIFEST.MF
     WEB-INF/
       lib/
       liferay-hook.xml
       liferay-plugin-package.properties
       web.xml
build.xml

The following is Liferay Hook Project In the Liferay IDE project explorer


docroot/WEB-INF/src/ :

consist all Java source files and related resource files such as language properties and portal properties.

lib/ :

consist required libraries for project.

liferay-hook.xml :

This is hook configuration file and its consist all predefined configuration tags and it will be available in liferay-hook_6_x_x.dtd file. Here we will define what we should customize in the portal.

A sample “liferay-hook.xml ” with some configuration


liferay-plugin-package.properties :

its common to all liferay plugins projects and here we will defined all properties related to project such as build number, version ant many which we can find from liferay-plugin-package_6_2_0.dtd file.

Example “liferay-plugin-package.properties” file


web.xml :

Its general deployment description for liferay hook project.

buil.xml:

This is ant build file to defined project build process its internally called another build files from plugins SDK to provide different ANT targets.

Example build.xml file as Follows



Liferay Hook Project Development with ANT+Plugins SDK

In the above we have created liferay hook project using Liferay IDE now we will create same project with ANT command line interface.

Liferay Plugins SDK provides ANT command line interface to create liferay Plugin projects.

Before start this we need to be ready with Liferay Plugins SDK+ANT development environment.


Once you ready with Plugins SDK+ANT environment then open command prompt or Linux terminal based on your Operating System.

From command prompt/terminal navigate to hooks directory( /liferay-plugins-sdk-6.2/hooks )

Now issue the following command

Linux and Mac OS X

./create.sh Sample "Sample"

In Windows

create.bat Sample "Sample"

A BUILD SUCCESSFUL message from ANT tells you there’s a new folder named Sample-hook inside the Plugins SDK’s hooks folder.

Project Name always appended with Plugin type when create any liferay project. In our case we just given Sample but finally project will be created with name Sample-hook

Once project is created then navigate to project root directory(/liferay-plugins-sdk-6.2/hooks/Sample-hook) then issue following ANT target then project will be deployed in the server.

ant deploy

Once we deploy the hook then we can see following message ,then hook is successfully deployed.

Hook for Sample-hook is available for use

Note:

well we have successfully created liferay hook Plugin project and here still we did not customize anything of portal. We will see how to customize liferay portal with this hook project in future articles. We simply do some configuration in liferay-hook.xml then it will change the portal behavior as for our need.

Follow the below articles to know more about Different Liferay Hook Plugins


Author

Saturday, December 6, 2014

Liferay Dockbar Custom User Notifications

Liferay 6.2 added new feature in dockbar. One of the feature is user notifications already well aware of dockbar in liferay. Liferay dockbar consist many options it will be appear once user login into the liferay and the options are based on user role.

The following screen shows Liferay Dockbar after user login into the portal


Assumption:

Assume portal administrator wants to send notification to all portal users when portal got new features.

This just example scenario we can make it better approach to send notifications but for understanding purpose i just took this assumption.

One of the Liferay Dockbar feature is User Notification display. This will be display user notifications and relevant messages with respect to users. If some document is added then we will notify to user about document is added. Some new blog post was added then we will notify to the user.

To Display User Notification in the Dockbar we need to deploy the Notifications Portlet then only we can see User Notifications in the dockbar.

Notification portlet available for Community Edition(Notifications CE) and Enterprise Edition(Notifications EE) in Liferay Market Place.

You must download portlet and deploy the portlet into your current Liferay Portal Instance. When you download make sure portlet version.

The following is Market Place Dowload URL (Notifications CE.lpkg)


You can also download from following URL



Once you downloaded and deploy the portlet, then you can see Notification Feature in the dockbar as like following screen.



Liferay have given flexibility to add custom notifications and we can show these notification in the Dockbar using Notification Portlet with respect to each login user.

Custom User Notification Implementation:

Liferay already have given some API classes and mechanism to implement custom notification and we can show in the dockbar. We need follow some set of steps to implement custom notifications.

In our portlet development we may get requirement to send notification to users now we will see how to implement custom user notifications.

Steps to Implement it in the Portlet Development
  1. Implement Custom User Notification Handler Class
  2. Define User Notification Definitions in XML file
  3. Configure User Notification Handler Class and User Notification Definitions Xml file in liferay-portlet.xml file
  4. Add user notifications using UserNotificationEventLocalServiceUtil
Implement Custom User Notification Handler Class

First we need to Implement Custom User Notification Handler class and it will take care of adding notifications in the dockbar. We implement logic like it will fetch respective user notifications and add html markup in the dockbar. Here we can design html to show the notification in the better way.

We should implement “UserNotificationHandler” interface for our Custom User Notification handler class.

Instead of directly implement “UserNotificationHandler” interface we can extends “BaseUserNotificationHandler” class so that we need not to implement all methods.

Do Design Html to add to dockbar we mainly implement the “getBody” and “getBodyTemplate” methods.

getBodyTemplate” We will design Notification Template and “getBody” we will fill the template with actual dynamic content.

The dynamic content will be getting from payload JSON object that will prepared at the time of user notification added in the database.

The example Implementation As follows


public class DockBarUserNotificationHandler extends BaseUserNotificationHandler {
public static final String PORTLET_ID = "dockbarnotificationaction_WAR_DockBarCustomNotificationportlet";

public DockBarUserNotificationHandler() {

setPortletId(DockBarUserNotificationHandler.PORTLET_ID);

}
@Override
protected String getBody(UserNotificationEvent userNotificationEvent,
ServiceContext serviceContext) throws Exception {
JSONObject jsonObject = JSONFactoryUtil
.createJSONObject(userNotificationEvent.getPayload());
//long userId = jsonObject.getLong("userId");
String notificationText=jsonObject.getString("notificationText");
String title = "<strong>Dockbar Custom User Notification for You</strong>";
String body = StringUtil.replace(getBodyTemplate(), new String[] {
"[$TITLE$]", "[$BODY_TEXT$]" },
new String[] { title, notificationText });
return body;
}
protected String getBodyTemplate() throws Exception {
StringBundler sb = new StringBundler(5);
sb.append("<div class=\"title\">[$TITLE$]</div><div ");
sb.append("class=\"body\">[$BODY_TEXT$]</div>");
return sb.toString();
}
}

Define UserNotification Definitions in XML file

This is very important step that we will define the notification information in the xml file. We have set of predefined tags to define notifications. We need to follows the “liferay-user-notification-definitions_6_2_0.dtd” for better understanding about notification tags. We have different type of Notification like Website and Email. Web Site type notification will be displayed in the dockbar.

We need to distinguish each notification by its Notification Type in our example we will distinguish by out custom PortetID.

The following is Example Notifications Configuration


<user-notification-definitions>
<definition>
<notification-type>${com.meera.notification.DockBarUserNotificationHandler.PORTLET_ID}</notification-type>
<description>receive-a-notification-when-administrator-triggered</description>
<delivery-type>
<name>email</name>
<type>${com.liferay.portal.model.UserNotificationDeliveryConstants.TYPE_EMAIL}</type>
<default>false</default>
<modifiable>true</modifiable>
</delivery-type>
<delivery-type>
<name>website</name>
<type>${com.liferay.portal.model.UserNotificationDeliveryConstants.TYPE_WEBSITE}</type>
<default>true</default>
<modifiable>true</modifiable>
</delivery-type>
</definition>
</user-notification-definitions>

For better understanding about notification configuration follow the micro-blogs portlet notification xml file


Configure Custom User Notification Handler Class and User Notification Definitions Xml file in “liferay-portlet.xml” file

Now we need to configure Custom User Notification Hander class and User Notification xml file in the our custom portlet “liferay-portlet.xml” file.
We will use two tags “<user-notification-definitions/>” and “<user-notification-handler-class/>”.We need to provide fully qualified path as value for the xml tags.

The following is configuration


<portlet>
<portlet-name>dock-bar-notification-action</portlet-name>
<icon>/icon.png</icon>
<user-notification-definitions>com/meera/notification/dockbar-user-notification-definitions.xml</user-notification-definitions>
<user-notification-handler-class>com.meera.notification.DockBarUserNotificationHandler</user-notification-handler-class>
<header-portlet-css>/css/main.css</header-portlet-css>
<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
<css-class-wrapper>dock-bar-notification-action-portlet</css-class-wrapper>
</portlet>

Add user notifications using “UserNotificationEventLocalServiceUtil”

We have to add User Notification using UserNotificationEventLocalServiceUtil so that notification will be added in the database.

Where ever particular information should notify to the user then we will use UserNotificationEventLocalServiceUtil to add notification.

Here the main important is we have to create Payload JSON object and fill with required information and this information will be gathered in the Custom User Handler “getBody” class to prepare notification with dynamic content.

Generally we will add notification in the portlet action class or some other business logic writing area we may need to add notification.

The following is example code.


public void sendUserNotification(ActionRequest actionRequest,
ActionResponse actionResponse) throws IOException, PortletException {
try {
List<User> users = UserLocalServiceUtil.getUsers(0,
UserLocalServiceUtil.getUsersCount());
ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequest);
String notificationText = ParamUtil.getString(actionRequest,"notifciationText");
for (User user : users) {
JSONObject payloadJSON = JSONFactoryUtil.createJSONObject();
payloadJSON.put("userId", user.getUserId());
payloadJSON.put("notificationText", notificationText);

UserNotificationEventLocalServiceUtil.addUserNotificationEvent(
user.getUserId(),DockBarUserNotificationHandler.PORTLET_ID,
(new Date()).getTime(), user.getUserId(),payloadJSON.toString(),false, serviceContext);
}
} catch (Exception e) {
e.printStackTrace();
}
}

Notifications are added in the “UserNotificationEventtable.

Well done we have completed implementation part now we can deploy portlet can see the notifications.

Download Source Code and Portlet war file from below URL


Usage of Portlet

Download Notification Portlelt from market place or above specified URL and deploy into portal.

Download “DockBar Custom User Notification” from above URL there you can see “DockBarCustomNotification-portlet-6.2.0.1.war” file and Source code.

If you want use war file just place in liferay deploy directory then it will be deployed. From source also you can deploy it.

Now drag the portlet in the page,portlet name is “DockBar Custom User Notification” and available in “Sample” Category.

Once you add portlet to page you can see text area there enter some notification text then click on Notify button then notification will be send to all portal users.



Now notification is visible in each user dockbar.



Note:

This is not exact real time use case just for understanding purpose i have took the scenario and i am fetching all portal users, if your portal users are more then it take time to complete request so you can change logic in portlet cation class.

Reference Links:



Author