I’m trying to refactor this code to be cleaner and use better OOP practices. This method takes a bunch radio/checkbox and textbox responses and updates them in a database table and then updates the Checklist itself in another database table.
I feel this method is trying to do to much. But I need to determine a few things that other methods and classes use, such as if a deficiency exists (radio value = 2), whether to advance the workFlow (advanceWorkflow boolean determined in processUpdateCheckbox), who to email next based on the status of the currentActionItem and the advanceWorkflow boolean, as well as persist the responses.
The setFormFeedback doesn’t belong here either because the method is being called from another Servlet that is handling the form data and this message is lost.
Any help at refactoring this greatly appreciated.
public ChecklistInstance updateYesNoNAChecklistTogles(HttpServletRequest request, ChecklistInstance ci) throws DAOException {
String work_item_class_id = request.getParameter("work_item_class_id");
String work_action_class_id = request.getParameter("work_action_class_id");
String paramName;
String attribute_id;
String radioValue;
String textValue;
String strStatus = "1";
String strStoredNo = "";
Date dateNow = new Date();
YesNoNAAnswerDAO ynnDao = new YesNoNAAnswerDAO();
ChecklistInstanceDAO ciDao = new ChecklistInstanceDAO();
WorkflowInstanceWorkItemAction currentActionItem = new WorkflowInstanceWorkItemAction();
currentActionItem.setWork_item_class_id(work_item_class_id);
currentActionItem.setWork_action_class_id(work_action_class_id);
// Put the form check list responses into a list
List answer_attribute_list = new ArrayList();
java.util.Enumeration enum2 = request.getParameterNames();
while (enum2.hasMoreElements()) {
paramName = (String) enum2.nextElement();
boolean isNewQ = paramName.startsWith("qID_");
if (isNewQ) {
attribute_id = paramName.replaceAll("qID_", "");
YesNoNAAnswer clr = new YesNoNAAnswer();
if (request.getParameter("radio_" + attribute_id) != null) {
radioValue = request.getParameter("radio_" + attribute_id);
} else {
radioValue = "0";
}
if (request.getParameter("textbox_" + attribute_id) != null) {
textValue = request.getParameter("textbox_" + attribute_id);
} else {
textValue = "";
}
if (request.getParameter("check_" + attribute_id) != null) {
radioValue = request.getParameter("check_" + attribute_id);
} else {
// checkValue = "";
}
if ("0".equals(radioValue) || "2".equals(radioValue)) {
strStatus = "0";
}
strStoredNo = request.getParameter("stored_" + attribute_id);
if ("2".equals(radioValue) && !"yes".equals(strStoredNo)) {
deficiencyFound = true;
}
clr.setWorkflow_instance_id(ci.getWorkflow_instance_id());
clr.setWfi_work_item_action_id(ci.getWfi_work_item_action_id());
clr.setFail_reason(textValue);
clr.setAttribute_id(attribute_id);
clr.setToggle_value(radioValue);
answer_attribute_list.add(clr);
}
}
ci.setChecklist_state(strStatus);
ci.setLast_update(dateNow);
ci.setAdditional_info(FormUtil.getFieldValue(request, FIELD_ADDITIONAL_INFO));
processUpdateCheckbox(request, ci, currentActionItem);
// Update the base check list
ciDao.updateInstance(ci, authenticatedUser);
// Update the check list question responses
ynnDao.updateToggles(answer_attribute_list, authenticatedUser);
// update the work flow
WorkflowInstanceDAO wfiDao = new WorkflowInstanceDAO();
WorkflowInstanceForm wfiForm = new WorkflowInstanceForm(wfiDao, authenticatedUser);
WorkflowInstance wfi = (WorkflowInstance) wfiForm.view(ci.getWorkflow_instance_id(), authenticatedUser);
wfiForm.updateWorkFlowInstance(wfi, currentActionItem);
setFormFeedback("You have successfully updated the checklist.");
triggerUpdateEmail(request, ci, wfi, currentActionItem);
return ci;
}
First things first : you’re doing Object-Oriented programming, not procedural programming, that’s why you should think before hand which class should take which responsibilities.
So, what are the responsiblities here ? We can list the following tasks that must be done independently :
Don’t forget to manage a transaction (if needed) from the start to the end of all processing.
Remove useless dependencies : Your domain model should not know about HTTP and from where data is coming from.
Your controller should not know how data is persisted.
Don’t reinvent the wheel : use a MVC framework like Struts 2 for example for your validation and controller needs. Use a framework for persistence like hibernate/JPA.