Category Archives: Design Patterns

The Danger of Carrier Objects

Arlan Whatling

Arlan Whatling

Software Engineering at Source Allies
Arlan Whatling

Latest posts by Arlan Whatling (see all)

In the everyday world, it’s usually a good thing to have what you need ahead of time, even if there’s a possibility of never using it: I’m going to school, better pack my books; I’m taking a flight this afternoon, better bring some earplugs; I’m going to a meeting, better bring a notebook, and coffee… and my phone.  If I don’t end up using these things for the task at hand, no problem.
Continue reading

Code Quality Metrics with Sonar, Part II: Overview of Sonar features

What we covered so far ?

In my previous post I covered the reasons why software quality metrics should be collected and why improvements to the code should be made based on those metrics. In this post I’ll be illustrating how Sonar can fulfill the job of collecting metrics and driving decisions.

Sonar goes beyond just collecting and displaying metrics:

  • Sonar can answer the following questions:
    • What are our most critical code quality issues?
    • Where is the highest concentration of code issues?
    • How many working hours will it take to fix the issues?
    • What does the metrics trend look like over the past year?
    • etc.
  • Sonar can be used to track work tickets assigned to team members.

In short, Sonar helps us analyze the situation, take actions, and quantify the improvement.

Continue reading

Iterators, Functors and Predicates

In this post I am sharing different ways to create ‘Custom Iterators’ and how to control the behavior of that Iterator.

Also part of this article focuses on how to apply different operations on a selected elements within a custom iterator.

The way to do this is to implement a new Iterator-subclass and pass it Functors and Predicates.

By the way Google has implemented all these and more within the Guava Framework, you just need to include this framework as part of your project.

So what are functors and predicates?

Functor: Functor is an interface implementation that apply an operation or status change on an object passed to it’s operation method.
Continue reading

Code Quality Metrics with Sonar, Part I

What is Sonar and Why it’s Needed?

I was fortunate to be able to attend the 2011 edition of No Fluff Just Stuff . One of my favorite presentations was by Matthew McCullough on Sonar . Hence, when the issue of code metrics was raised at a client, Sonar seemed like the right tool to use.

Our client wanted to explore ways to measure and enforce software and code quality metrics. Their goals were to have quantitative measurements of their code quality and analyze those metrics to come up with a set of benchmark measurements. They wanted to utilize Sonar to discourage bad practices.

Continue reading

Exploring Design Patterns in the JDK

Design Patterns are software design solutions that can be applied in our daily software development to help us develop code that is flexible, resilient to change and easily understood (when you are familiar with the pattern). The JDK APIs employ patterns in several areas. So even if you aren’t familiar with patterns if you’ve programmed in Java, you’ve been developing against APIs that have been built using patterns.

Continue reading

Developing a multithreaded test harness

You can’t ignore the fact that web servers are multithreaded. We can hide as much as we want, but sooner or later you’ll find yourself in the situation where your application works fine during development and testing; but once it hits production you start hearing about “funny” things happening. While there are plenty of tools that can be used to simulate multiple users, they aren’t always the easiest to run locally and they seem to take to much time to modify while hot on the trail of a multithreading bug. Here I’ll discuss the approach I took when I was recently faced with this situation.
Continue reading

Taking Advantage of Spring MVC’s Default Behavior

Over the last several months I have worked on several content heavy websites for one of our clients. When I say “content heavy”, I mean that 80%-90% of the pages in the application are static, or at least mostly static, a customer name, membership number, etc may need to be floated in, but not big data tables with dynamic data being pulled from the database. The marketing department manages this content with their content management system and publish fully formed HTML pages (layout, look and feel, etc is controlled in the CMS) which are then pulled into our /WEB-INF/jsp/content directory by our build process.

Our applications treat these HTML pages as JSPs (simple rename in the build script). This lets the marketing team work from a cheat sheet of EL expressions such as ${customerName} and keeps the IT department out of the day to day content management work. One of our goals with these systems was to easily and seamlessly deal with both static pages and very dynamic pages requiring custom controllers to be built. With just a little bit of work Spring MVC makes it easy to provide this functionality and also provides a sane set of defaults for building out these web sites one page at a time.
Continue reading

