1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
43   * <a href="RosterAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Ming-Gih Lam
46   *
47   */
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 }