Monday, June 18, 2012

Paging and Natural Sorting with Displaytag

Source: 
To determine the request parameter name for column sorting you can do:
   new ParamEncoder(tableId).encodeParameterName(TableTagParameters.PARAMETER_SORT))
To get the order (ASC/DESC):
   new ParamEncoder(tableId).encodeParameterName(TableTagParameters.PARAMETER_ORDER))
Descending is 2, Ascending is 1.



<%@ page import="beans.Employee,data.DAO,java.util.List,org.displaytag.tags.TableTagParameters,org.displaytag.util.ParamEncoder"%>

<logic:notEqual name="empList" value="null">
<jsp:scriptlet>
 if (session.getAttribute("empList") != null) {
   String sortBy = request.getParameter((new ParamEncoder("empTable")).encodeParameterName(TableTagParameters.PARAMETER_SORT));
   DAO.sort((List) session.getAttribute("empList"), sortBy);
 }
</jsp:scriptlet>

<display:table name="sessionScope.empList" pagesize="4" id="empTable" sort="external" defaultsort="1" defaultorder="ascending">
 <display:column property="empId" title="ID" sortable="true" sortName="empId" headerClass="sortable" />
 <display:column property="empName" title="Name" sortName="empName" sortable="true" headerClass="sortable" />
 <display:column property="empJob" title="Job" sortable="true" sortName="empJob" headerClass="sortable" />
 <display:column property="empSal" title="Salary" sortable="true" headerClass="sortable" sortName="empSal" />
</display:table>
</logic:notEqual>

public class DAO {
public static List getData(long minSal) {
 SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
 Session session = sessionFactory.getCurrentSession();
 List result = null;
 try {
   session.beginTransaction();
   result = session.createQuery("from Employee as emp where emp.empSal >=?").setLong(0, minSal).list();
   System.out.println("Result size : " + result.size());
   session.getTransaction().commit();
 } catch (Exception e) {
   e.printStackTrace();
 }
 return result;
}

public static List sort(List<Employee> list, String sortBy) {
 Comparator comp = getComparator(sortBy);
 Collections.sort(list, comp);
 return list;
}

private static Comparator getComparator(String sortBy) {
 System.out.println("Sort by : " + sortBy);
 if (sortBy ==null) {
   return new NameComparator();
 }
 if (sortBy.equals("empName"))
   return new NameComparator();
 if (sortBy.equals("empId"))
   return new IdComparator();
 if (sortBy.equals("empSal"))
   return new SalComparator();
 if (sortBy.equals("empJob"))
   return new JobComparator();

 return null;

}

private static class NameComparator implements Comparator {
 public int compare(Object emp1, Object emp2) {
   Employee employee1 = (Employee) emp1;
   Employee employee2 = (Employee) emp2;
   return employee1.getEmpName().compareTo(employee2.getEmpName());
 }
}

private static class IdComparator implements Comparator {
 public int compare(Object emp1, Object emp2) {
   Employee employee1 = (Employee) emp1;
   Employee employee2 = (Employee) emp2;
   return new Long(employee1.getEmpId()).compareTo(new Long(employee2.getEmpId()));
 }
}

private static class SalComparator implements Comparator {
 public int compare(Object emp1, Object emp2) {
   Employee employee1 = (Employee) emp1;
   Employee employee2 = (Employee) emp2;
   return new Long(employee1.getEmpSal()).compareTo(new Long(employee2.getEmpSal()));
 }
}

private static class JobComparator implements Comparator {
 public int compare(Object emp1, Object emp2) {
   Employee employee1 = (Employee) emp1;
   Employee employee2 = (Employee) emp2;
   return employee1.getEmpJob().compareTo(employee2.getEmpJob());
 }
}

}


Saturday, October 8, 2011

Clojure Presentation

Here is a link to a presentation on Clojure I gave at the 1st O.co Technology days.

Clojure Presentation

Tuesday, August 2, 2011

Sorting Java Objects - using BeanUtils

Sorting Java Objects - using BeanUtils 


class Person {


  private String firstName;
  private String lastName;
  
}


The org.apache.commons packages help a great deal with property based sorting:

ArrayList sortFields = new ArrayList();
sortFields.add(new BeanComparator("lastName"));
sortFields.add(new BeanComparator("firstName"));
ComparatorChain multiSort = new 
ComparatorChain(sortFields);
java.util.Collections.sort(list_of_objects_to_sort,multiSort);

See the Collections and BeanUtils packages at

http://jakarta.apache.org/commons/

Monday, June 6, 2011

double vs. BigDecimal

So, what's the conclusion here?
1. Do not use double/float for floating-point arithmetic in Java, use BigDecimal instead. This is because Java cannot represent floating-point precisely.
2. Do not use == or != as a floating-point comparison. Compare Float.floatToIntBits (float) or Double.doubleToLongBits (double) instead. If == or != is used on float/double, there's a possibility that the code will go into infinite loop.
3. Always use BigDecimal for temporary variables, which will be processed/involved in future calculations. Convert the values to float/double only if you want to persist them into the database.



source: http://epramono.blogspot.com/2005/01/double-vs-bigdecimal.html

Saturday, May 28, 2011

Reflections

At my current company, one of the Architects, Chris Hansen introduced, several of us to, a library called Reflections.

Setup:
  • Add the Maven repository which contains the dependency to your Nexus repository if you have one or else add it POM file.
  • Add the Maven dependency to your POM file. GAV=(org.reflections,reflections,0.9.5-RC2)
  • Depending on your needs, configure an instance of the Reflections class as outlined in the Docs.
Recently, I've had to look at a package for all classes annotated with a particular annotation.


Once setup, there are several handy methods on the Reflections class, one of which serves this exact purpose.

reflections.getTypesAnnotatedWith(SomeAnnotation.class);


I tried using a convenience constructor rather than the full blown constructor for Reflections since the javadoc on the convenience constructor seemed to be sufficient for my needs but  that didn't behave as expected.

Convenience constructor
Reflections(final String prefix, final Scanner... scanners)

And since all I needed was a TypeAnnotationsScanner and the convenience constructor above seemed to add it by default, I was hoping to use the constructor without passing any scanners.

Monday, May 2, 2011

Eclipse- tomcat deploy -Exploded location overlaps an existing deployment

Exploded location overlaps an existing deployment - eclipse

problem:
tomcat deployment project is loaded with "Exploded location overlaps an existing deployment" ERROR
solution:
If it appears when using tomcat "Exploded location overlaps an existing deployment" and its three buttons are grayed out state.

1.Find the address of your tomcat deployment, such as
C: \ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ webapps and
C: \ Program Files \ Apache Software Foundation \ Tomcat 6.0 \ work \ Catalina \ localhost
check if the application you want to publish already exist. Delete the published application.

2 go to
project-> properties-> MyEclipse-> Web-> ContextRoot-> WebContext-root: RENAME