Wednesday, August 7, 2013

Liferay Scheduler -- Simple/Cron Setup

This is a small post but very important for those who want to learn scheduling and who works on batch, automated tasks. Here, I am explaining how you can use liferay's simple & easy scheduling framework to automate your tedious day2day tasks.

I'm not kidding you can easily schedule your task with Liferay inbuilt Quartz scheduler with below mentioned 5 simple steps:

1) Create a sample Liferay portlet where you want to implement your scheduler. You can create Generic,MVC or Spring.Scheduler is independent of the framework.

2) Make the following change in "liferay-portlet.xml" file.

3) Define the scheduler time in "simple-trigger-value" or "cron-trigger-value" tag.

4) Implement a scheduler class in your src folder. You can implement the method in your normal class  also but I would prefer creating a separate class. Implement "MessageListener" interface of Liferay and implement the unimplemented method of that interface.
"com.liferay.portal.kernel.messaging.MessageListener"
Paste the below code in your scheduler implementation class:
 

5) Deploy and scheduler will run at the defined time.

See, all is done in simple 5 steps.

Now, lets take them one by one. Creating a portlet is simple and an IDE do that for you easily.

Next, we created an entry in "liferay-portlet.xml". In this entry we are telling liferay about our scheduler class and which method to trigger.
Don't change the name of method from "receive" to something else.I wrote "receive" because I implemented Liferay's MessageListener interface which handles scheduler call.

At Last, we are telling liferay that whether we are using Cron job or simple job.
For Cron, I would recommend you to read about Cron implementation from Cron Tutorial.
In Simple job, we simple defined our task time-interval along with the unit (seconds,minute or hours).

Visit below link to create your own Cron expression with a user friendly interface: Cron Maker

Feel free to post your questions and doubts. Happy Learning :)


Wednesday, July 31, 2013

How to override/hide Liferay default error messages?

Many people asked me this question "how we can hide or override Liferay default error message with custom messages?".



So, I thought of sharing this simple post explaining how to do this.

There are 3 ways of doing this:
1) Hide the complete portal's default error messages.(Most client don't want this but some client does.)
2) Hide the default error messages for specific portlet.
3) Hide the messages with the help of CSS.(People generally don't use this method but sometime for some specific requirement you might need this).

Let's take them one by one.

1) Hide the complete portal's default error messages.

In order to accomplish this, we need to create a hook of "end.jsp" file(html/taglib/ui/error).

end.jsp file pops liferay error messages.

Hook is a liferay functionality provided to override liferay's default functionality. You can create hook for custom jsps, language properties, services and portal properties.

 1.1) In your project create a liferay-hook.xml file with below content:
     

This will tell liferay to pick new custom "end.jsp" from custom_jsps folder.
Folder structure will be like this: /custom_jsps/html/taglib/ui/error/end.jsp
If you are using Liferay IDE then it will create the folder structure itself else create the structure and add end.jsp.
To disable the default message comment code from line 21 till line 33. Just comment that piece of code, deploy the hook and you are all set.



 
2) Hide the default error messages for specific portlet

For this we will follow the same steps for creating the hook and extending the end.jsp.
Only change will be of adding a check for our specific portlet.

In end.jsp, instead of commenting the code which we comment in the above method we will add a check for our specific portlet using portlet Id.



3) Hide the messages with the help of CSS

I actually don't recommend this approach but sometime we do need this. Basically with help of css we are hiding our error messages or success messages.
Mostly used inside specific jsps instead of making generic.

.portlet-msg-error{           display : none;}
Note:
If you don't know how to create a hook , please refer liferay wiki. It's simple and easy.
Also, refer the below link for further details on error & success messages:

Liferay error & success messages Wiki

Feel free to ask any question and Happy Learning :)

Tuesday, July 30, 2013

A simple jQuery Form Validation

Here I am creating a simple jQuery form validation consist of a user form, custom js file, jQuery.js and jQuery-validation.js.

Both jQuery js & jQuery-validation.js file you can download from the below jQuery site and I would highly recommend to go through the API-documentation if you have time.

jQuery Validation JS --- jQuery Validation API Documentation
Download jQuery --- jQuery API Documentation

First, we will create a simple form with labels and input tabs.

It is not required to have labels, but a good practice suggests that we should use label with our inputs.
I used both in the below code just to explain validation works irrespective of labels(I got this question many times, so thought of trying that).



Second, we need a js file (you can add the js code inside html or jsp file too inside the "Script" tag.I added a separate js because I follow that pattern.)
In your js file add the below code to validate the form input fields.
Make sure you added jQuery.js and jQuery-validation.js else the below code will not work and you will get error in your web dev tool console.

As I told you earlier, I am creating a simple jQuery validation form. So, here I am only implementing rules, different messages and what action to perform after successful validation.


Thats all you need to do and form is ready to validate via jQuery validation framework.

Note:
Don't forget to add jQuery.js and jQuery-validation.js in you project.


Thursday, July 25, 2013

Liferay portlet with Spring MVC


How to setup a liferay portlet with Spring MVC?

Again, a nicely written and very well explained blog by Nilang Patel explaining the complete setup of a liferay portlet with Spring MVC.

Here you will find one more blog on how to setup render and action method in Spring MVC portlet.

Few things which I want to highlight here:

You can implement diff mode of Liferay portlet within the same Controller.

Below I added a code snippet from my Controller class.


Here I am telling the controller which method serve which mode of the portlet.Now, lets say you want to implement one more method. One action method name=”submitForm”.Now in that case you need to first annotate the method with @ActionMapping(params = “actionParameterName=actionParameterValue”) along with @RequestMapping(“VIEW”) annotation.I put view because I want to tell controller that I want o perform this action method on portlet’s VIEW mode.


Feel free to ask your queries and Happy learning :)