/** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter pw = response.getWriter(); int id = -1; if (request.getParameter("id") != null && request.getParameter("id") != "") { try { id = Integer.parseInt(request.getParameter("id")); } catch (NumberFormatException exc) { // Do nothing. Just go on... } } Order got = OrdersManager.GetOrderById(id); if (id == -1) { // Форма создания нового заказа try { printForm(pw, id, StateType.STATE_NEW, "", "", "", "0", "0", true, "0", false, false, null); } catch (UnsupportedOrderState e) { // TODO Auto-generated catch block e.printStackTrace(); } } else if (got == null) { printNotFound(pw); } else { // Форма правки существующего заказа try { printForm( pw, id, got.getState(), got.getProductManufacturerAndModel(), got.getCustomerName(), got.getTargetAddress(), got.getPipeLineLength() != null ? got.getPipeLineLength().toString() : null, got.getAdditionalCoolantAmount() != null ? got.getAdditionalCoolantAmount().toString() : null, got.getPumpNeeded(), got.getFullCost().toString(), false, false, null); } catch (UnsupportedOrderState e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html; charset=UTF-8"); resp.setCharacterEncoding("UTF-8"); String message = null; int id = -1; if (req.getParameter("id") != null && req.getParameter("id") != "") { try { id = Integer.parseInt(req.getParameter("id")); } catch (NumberFormatException exc) { // Do nothing. Just go on... } } Order got = OrdersManager.GetOrderById(id); try { String state = req.getParameter("state"); StateType stateType; if (state.equals("new")) { stateType = StateType.STATE_NEW; } else if (state.equals("after_insp")) { stateType = StateType.STATE_AFTER_INSPECTION; } else if (state.equals("complete")) { stateType = StateType.STATE_COMPLETE; } else if (state.equals("cancelled")) { stateType = StateType.STATE_CANCELLED; } else { throw new UnsupportedOrderState(state); } String productManufacturerAndModel = req.getParameter("productManufacturerAndModel"); String customerName = req.getParameter("customerName"); String targetAddress = req.getParameter("targetAddress"); String pipeLineLength = req.getParameter("pipeLineLength"); String additionalCoolantAmount = req.getParameter("additionalCoolantAmount"); Boolean pumpNeeded = (req.getParameter("pumpNeeded") != null && req.getParameter("pumpNeeded").equals("on")); String fullCost = req.getParameter("fullCost"); PrintWriter pw = resp.getWriter(); boolean pipeLineLength_incorrect = false, additionalCoolantAmount_incorrect = false; try { if (id == -1) { if (stateType == StateType.STATE_NEW) { OrdersManager.CreateNewOrder(productManufacturerAndModel, customerName, targetAddress); } else if (stateType == StateType.STATE_AFTER_INSPECTION) { OrdersManager.CreateInspectionCompleteOrder( productManufacturerAndModel, customerName, targetAddress, pipeLineLength, additionalCoolantAmount, pumpNeeded); } else { throw new UnsupportedOrderState(stateType); } } else { switch (stateType) { case STATE_NEW: OrdersManager.ModifyOrder( got, stateType, productManufacturerAndModel, customerName, targetAddress, null, null, null); break; case STATE_AFTER_INSPECTION: OrdersManager.ModifyOrder( got, stateType, productManufacturerAndModel, customerName, targetAddress, pipeLineLength, additionalCoolantAmount, pumpNeeded); break; case STATE_CANCELLED: OrdersManager.ModifyOrder(got, stateType, null, null, null, null, null, null); break; case STATE_COMPLETE: OrdersManager.ModifyOrder(got, stateType, null, null, null, null, null, null); break; default: throw new UnsupportedOrderState(stateType); } } } catch (InvalidInputException iie) { if (iie.getFieldName().equals("pipeLineLength")) { pipeLineLength_incorrect = true; message = pipeLineLength_INCORRECT_MESSAGE; } if (iie.getFieldName().equals("additionalCoolantAmount")) { additionalCoolantAmount_incorrect = true; message = additionalCoolantAmount_INCORRECT_MESSAGE; } } catch (IncorrectValueException ive) { if (ive.getFieldName().equals("pipeLineLength")) { pipeLineLength_incorrect = true; message = pipeLineLength_INCORRECT_MESSAGE; } if (ive.getFieldName().equals("additionalCoolantAmount")) { additionalCoolantAmount_incorrect = true; message = additionalCoolantAmount_INCORRECT_MESSAGE; } } catch (ArgumentCantBeNull e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FieldIsUnchangeable e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IncorrectOrderStateChange e) { // TODO Auto-generated catch block e.printStackTrace(); } if (message == null) { message = "Заказ принят!"; } if (got != null) fullCost = got.getFullCost().toPlainString(); printForm( pw, id, stateType, productManufacturerAndModel, customerName, targetAddress, pipeLineLength, additionalCoolantAmount, pumpNeeded, fullCost, pipeLineLength_incorrect, additionalCoolantAmount_incorrect, message); } catch (UnsupportedOrderState e) { // TODO Auto-generated catch block e.printStackTrace(); } // super.doPost(req, resp); }