Featured post

Docker setup for Liferay 7 with MySQL

Thursday, 26 January 2012

Column base sorting in Liferay's Search Container with BeanComparator.

Sometimes we have such requirement that we need sorting on our column basis,
   like by clicking on column it gives us sorted data in ascending/descending form.
You can define which column you want to sort and by which order.

By using BeanComparator, it gives you  flexibility not to create your own comparators.

1) Declare String variables to define order by column and order by type

String orderByCol = ParamUtil.getString(request, "orderByCol");
String orderByType = ParamUtil.getString(request, "orderByType");
if(orderByCol==null || orderByCol.equals(StringPool.BLANK)) {
if(orderByType==null || orderByType.equals(StringPool.BLANK)) {
orderByType="asc";            //Default value for order by type

// In your liferay Search Container pass this param orderByCol and orderByType

<liferay-ui:search-container  orderByCol="<%=orderByCol %>" orderByType="<%=orderByType %>" delta="10">

2) BeanComparator beanComparator=new BeanComparator(orderByCol);


//You have to make a copy of this list, otherwise it will throw an exception.

List<Test> copy_List_Details = list_Details;

// Assign it to results object of Search Container

results = ListUtil.subList(list_Details, searchContainer.getStart(), searchContainer.getEnd());

                  //In your column's of Search Container make orderable true and pass orderableProperty,
                  //you can sort by as many colums as you want.

     name="Start Date"
       value="<%=sDate_dis %>"
       orderable="<%=true %>"

You are done with sorting, Try and Enjoy ....:))

Monday, 9 January 2012

Crop Image in Liferay for Images of Image Gallery or Profile Images

Cropping is the way to set/cut image as per your requirement. You can set height/width of the whichever area of the image, you want.
As we all knows that Liferay's Image Gallery does not provide function to crop an Image, so it's time to develop our own.

Steps to to Crop an Image in Liferay-
1) Copy these two functions into your JAVA file or you can simplify the path by creating an ext of com.liferay.portal.image.FileSystemHook.

private static File getFile(long imageId, String type) {
String path = buildPath(String.valueOf(imageId));
String _rootDir=PropsUtil.get("image.hook.file.system.root.dir");
return new File(
_rootDir + StringPool.SLASH + path + StringPool.SLASH +
imageId + StringPool.PERIOD + type);
private static String buildPath(String fileNameFragment) {
int fileNameFragmentLength = fileNameFragment.length();

if (fileNameFragmentLength <= 2) {
return StringPool.BLANK;

StringBundler sb = new StringBundler(
fileNameFragmentLength / 2 + fileNameFragmentLength);

for (int i = 0; i < fileNameFragmentLength; i += 2) {
if ((i + 2) < fileNameFragmentLength) {
sb.append(fileNameFragment.substring(i, i + 2));

return sb.toString();

2) You can use any jQuery plugin like tapmodo-Jcrop or Javascript to get co-ordinates of Image or preview Image before croping.

3) Then write below action as per your requirements, i keep it as simple as i can.

public static void cropImage(
ActionRequest request, ActionResponse response) throws PortalException, SystemException, IOException{
long imageId=ParamUtil.getLong(request,"imageId");
String imageType=ImageLocalServiceUtil.getImage(imageId).getType();

BufferedImage outImage=ImageIO.read(getFile(imageId, imageType));
x1,y1,x2,y2 are the co-ordinates, will be passed as parameters used to crop the image.
BufferedImage cropped=outImage.getSubimage(ParamUtil.getInteger(request, "x1"),ParamUtil.getInteger(request, "y1"),
ParamUtil.getInteger(request, "w"),ParamUtil.getInteger(request, "h"));

new FileOutputStream(getFile(imageId, imageType)));
ImageLocalServiceUtil.updateImage(imageId,getFile(imageId, imageType));

4) Try & Enjoy the function :)