1
22
23 package com.liferay.portlet.chat.action;
24
25 import com.liferay.portal.kernel.util.Constants;
26 import com.liferay.portal.kernel.util.ParamUtil;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.service.UserLocalServiceUtil;
30 import com.liferay.portal.struts.JSONAction;
31 import com.liferay.portal.util.PortalUtil;
32 import com.liferay.portlet.messaging.util.MessagingUtil;
33
34 import javax.servlet.http.HttpServletRequest;
35 import javax.servlet.http.HttpServletResponse;
36
37 import org.apache.struts.action.ActionForm;
38 import org.apache.struts.action.ActionMapping;
39
40 import org.json.JSONObject;
41
42
48 public class RosterAction extends JSONAction {
49
50 public String getJSON(
51 ActionMapping mapping, ActionForm form, HttpServletRequest req,
52 HttpServletResponse res)
53 throws Exception {
54
55 String cmd = ParamUtil.getString(req, Constants.CMD);
56
57 JSONObject jo = new JSONObject();
58
59 if (cmd.equals("addEntry")) {
60 jo = addEntry(req);
61 }
62 else if (cmd.equals("deleteEntries")) {
63 jo = deleteEntries(req);
64 }
65 else if (cmd.equals("getEntries")) {
66 jo = MessagingUtil.getRosterEntries(req.getSession());
67 }
68
69 return jo.toString();
70 }
71
72 protected JSONObject addEntry(HttpServletRequest req) {
73 JSONObject jo = new JSONObject();
74
75 try {
76 long companyId = PortalUtil.getCompanyId(req);
77
78 long userId = ParamUtil.getLong(req, "userId");
79 String email = ParamUtil.getString(req, "email");
80
81 User user = null;
82
83 if (userId > 0) {
84 user = UserLocalServiceUtil.getUserById(companyId, userId);
85 }
86 else {
87 user = UserLocalServiceUtil.getUserByEmailAddress(
88 companyId, email);
89 }
90
91 jo = MessagingUtil.addRosterEntry(req.getSession(), user);
92 }
93 catch (Exception e) {
94 jo.put("status", "failure");
95 }
96
97 return jo;
98 }
99
100 protected JSONObject deleteEntries(HttpServletRequest req) {
101 String rosterList = ParamUtil.getString(req, "entries");
102 String entries[] = StringUtil.split(rosterList, ",");
103
104 JSONObject jo = new JSONObject();
105
106 try {
107 MessagingUtil.deleteRosterEntries(req.getSession(), entries);
108 jo.put("status", "success");
109 }
110 catch (Exception e) {
111 jo.put("status", "failure");
112 }
113
114 return jo;
115 }
116
117 }