Tuesday, July 22, 2008

Problem Statement and Source Files


Theory without examples can be hard to grasp and less useful. So just making a small problem and corresponding files. Lets see
Problem statement: Given an online reporting application. There is a form that posts certain parameters and displays reports accordingly. Let us say that there are two basic parameters: report type and date range. Report type may be word, pdf, text. Date range consists of a from and a to time. Further user has an option to specify current time as the to time. Our form bean would look something like this:

package myPackage;

public class MyFormBean{
private String reportType;
//date in format dd/MM/yyyy HH:mm
privateDate fromDate;
private Date toDate;
private boolean useCurrentTime=true;

//.......
//public getters and setters for fields
//.......
}

Reports are denoted by a simple interface:
package myPackage;

public interface Report{
String getFileUrl();
String getType();
Date getCreationTime();
}
Reports are fetched using this interface:
package myPackage;

public interface DataBaseUtil{
List<Report> getReports(MyFormBean myFormBean);
}
which may have some implementation like:
package myPackage;

public class DataBaseUtilImpl{
private static DataBaseUtilImpl singleInst = new DataBaseUtilImpl();
private DataBaseUtilImpl(){}

public static DataBaseUtilImpl getInstance(){ return singleInst ; }

public List<Report> getReports(MyFormBean myFormBean){
//process command object to get a list of reports
}
}
The controller will be injected something like this in the config file:


 <bean id="reportFormController" class="myPackage.ReportFormController">
<property name "dataBaseUtil" ref="DataBaseUtilBean"/>
<property name="formView" value="reportInput"/>
<property name="bindOnNewForm" value="true"/>
<property name="successView" value="reportOutput"/>
<property name="validator" ref="myValidator"/>
<property name="commandName" value="reportFormBean"/>
<property name="commandClass" value="myPackage.MyFormBean"/>
</bean>
The actual form controller will look something like:

package myPackage;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.text.*;

public class ReportFormController extends SimpleFormController {
private DataBaseUtil dataBaseUtil;

public void setDataBaseUtil(DataBaseUtil dbUtil){
this.dataBaseUtil = dbUtil;
}

public DataBaseUtil getDataBaseUtil(){
return this.dataBaseUtil;
}

/*Override so as string inputs in the form bind directly to date in command object*/
@Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder dataBinder) throws Exception{
DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
df.setLenient(false);
dataBinder.registerCustomEditor(Date.class,new CustomDateEditor(df,false));
super.initBinder(request,dataBinder);
}


@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response,Object command, BindException bindException)throws Exception{
//handle submit request
ReportFormBean repFormBean = (ReportFormBean ) command;

List<Report> reps ;

if(dataBaseUtil != null)
reps = dataBaseUtil.getReports(repFormBean);
else
reps = new ArrayList<Report>();

ModelAndView mv = showForm(request, response, bindException);

mvmodel.put("REPORTS", reps);

return mv;
}
}
From next post we will start exploring some practical problems using these files as our base.

No comments: