Pages

Thursday 10 May 2012

delete of log file older than some day in linux

In liferay log file are created .
 It is difficult to delete every time manually . so here is command that delete all file older than 5 days in log folder.
find path to your log folder/* -mtime +5 -exec rm {} \;
for example
find /Server/tomcat-6.0.29/logs/* -mtime +5 -exec rm {} \;
This will delete all file in logs folder older than 5 days .
You can set this command as cron job so it clean your folder daily or weekly.

Thanks
Srikanth 

Monday 7 May 2012

Sorting a List based on the a columnValue /property (BeanComparator)


 Sorting a List<Bean> based on the a columnValue /property (BeanComparator)
Hi  everyone,
            Here is code snippet for sorting a resultant List, which we obtain after performing our business logic .All we need to use is a class provided by apache i.e. org.apache.commons.beanutils.BeanComparator .
Its has a constructor  with a parameter  i.e. String PropertyName .Constructs a property-based comparator for beans. This compares two beans by the property specified in the property parameter. This constructor creates a BeanComparator that uses a ComparableComparator to compare the property values.
Passing "null" to this constructor will cause the BeanComparator to compare objects based on natural order,  that is java.lang.Comparable.


package com.sample.test.sorting;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.commons.beanutils.BeanComparator;
/**
 * @Author Srikanth Reddy
 * @Description SortingUtil provides All sorting  logic  to be used by  all the portlets.
 */
