Share

Setting preferences in the Liferay Way

UseCase: I have a one form and it has set fields like
1. Project
2. IssueType
3. Summary
4. Priority

Now i will create a preferences page where user has provision to select which fields has to show in the form or which fields user wants to give values.
In the preferences page if a user checks a particular field check box then that field will show in the form.

Now lets go technically how we can do this one.

1.Create one portlet

2. Insert the below line in liferay-portlet.xml between icon tag and schedule-entry tag.
          
                   com.liferay.portal.kernel.portlet.DeafaultConfigurationAction
               
3. Then deploy the portlet and observe the changes in the UI by clicking the Configuration in the portlet settings.New "setup "tab has added in the popup and new option "Archive/Restore setup"


4. Open "portlet.xml"and define a new "init-param"  with "name" as "config-template" and value as "/html/config.jsp"

          config-template
          /html/config.jsp
     
Inside tag entry following line

config
     

5. Now open config.jsp and Add the following code
<%@page import="com.liferay.portal.kernel.util.Constants"%>
<%@page import="com.liferay.portal.kernel.util.GetterUtil"%>
<%@include file="init.jsp" %>
Show Fields

There are two important things that note here

1.Inside the form have to define a hidden field.With out this the preferences will not be saved into the
portletPreferences table.
2.Any preference that you want to set should be prefixed by "preferences--" and suffixed by "--."
This will help the processAction method of the ConfigurationAction class to process only
those fields and properly store them to database.


6. Now open view.jsp and Add the following code
<%@page import="com.liferay.portal.kernel.util.GetterUtil"%>
<%@include file="/html/init.jsp"%>

Portlet Preferences

Project Issue Type
Summary Priority

Implementing CRON Jobs through Quartz

First of all we have to know the what is CRON Job.It is defined as follows

A CRON job is time based job scheduler.People who setup and maintain software environments use CRON to schedule jobs to run periodically at fixed times,dates or intervals.

Liferay has an in-built feature to support this feature with the help of powerful scheduling and triggering engine called Quartz

Now lets go technically how to use this feature in our portlet development.

Usecase: 
Suppose i am maintaining a one blog which posts the technical details of the Liferay.Now what i am doing is when user registering into the blog he/she has provision to select for monthly news letter

If he/she is willing to take monthly news letter then automatically on every month user will be able to get the news letter on email through Quartz.

Creating a Quartz in Liferay is two step process 

          1. Write a job itself
          2.  Configure and registering a job Quartz Scheduler that is running inside the server

Now lets implement 

1. Create one portlet
2. Create on package com.blognewsletter.job and create one class NotifyNewsLetter inside the package which implements the MessageListener


package com.blognewsletter.job;

import com.liferay.portal.kernel.messaging.Message;
import com.liferay.portal.kernel.messaging.MessageListener;
import com.liferay.portal.kernel.messaging.MessageListenerException;

public class NotifyNewsLetter implements MessageListener {

@Override
public void receive(Message arg0) throws MessageListenerException {
// TODO Auto-generated method stub
System.out.println("This is coming from CRON Job scheuled for every 7 Minutes");
}

}

    Note:step1 completed i.e defining CRON job.
3. Open liferay-portlet.xml and add the following entries immediately after icon tag

4. Deploy the portlet now observe the console .You can see the following messagae for every 7 sec
    "This is coming from CRON Job scheuled for every 7 minutes".
   

Download Source Code Here

How to create a multiple instances on a single server SaaS(Software as a Service)

Before going to topic first let me explain What is Multitenancy


Multitenanacy refers to principle in a software architecture where a single instance of the software runs on a server,serving multiple client organizations(tenants).It is contrasted with a multi-instance architecture where separate software instances are setup for different client organizations. 

It seems to like as follows


 As per my understanding liferay uses software as a service.Hope you got brief idea about multitenancy now lets move onto our liferay architecture an how it supports Multitenancy.The following figure is the best usecase to support multitenancy concept

If we observe above diagram we can have following points

1. A Single instance of liferay serving for two universities means two tenants.
2. In a single university we have two colleges respectively
3. For each college we have a different students
4. So if we develop a portlet then that portlet has to show the student records respect to in his/her         college.

Here our concentration is only on first point how single liferay server working for two tenants.Lets go technically now.

1. Login to the portal as Omni Administrator(test)
2. Goto the ControlPanel
       Under Configuration Goto the PortalInstances

3. Click on the Add button to add new instance
4. Give value as "JNTUniversity" for WebID,Virtual Host and Mail Domain


5. Click on Save button
6. Confirm a new instance got created as part of instances list.
7. Open "C:\Windows\System32\drivers\etc\hosts" in a notepad and append "127.0.0.1 
    JNTUniversity"and save this file.
8. Now how can you access new instance?
9. Open the browser and access an type "jntuniversity:8080".


10. Login to the new instance with email as test@JNTUniversity.com and password as "test".
11. See the below screenshots and observe the URLS.I am able to signIn both the instances and accessing the server.

Localhost



JNTUniversity


This is how Liferay supports Multi tenancy as a Single server instance.

Making our Portlet appear in Control Panel

1. Create a New portlet
2. Myself i have create a portlet with the name as Custom_ControlPanel-portlet

3.Open liferay-portlet.xml and add the following tags to the tags as a child tags




custom_controlpanel
/icon.png
portal
1.0
true
false
/css/main.css
/js/main.js
custom_controlpanel-portlet
        .................
        .................


4. Deploy the portlet by executing ant-deploy
5. After successful execution ant task access the liferay server and observe the control panel.


6. Access the portlet it will show the functionality what you have done.Its up to you.


Download Source Code Here

Difference between Servlet Vs Portlet



Portlet
Servlet
These are desinged to provide aggregate content on a page
Servlet is designed to generate whole web content
Apart from web.xml as a deployment descriptor portlet has one more deployment descriptor portlet.xml
Here only one web.xml as deployment descriptor
While developing portlet we can't predict portlet URL
We can predict servlet's URL at development time as part of element in web.xml
Portlet does not have direct access to ActionRequest,RenderRequest and ResourceRequest.Instead it have access to PortletRequest interface.
Servlet has direct access to ServletRequest and ServletResponse objects
Portlets provides various modes like view,help,edit etc.
Like portlet it does not have modes
While serving user request portlet will execute different methods depending on phase(render,action and resource)
While it serves by using service() method
Portlet specification provides different window states like minimized,maximized and Normal
Servlet specification does not have like portlet.
It always responds two type of action-render and request
It always responds single type of action-request