我正在尝试重构此代码以使其更清晰并使用更好的OOP实践。此方法接受一堆radio / checkbox和textbox响应,并在数据库表中更新它们,然后在另一个数据库表中更新Checklist本身。
我觉得这种方法正在尝试做很多事情。但是我需要确定其他方法和类使用的一些东西,例如是否存在缺陷(无线电值= 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;
}
答案 0 :(得分:1)
首先要做的事情:你正在进行面向对象的编程,而不是程序编程,这就是为什么你应该事先考虑哪个类应该承担哪些责任。
那么,这里的责任是什么?我们可以列出必须独立完成的以下任务:
不要忘记从所有处理的开始到结束管理交易(如果需要)。
删除无用的依赖项:您的域模型不应该知道HTTP以及数据的来源。 您的控制器不应该知道数据是如何持久的。
不要重新发明轮子:例如,使用像Struts 2这样的MVC框架来满足验证和控制器需求。使用像hibernate/JPA这样的持久性框架。
答案 1 :(得分:0)
快速修复:
HttpServletRequest
HttpServletRequest
创建此类对象的方法(构造函数,静态方法,setter,如您所愿)伪代码看起来像
HttpServletRequest req;
RequestParameters params = new RequestParameters(req);
//make some processing here (database)
for(UIState state : stateList){
state.makechanges(params)
}
/* UIState can be an interface, each UI item has it own subclass*/