我試圖重構此代碼以使其更清潔並使用更好的OOP實踐。這種方法需要一堆無線電/複選框和文本框響應,並將它們更新到數據庫表中,然後更新另一個數據庫表中的清單本身。如何重構此Java方法以更好地使用OOP
我覺得這個方法試圖做的很多。但是我需要確定其他方法和類使用的一些東西,例如,如果存在缺陷(無線電值= 2),是否推進workFlow(processUpdateCheckbox中的advanceWorkflow布爾值確定),根據狀態currentActionItem和advanceWorkflow布爾值,以及保留響應。
setFormFeedback不屬於此處,因爲該方法是從另一個處理表單數據的Servlet調用的,並且此消息丟失。
任何重構幫助,非常感謝。
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;
}
您可能會在http://codereview.stackexchange.com/上獲得更好的答案。 –
我是否在此處轉發問題或者版主如何移動?不想因交叉發帖而被罵! – jeff