I have a controller servlet that will either call the service of Editing (adding/deleting appointments) the calendar or just Displaying it.
actionMap.put(null, new DisplayCalendarAction());
actionMap.put("display", new DisplayCalendarAction());
actionMap.put("modify", new EditCalendarAction());
DisplayCalendarAction
and EditCalendarAction
duplicate code. I realize I should move the duplicate code elsewhere and make it OO, but this is where I need help. I think it should go in the form class. BTW, I am not using STRUTS.
For the sake of learning and keeping it simple, I'll discuss only a subset of my refactoring, which deals with setting up a calendar to display in the view. The calendar view has a year and month selecter. Here is my current way.
public EditCalendarAction() { }
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get any request parameters
int iYear = STKStringUtils.nullIntconv(request.getParameter("iYear"));
int iMonth = STKStringUtils.nullIntconv(request.getParameter("iMonth"));
// Get the current month and year
Calendar ca = new GregorianCalendar();
int curYear = ca.get(Calendar.YEAR);
int iTMonth = ca.get(Calendar.MONTH);
// If request parameters are null default to todays calendar
if (iYear == 0) {
iYear = curYear;
iMonth = iTMonth;
}
// Create a calendar to get the number of days and the week start day
GregorianCalendar cal = new GregorianCalendar(iYear, iMonth, 1);
int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
int weekStartDay = cal.get(Calendar.DAY_OF_WEEK);
// Then create another calender just to get the total number of weeks
cal = new GregorianCalendar(iYear, iMonth, days);
int iTotalweeks = cal.get(Calendar.WEEK_OF_MONTH);
then I pass the days
weekStartDay
and iTotalweeks
to the view
request.setAttribute("iTotalweeks", iTotalweeks);
request.setAttribute("weekStartDay", weekStartDay);
request.setAttribute("days", days);
I use jstl forEach tags to generate a calendar based on these Attributes
Since both my EditCalendarAction
and DisplayCalendarAction
need to generate calendars, the code above is duplicated in both classes.
My attempt at refactoring is to have a method in a form class SchedulerCalendarForm
that is called a setUpCalendar.
public EditCalendarAction() { }
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
EventDAO eventDAO = new EventDAO();
SchedulerCalendarForm calForm = new SchedulerCalendarForm(eventDAO);
calForm.setUpCalendar(request);
RequestDispatcher view = request.getRequestDispatcher("views/calendarEdit_v.jsp");
request.setAttribute("form", calForm);
view.forward(request, response);
}
But now how does my view know what the three values days
weekStartDay
and iTotalweeks
that I had passed as attributes before? I'm passing the calForm to the view but the forEach does not work anymore
Should I instead make a three static procedures and call them to get the values and then pass as attributes?