Пример #1
0
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    request.setCharacterEncoding("UTF-8");
    response.setContentType("text/html;charset=utf-8");
    PrintWriter out = response.getWriter();
    JSONObject obj = new JSONObject();
    HttpSession session = request.getSession();
    Users users = (Users) session.getAttribute("user");
    if (users == null) {
      obj.put("result", false);
      out.println(obj);
      out.flush();
      out.close();
      return;
    }

    String userId = request.getParameter("userId");
    String content = request.getParameter("content");
    Note note = new Note();
    NoteDao noteDao = new NoteDaoImpl();
    UsersDao usersDao = new UsersDaoImpl();

    note.setContent(content);
    note.setToId(Integer.parseInt(userId));
    Users u = usersDao.findById(Integer.parseInt(userId));
    note.setToName(u.getName());
    note.setFromId(users.getUserId());
    note.setFromName(users.getName());
    note.setPostTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));

    int i = noteDao.addNote(note);
    if (i > 0) {
      obj.put("result", true);
    } else {
      obj.put("result", false);
    }
    out.println(obj);
    out.flush();
    out.close();
  }
Пример #2
0
  public ActionForward execute(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws Exception {

    Date todayDate = new Date();

    Session session = null;
    SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
    session = sessionFactory.openSession();

    HttpSession httpSession = request.getSession(true);
    String LoggedInUserName = (String) httpSession.getAttribute("userName");

    if (request.getParameter("invitedBy") != null) {
      String invitedBy = request.getParameter("invitedBy");

      Transaction tr1 = session.beginTransaction();
      hibernate.GamesHistory newGame = new hibernate.GamesHistory();
      newGame.setUser1(LoggedInUserName);
      newGame.setUser2(invitedBy);
      newGame.setGameStatus("ongoing");
      newGame.setStartDate(todayDate);
      session.save(newGame);

      tr1.commit();

      String SQL_QUERY =
          "from GamesHistory game where game.user1='"
              + LoggedInUserName
              + "' AND user2='"
              + invitedBy
              + "' order by game.startDate DESC";

      Query query = session.createQuery(SQL_QUERY);
      query.setMaxResults(1);
      int gameId = 0;
      for (Iterator it = query.iterate(); it.hasNext(); ) {
        GamesHistory gameObject = (GamesHistory) it.next();
        gameId = gameObject.getGameId();
      }

      GameMovesStore newGameMovesStore = new GameMovesStore();
      newGameMovesStore.setNewGameMoveMap(gameId);

      request.setAttribute("invitedBy", invitedBy);
      request.setAttribute("gameId", gameId);

      session.flush();

      UsersDao newUsersDao = new UsersDao();
      newUsersDao.setUserBusy(LoggedInUserName);
      newUsersDao.setUserBusy(invitedBy);
    }

    if (request.getParameter("invited") != null) {
      String invited = request.getParameter("invited");

      String SQL_QUERY =
          "from GamesHistory game where game.user1='"
              + invited
              + "' AND user2='"
              + LoggedInUserName
              + "' order by game.startDate DESC";

      Query query = session.createQuery(SQL_QUERY);
      query.setMaxResults(1);
      int gameId = 0;
      for (Iterator it = query.iterate(); it.hasNext(); ) {
        GamesHistory gameObject = (GamesHistory) it.next();
        gameId = gameObject.getGameId();
      }

      request.setAttribute("gameId", gameId);
      request.setAttribute("invited", invited);
    }

    session.close();

    InvitationBean newInvitationBean = new InvitationBean();
    newInvitationBean.deleteUserInvitationMap(LoggedInUserName);
    return mapping.findForward("success");
  }