コード例 #1
0
  /** 取消预订 */
  @PUT
  @Path("cancelorder")
  @Produces("application/json")
  public Representation cancelOrder(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("orderId")) {
      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();
    int orderId = Integer.parseInt(result.get("orderId").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);
    }

    SLOrder slOrder = orderDao.getSLOrderByOrderId(orderId);

    if (!slOrder.getUserEmail().equals(slUser.getUserEmail())
        && !slUser.getUserType().equals("管理员")) {
      returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.fail);
      returnInfo.put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.can_not_modify_other_person);
      return new JsonRepresentation(returnInfo);
    }

    slOrder.setStatus(ORDER_CANCElED);

    if (!orderDao.updateOrder(slOrder)) {
      returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.fail);
      returnInfo.put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.db_operate_error);
      return new JsonRepresentation(returnInfo);
    }

    returnInfo.put(RestCallInfo.REST_STATUS, RestCallStatus.success);
    returnInfo.put(RestCallInfo.REST_ERROR_CODE, RestCallErrorCode.no_error);
    return new JsonRepresentation(returnInfo);
  }
コード例 #2
0
  /** 预订图书 */
  @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);
  }