public void testIndexFormulaMap() { Session s = openSession(); Transaction t = s.beginTransaction(); User gavin = new User("gavin", "secret"); User turin = new User("turin", "tiger"); Group g = new Group("developers"); g.getUsers().put("gavin", gavin); g.getUsers().put("turin", turin); s.persist(g); gavin.getSession().put("foo", new SessionAttribute("foo", "foo bar baz")); gavin.getSession().put("bar", new SessionAttribute("bar", "foo bar baz 2")); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); g = (Group) s.get(Group.class, "developers"); assertEquals(g.getUsers().size(), 2); g.getUsers().remove("turin"); Map smap = ((User) g.getUsers().get("gavin")).getSession(); assertEquals(smap.size(), 2); smap.remove("bar"); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); g = (Group) s.get(Group.class, "developers"); assertEquals(g.getUsers().size(), 1); smap = ((User) g.getUsers().get("gavin")).getSession(); assertEquals(smap.size(), 1); gavin = (User) g.getUsers().put("gavin", turin); s.delete(gavin); assertEquals( s.createQuery("select count(*) from SessionAttribute").uniqueResult(), new Integer(0)); t.commit(); s.close(); s = openSession(); t = s.beginTransaction(); g = (Group) s.get(Group.class, "developers"); assertEquals(g.getUsers().size(), 1); turin = (User) g.getUsers().get("turin"); smap = turin.getSession(); assertEquals(smap.size(), 0); assertEquals(s.createQuery("select count(*) from User").uniqueResult(), new Integer(1)); s.delete(g); s.delete(turin); assertEquals(s.createQuery("select count(*) from User").uniqueResult(), new Integer(0)); t.commit(); s.close(); }
protected void create(String[] params, HttpServletRequest request, HttpServletResponse response) { PrintWriter writer; IUser user; ISocialmore sm; Integer count; MultipartMap map; ArrayList<String> attaches = new ArrayList<>(); sm = Socialmore.connect(this); user = User.getSession(request); try { map = new MultipartMap(request, this); writer = response.getWriter(); count = Integer.parseInt(map.getParameter("count")); if (count == null || user == null || sm == null) { writer.write(""); return; } File file; for (int i = 0; i < count; i++) { file = map.getFile("attach" + i); writer.write("/assets/attaches/" + file.getName() + ";"); } } catch (IOException | ServletException e1) { } }
public Room getRoom(Session session) { for (Room room : roomlist) { for (User user : room.getUserlist()) { if (user.getSession() == session) { return room; } } } return null; }
public User getUser(String sessionId) { for (Room room : roomlist) { for (User user : room.getUserlist()) { if (user.getSession().getId().equals(sessionId)) { return user; } } } return null; }
// get 관련 public User getUser(Session session) { for (Room room : roomlist) { for (User user : room.getUserlist()) { if (user.getSession() == session) { return user; } } } return null; }
public void testSQLQuery() { Session s = openSession(); Transaction t = s.beginTransaction(); User gavin = new User("gavin", "secret"); User turin = new User("turin", "tiger"); gavin.getSession().put("foo", new SessionAttribute("foo", "foo bar baz")); gavin.getSession().put("bar", new SessionAttribute("bar", "foo bar baz 2")); s.persist(gavin); s.persist(turin); s.flush(); s.clear(); List results = s.getNamedQuery("userSessionData").setParameter("uname", "%in").list(); assertEquals(results.size(), 2); gavin = (User) ((Object[]) results.get(0))[0]; assertEquals(gavin.getName(), "gavin"); assertEquals(gavin.getSession().size(), 2); s.createQuery("delete SessionAttribute").executeUpdate(); s.createQuery("delete User").executeUpdate(); t.commit(); s.close(); }
protected void show(String[] params, HttpServletRequest request, HttpServletResponse response) { ISocialmore sm = Socialmore.connect(this); if (sm == null || User.getSession(request) == null) { forwardTo(request, response, "Sessions/login"); return; } IUser dest; try { dest = sm.getUserByID(Integer.parseInt(params[1])); } catch (NumberFormatException | RemoteException e) { forwardTo(request, response, "Users/index"); return; } request.setAttribute("dest", dest); index(request, response); }
protected void index(HttpServletRequest request, HttpServletResponse response) { ISocialmore sm = Socialmore.connect(this); IUser user = User.getSession(request); if (sm == null || user == null) { forwardTo(request, response, "Sessions/login"); return; } try { ArrayList<IMessage> inbox = (ArrayList<IMessage>) user.getInbox(); request.setAttribute("inbox", inbox); } catch (RemoteException e) { request.setAttribute("flash", "We are sorry! :( Your inbox is temporaly unavailable..."); forwardTo(request, response, "Users/index"); } forwardTo(request, response, "Messages/index"); }
protected void update(String[] params, HttpServletRequest request, HttpServletResponse response) { ISocialmore sm; IUser user; IPost post; HttpSession session = request.getSession(); if (session.getAttribute("user") == null) { forwardTo(request, response, "Sessions/login"); return; } sm = Socialmore.connect(this); user = User.getSession(request); int post_id = -1; try { post_id = Integer.parseInt(params[1]); } catch (Exception e) { } String content = request.getParameter("content"); try { post = sm.getPost(post_id); if (post != null && !content.isEmpty()) { System.out.println("Edit: " + user.getID() + " with " + content); post.edit(user.getID(), content); System.out.println("Edited!!! :D"); } } catch (RemoteException e) { System.out.println("Fail!"); } forwardTo(request, response, "Users/index"); }
protected void delete(String[] params, HttpServletRequest request, HttpServletResponse response) { IUser user; ISocialmore sm; IPost post; IMessage msg; int msg_id, post_id = -1; HttpSession session = request.getSession(); sm = Socialmore.connect(this); user = User.getSession(request); if (sm == null || user == null) { forwardTo(request, response, "Sessions/login"); return; } if (params[0].equalsIgnoreCase("cancel-message")) { try { ArrayList<IMessage> inbox = (ArrayList<IMessage>) user.getInbox(); request.setAttribute("inbox", inbox); } catch (RemoteException e1) { forwardTo(request, response, "Users/index"); return; } try { msg_id = Integer.parseInt(params[1]); msg = user.getSentMessage(msg_id); if (user.getID() == msg.getSenderID() && msg.getReceiving() == null) { if (user.removeMessage(msg_id)) { request.setAttribute("flash_type", "alert-success"); request.setAttribute("flash", "Your scheduled message was cancelled!"); forwardTo(request, response, "Messages/index"); return; } } } catch (Exception e) { } request.setAttribute("flash_type", "alert-error"); request.setAttribute( "flash", "It was <b>not</b> possible to remove the schedule message specified"); forwardTo(request, response, "Messages/index"); return; } try { post_id = Integer.parseInt(params[1]); } catch (Exception e) { } try { post = sm.getPost(post_id); if (post != null) { post.delete(user.getID()); System.out.println("Delete " + post_id); } } catch (RemoteException e) { } forwardTo(request, response, "Users/index"); }