public class SortingUtil {
                public static final boolean DESC = true;   
                /**
                 * Sorts the given List based on columnName.
                 * @param inputList
                 * @param columnName
                 * @return
                 */
    @SuppressWarnings("rawtypes")
                public static List sort(List inputList, String columnName)
    {
            return sort(inputList, false, columnName);
    }
        /**
     * Sorts the given List based on columnName and order.
     * @param inputList
     * @param desc
     * @param columnName
     * @return
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
                public static List sort(List inputList, boolean desc, String columnName)
    {           
            List outputList = new ArrayList(inputList);
            BeanComparator comp = new BeanComparator(columnName);
            Collections.sort(outputList, comp);
            if (desc) {
                    Collections.reverse(outputList);
            }
            return outputList;
    }
}

To use it in your application ,just   call the above class and pass your list to be sorted and property on which it should be sorted.
Ie.
List sortedList = SortingUtil.sort(unsortedList, columnName);
And for Descending Order:
List sortedList = SortingUtil.sort(unsortedList,true, columnName);

Now that you know how to sort a List of  Object /Bean  based on one of the property/columName.
Lets see how to sort a List on multiple coloumNames/Properties  >
We use ComparatorChain.
Why we need ComparatorChain?
If  there is a need of multi-column sorting similar to SQL, then you can use this! The following example sorts the list using the firstName, SecondName and Division. If the firstName is same, then the sorting happens on SecondName and if the SecondName is also same, the sorting happens on Division.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.collections.comparators.ComparatorChain;
import org.apache.commons.collections.comparators.NullComparator;
public class ComparatorExample {
public static void main(String[] args) {
Random idRandomizer = new Random();
List studentList = new ArrayList();
StudentBean stBean1 = new StudentBean(idRandomizer.nextInt(), “Jagan”,
“Asokan”, “III”, “Bangalore”);
StudentBean stBean2 = new StudentBean(idRandomizer.nextInt(), “Jagan”,
“Asokan”, “II”, “Bangalore”);
StudentBean stBean3 = new StudentBean(idRandomizer.nextInt(), “Satya”,
“Asokan”, “III”, “Chennai”);
StudentBean stBean4 = new StudentBean(idRandomizer.nextInt(), “Balaji”,
“MK”, “I”, “Chennai”);
StudentBean stBean5 = new StudentBean(idRandomizer.nextInt(), “Satya”,
“Asokan”, “IV”, “Chennai”);
StudentBean stBean6 = new StudentBean(idRandomizer.nextInt(), “Balaji”,
“MK”, “V”, “Chennai”);
StudentBean stBean7 = new StudentBean(idRandomizer.nextInt(), “Jagan”,
“Asokan”, “VI”, “Chennai”);
StudentBean stBean8 = new StudentBean(idRandomizer.nextInt(), “Venu”,
“Karthik”, “I”, “Bangalore”);
studentList.add(stBean1);
studentList.add(stBean2);
studentList.add(stBean3);
studentList.add(stBean4);
studentList.add(stBean5);
studentList.add(stBean6);
studentList.add(stBean7);
studentList.add(stBean8);

Comparator<StudentBean> firstNameComparator = new BeanComparator(
“firstName”, new NullComparator(true));
Comparator<StudentBean> secondNameComparator = new BeanComparator(
“secondName”, new NullComparator(true));
Comparator<StudentBean> divisionComparator = new BeanComparator(
“division”, new NullComparator(true));
ComparatorChain studentChain = new ComparatorChain();
studentChain.addComparator(firstNameComparator);
studentChain.addComparator(secondNameComparator);
studentChain.addComparator(divisionComparator);

System.out.println(“BEFORE”);
for (int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i));
}
Collections.sort(studentList, studentChain);
System.out.println(“AFTER”);
for (int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i));
}
}
}
The Bean Class
public class StudentBean {
private int rollNumber;
private String firstName;
private String secondName;
private String division;
private String address;
@Override
public String toString() {
return “StudentBean [rollNumber=" + rollNumber + ", firstName="
+ firstName + ", secondName=" + secondName + ", division="
+ division + ", address=" + address + "]“;
}
public StudentBean(int rollNumber, String firstName, String secondName,
String division, String address) {
super();
this.rollNumber = rollNumber;
this.firstName = firstName;
this.secondName = secondName;
this.division = division;
this.address = address;
}
public int getRollNumber() {
return rollNumber;
}
public void setRollNumber(int rollNumber) {
this.rollNumber = rollNumber;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public String getDivision() {
return division;
}
public void setDivision(String division) {
this.division = division;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
The result
BEFORE
StudentBean [rollNumber=1612364660, firstName=Jagan, secondName=Asokan, division=III, address=Bangalore]
StudentBean [rollNumber=724494682, firstName=Jagan, secondName=Asokan, division=II, address=Bangalore]
StudentBean [rollNumber=-861754848, firstName=Satya, secondName=Asokan, division=III, address=Chennai]
StudentBean [rollNumber=588244880, firstName=Balaji, secondName=MK, division=I, address=Chennai]
StudentBean [rollNumber=-1459805186, firstName=Satya, secondName=Asokan, division=IV, address=Chennai]
StudentBean [rollNumber=1559882159, firstName=Balaji, secondName=MK, division=V, address=Chennai]
StudentBean [rollNumber=762774394, firstName=Jagan, secondName=Asokan, division=VI, address=Chennai]
StudentBean [rollNumber=1545447361, firstName=Venu, secondName=Karthik, division=I, address=Bangalore]

AFTER
StudentBean [rollNumber=588244880, firstName=Balaji, secondName=MK, division=I, address=Chennai]
StudentBean [rollNumber=1559882159, firstName=Balaji, secondName=MK, division=V, address=Chennai]
StudentBean [rollNumber=724494682, firstName=Jagan, secondName=Asokan, division=II, address=Bangalore]
StudentBean [rollNumber=1612364660, firstName=Jagan, secondName=Asokan, division=III, address=Bangalore]
StudentBean [rollNumber=762774394, firstName=Jagan, secondName=Asokan, division=VI, address=Chennai]
StudentBean [rollNumber=-861754848, firstName=Satya, secondName=Asokan, division=III, address=Chennai]
StudentBean [rollNumber=-1459805186, firstName=Satya, secondName=Asokan, division=IV, address=Chennai]
StudentBean [rollNumber=1545447361, firstName=Venu, secondName=Karthik, division=I, address=Bangalore]


Note :
My sorting logic based on the BeanComparator.
    Comparator<T> firstNameComparator = new BeanComparator(“firstName”, new NullComparator(true));
    Collections.sort(employeeList, firstNameComparator );

But why we need NullComparator?
If the values can be null, then the BeanComparator would thrown NullPointerException.

Hope this will help you …
Thanks & Regards
Srikanth S

Available implicit objects Liferay JSP page

Available implicit objects Liferay JSP page
On a normal JSP page, some objects are implicitly available. In addition, we can get several others in Liferay using the taglibs. But we don't know all. So, lets become a technical James Bond and investigate about it. :D

Lets see about normal JSP first:
These objects are created by the container automatically and the container makes them available to us. Since these objects are created automatically by the container and are accessed using standard variables; and that is why, they are called implicit objects. They are parsed by the container. They are available only within the jspService method and not in any declaration.

These are 9 objects.
  1. request     (javax.servlet.ServletRequest)
  2. response    (javax.servlet.ServletResponse)
  3. out         (javax.servlet.jsp.JspWriter)
  4. pageContext (javax.servlet.jsp.PageContext)
  5. session     (javax.servlet.http.HttpS)
  6. application (javax.servlet.ServletContext)
  7. config      (javax.servlet.ServletConfig)
  8. page        (java.lang.Object)
  9. exception   (java.lang.Throwable)

Now, lets ebter the liferay context:

The following statements  will give 14 default objects :
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
  1. actionRequest              (javax.portlet.ActionRequest)
  2. actionResponse             (javax.portlet.ActionResponse)
  3. eventRequest               (javax.portlet.EventRequest)
  4. eventResponse,             (javax.portlet.EventResponse)
  5. portletConfig,             (javax.portlet.PortletConfig)
  6. portletName,               (java.lang.String portletName ;
  7. portletPreferences,        (javax.portlet.PortletPreferences)
  8. portletPreferencesValues,  (java.util.Map)
  9. portletSession,            (javax.portlet.PortletSession)
  10. portletSessionScope,     (java.util.Map)
  11. renderRequest,           (javax.portlet.RenderRequest)
  12. renderResponse,          (javax.portlet.RenderResponse)
  13. resourceRequest,         (javax.portlet.ResourceRequest)
  14. resourceResponse         (javax.portlet.ResourceResponse)
And the following statements will give 18 default objects :

<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>
<liferay-theme:defineObjects />

and the type of each object as follows :
  1. themeDisplay         (com.liferay.portal.theme.ThemeDisplay)
  2. company              (com.liferay.portal.model.Company)
  3. account              (com.liferay.portal.model.Account)
  4. user                 (com.liferay.portal.model.User)
  5. realUser             (com.liferay.portal.model.User)
  6. contact              (com.liferay.portal.model.Contact)
  7. layout               (com.liferay.portal.model.Layout)
  8. layouts              (java.util.List)
  9. plid                 (java.lang.Long)
  10. layoutTypePortlet    (com.liferay.portal.model.LayoutTypePortlet)
  11. scopeGroupId         (java.lang.Long)
  12. permissionChecker    (com.liferay.portal.security.permission.PermissionChecker)
  13. locale              (java.util.Locale)
  14. timeZone            (java.util.TimeZone)
  15. theme               (com.liferay.portal.model.Theme)
  16. colorScheme         (com.liferay.portal.model.ColorScheme)
  17. portletDisplay      (com.liferay.portal.theme.PortletDisplay)
  18. portletGroupId      (java.lang.Long)

These default objects can be acquired using pageContext which is an implicit object of JSP. Use the following code for getting default objects.

ActionRequest actionRequest = (ActionRequest) pageContext.findAttribute(“actionRequest”);

  Hope this will help. 


Thanks to Apoorva prakash for sharing this :
http://apoorvaprakash.blogspot.com/2011/09/available-implicit-objects-liferay-jsp.html