/** 获取预订信息 */ @GET @Path("getorderinfo/{orderId}") @Produces("application/json") public Representation getOrderInfo(@PathParam("orderId") int orderId) { SLOrder slOrder = orderDao.getSLOrderByOrderId(orderId); if (slOrder == null) { HashMap returnInfo = new HashMap(); returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.fail); returnInfo.put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.no_such_order); return new JsonRepresentation(returnInfo); } slOrder.setTheBook(bookDao.queryByISBN(slOrder.getBookISBN())); slOrder.setTheUser(userDao.getSLUserByEmail(slOrder.getUserEmail())); JsonRepresentation ret = new JsonRepresentation(slOrder); try { ret.getJsonObject().put(RestCallInfo.REST_STATUS, RestCallStatus.success); ret.getJsonObject().put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.no_error); } catch (JSONException e) { e.printStackTrace(); } return ret; }
/** 预订图书 */ @PUT @Path("orderbook") @Produces("application/json") public Representation orderBook(Representation entity) { JSONReader reader = new JSONValidatingReader(); HashMap result = null; HashMap returnInfo = new HashMap(); try { result = (HashMap) reader.read(entity.getText()); } catch (IOException e) { e.printStackTrace(); returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.fail); returnInfo.put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.json_format_error); return new JsonRepresentation(returnInfo); } if (result == null || !result.containsKey("sessionKey") || !result.containsKey("bookISBN")) { returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.fail); returnInfo.put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.json_format_error); return new JsonRepresentation(returnInfo); } String sessionKey = result.get("sessionKey").toString(); String bookISBN = result.get("bookISBN").toString(); SLUser slUser = SLSessionManager.getSession(sessionKey); if (slUser == null) { returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.fail); returnInfo.put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.need_login); return new JsonRepresentation(returnInfo); } // 如果还有的借,不能预订 if (bookDao.queryByISBN(bookISBN).getBookAvailableQuantity() > 0) { returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.fail); returnInfo.put( RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.can_not_order_while_you_can_borrow); return new JsonRepresentation(returnInfo); } // 先检查是否已借! if (borrowDao.isUserBookBorrowed(slUser.getUserEmail(), bookISBN)) { returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.fail); returnInfo.put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.already_borrowed); return new JsonRepresentation(returnInfo); } // 再检查是否已预订! if (orderDao.isUserBookOrdered(slUser.getUserEmail(), bookISBN)) { returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.fail); returnInfo.put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.already_ordered); return new JsonRepresentation(returnInfo); } // 插入记录 SLOrder slOrder = new SLOrder(); Calendar c = Calendar.getInstance(); Date orderDate = c.getTime(); slOrder.setUserEmail(slUser.getUserEmail()); slOrder.setBookISBN(bookISBN); slOrder.setOrderDate(orderDate); slOrder.setStatus(ORDER_INQUEUE); if (!orderDao.orderBook(slOrder)) { returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.fail); returnInfo.put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.db_operate_error); return new JsonRepresentation(returnInfo); } // 发送邮件 slOrder.setTheBook(bookDao.queryByISBN(bookISBN)); slOrder.setTheUser(userDao.getSLUserByEmail(slOrder.getUserEmail())); emailUtil.sendOrderSuccessEmail(slOrder); returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.success); returnInfo.put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.no_error); return new JsonRepresentation(returnInfo); }