Building Multi-Criteria Search Queries in Hibernate

In this post I am going to show how to write queries multi-criteria search screens. There are two approaches for making this possible.

  • HQL for building the Query
  • Building Query using Criteria API

HQL for building the Query

Here I am going to show 2 approaches to building the HQL and try to point out the better approach.

Approach I:String concatenation
This approach uses String concatenation and setting up the values directly in the query.

if (startDate != null) {
   if (firstClause) {
      query = query + " where ";
   }
   else {
      query = query + " and ";
   }
 
   query += " s.date >= '" + startDate + "'";
}

Using the above approach there might be a chance of SQL Injection attack and using string concatenation is inherently error-prone.

Approach 2: Criteria as Named Parameters

In this one we create two map to hold parameter name and value which could be binded to the HQL during the execution.

Here is brief example of the approach.

public List search() {
   StringBuilder aQuery = 
      new StringBuilder(" from document p where p.id is not null ");
   HashMap parameterMap = new HashMap();
   HashMap parameterListMap =  new HashMap();
 
   //Collection Criteria
   if (countyList != null) {
      buildCollectionCriterion(aQuery, parameterListMap, "listID", countyList);
   }
 
   //Date Criteria
   if (startDate!= null || endDate != null) {
      buildDateCriterion(aQuery, parameterMap, "dateField",startDate, endDate);
   }
 
   Query query = hibSession.createQuery(aQuery.toString());
 
   for (String key : parameterMap.keySet()) {
      query.setParameter(key, parameterMap.get(key));
   }
 
   for (String key : parameterListMap.keySet()) {
      query.setParameterList(key, parameterListMap.get(key));
   }
 
   List results = query.list();
}
 
//Helper Methods for different type of criteria
 
protected void buildCollectionCriterion(StringBuilder aQuery, 
                                        Map parameterListMap, 
                                        String aFieldName, 
                                        Collection aList) {
   if (aList != null && !aList.isEmpty()) {
      aQuery
         .append(" and p.")
         .append(aFieldName)
         .append(" in (:")
         .append(aFieldName)
         .append(")");
      parameterListMap.put(aFieldName, aList);
   }
}
 
public void buildDateCriterion(StringBuilder aQuery, 
                               Map parameterMap, 
                               String aFieldName, 
                               Date aStartDate, 
                               Date anEndDate) {
   if (aStartDate != null && anEndDate != null) {
      aQuery
         .append(" and ( p."
         .append(aFieldName)
         .append(" between :aStartDate and :anEndDate)");
      parameterMap.put("aStartDate", aStartDate);
      parameterMap.put("anEndDate", anEndDate);
   }
   else if (aStartDate != null) {
      aQuery
         .append(" and  (p.")
         .append(aFieldName)
         .append(" >= :aStartDate)");
      parameterMap.put("aStartDate", aStartDate);
   } 
   else if (anEndDate != null) {
      aQuery
         .append(" and (p.")
         .append(aFieldName)
         .append(" <=:anEndDate");
      parameterMap.put("anEndDate", anEndDate);
   }
}

Building Query using Criteria API

public List search() {
        Criteria c = hibSession.createCriteria(Document.class);
        c.add(Restrictions.notNull("id"));
 
        if(countryList != null) {
                c.add(Restrictions.in("listId", countryList));
        }
 
        if(startDate != null) {
                c.add(Restrictions.ge("dateField", startDate);
        }
 
        if(endDate != null) {
                c.add(Restrictions.le("dateField", endDate);
        }
 
        return c.list();
}

Conclusion

The Hibernate Criteria API is a powerful and elegant library which is well adapted for implementing multi-criteria search functionality and also HQL queries must be built ‘on-the-fly’. Using it in appropriate circumstances will result in cleaner, clearer, more reliable and more maintainable code.