001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.chat.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.ParamUtil;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.portal.model.User;
027: import com.liferay.portal.service.UserLocalServiceUtil;
028: import com.liferay.portal.struts.JSONAction;
029: import com.liferay.portal.util.PortalUtil;
030: import com.liferay.portlet.messaging.util.MessagingUtil;
031:
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import org.apache.struts.action.ActionForm;
036: import org.apache.struts.action.ActionMapping;
037:
038: import org.json.JSONObject;
039:
040: /**
041: * <a href="RosterAction.java.html"><b><i>View Source</i></b></a>
042: *
043: * @author Ming-Gih Lam
044: *
045: */
046: public class RosterAction extends JSONAction {
047:
048: public String getJSON(ActionMapping mapping, ActionForm form,
049: HttpServletRequest req, HttpServletResponse res)
050: throws Exception {
051:
052: String cmd = ParamUtil.getString(req, Constants.CMD);
053:
054: JSONObject jo = new JSONObject();
055:
056: if (cmd.equals("addEntry")) {
057: jo = addEntry(req);
058: } else if (cmd.equals("deleteEntries")) {
059: jo = deleteEntries(req);
060: } else if (cmd.equals("getEntries")) {
061: jo = MessagingUtil.getRosterEntries(req.getSession());
062: }
063:
064: return jo.toString();
065: }
066:
067: protected JSONObject addEntry(HttpServletRequest req) {
068: JSONObject jo = new JSONObject();
069:
070: try {
071: long companyId = PortalUtil.getCompanyId(req);
072:
073: long userId = ParamUtil.getLong(req, "userId");
074: String email = ParamUtil.getString(req, "email");
075:
076: User user = null;
077:
078: if (userId > 0) {
079: user = UserLocalServiceUtil.getUserById(companyId,
080: userId);
081: } else {
082: user = UserLocalServiceUtil.getUserByEmailAddress(
083: companyId, email);
084: }
085:
086: jo = MessagingUtil.addRosterEntry(req.getSession(), user);
087: } catch (Exception e) {
088: jo.put("status", "failure");
089: }
090:
091: return jo;
092: }
093:
094: protected JSONObject deleteEntries(HttpServletRequest req) {
095: String rosterList = ParamUtil.getString(req, "entries");
096: String entries[] = StringUtil.split(rosterList, ",");
097:
098: JSONObject jo = new JSONObject();
099:
100: try {
101: MessagingUtil
102: .deleteRosterEntries(req.getSession(), entries);
103: jo.put("status", "success");
104: } catch (Exception e) {
105: jo.put("status", "failure");
106: }
107:
108: return jo;
109: }
110:
111: }
|