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.mail.action;
022:
023: import com.liferay.portal.kernel.util.Constants;
024: import com.liferay.portal.kernel.util.GetterUtil;
025: import com.liferay.portal.kernel.util.ParamUtil;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.kernel.util.StringUtil;
028: import com.liferay.portal.struts.JSONAction;
029: import com.liferay.portal.theme.ThemeDisplay;
030: import com.liferay.portal.util.PortalUtil;
031: import com.liferay.portal.util.PrettyDateFormat;
032: import com.liferay.portal.util.WebKeys;
033: import com.liferay.portlet.mail.model.MailEnvelope;
034: import com.liferay.portlet.mail.model.MailFolder;
035: import com.liferay.portlet.mail.search.MailDisplayTerms;
036: import com.liferay.portlet.mail.util.MailUtil;
037: import com.liferay.portlet.mail.util.comparator.DateComparator;
038: import com.liferay.portlet.mail.util.comparator.RecipientComparator;
039: import com.liferay.portlet.mail.util.comparator.SizeComparator;
040: import com.liferay.portlet.mail.util.comparator.StateComparator;
041: import com.liferay.portlet.mail.util.comparator.SubjectComparator;
042: import com.liferay.portlet.mail.util.recipient.RecipientFinder;
043: import com.liferay.portlet.mail.util.recipient.RecipientFinderLocator;
044: import com.liferay.util.Autocomplete;
045: import com.liferay.util.ListUtil;
046: import com.liferay.util.TextFormatter;
047:
048: import java.util.Comparator;
049: import java.util.HashMap;
050: import java.util.Iterator;
051: import java.util.List;
052: import java.util.Set;
053: import java.util.SortedSet;
054: import java.util.TreeSet;
055:
056: import javax.portlet.PortletPreferences;
057:
058: import javax.servlet.http.HttpServletRequest;
059: import javax.servlet.http.HttpServletResponse;
060: import javax.servlet.http.HttpSession;
061:
062: import org.apache.commons.collections.map.MultiValueMap;
063: import org.apache.commons.lang.time.StopWatch;
064: import org.apache.commons.logging.Log;
065: import org.apache.commons.logging.LogFactory;
066: import org.apache.struts.action.ActionForm;
067: import org.apache.struts.action.ActionMapping;
068:
069: import org.json.JSONArray;
070: import org.json.JSONObject;
071:
072: /**
073: * <a href="MailAction.java.html"><b><i>View Source</i></b></a>
074: *
075: * @author Ming-Gih Lam
076: *
077: */
078: public class MailAction extends JSONAction {
079:
080: public String getJSON(ActionMapping mapping, ActionForm form,
081: HttpServletRequest req, HttpServletResponse res)
082: throws Exception {
083:
084: String cmd = ParamUtil.getString(req, Constants.CMD);
085:
086: try {
087: if (cmd.equals("addFolder")) {
088: addFolder(req);
089: } else if (cmd.equals("deleteFolder")) {
090: deleteFolder(req);
091: } else if (cmd.equals("deleteMessages")) {
092: deleteMessages(req);
093: } else if (cmd.equals("emptyFolder")) {
094: return emptyFolder(req);
095: } else if (cmd.equals("getFolders")) {
096: return getFolders(req);
097: } else if (cmd.equals("getMessage")) {
098: return getMessage(req);
099: } else if (cmd.equals("getPreview")) {
100: return getPreview(req);
101: } else if (cmd.equals("getRecipients")) {
102: return getRecipients(req);
103: } else if (cmd.equals("getSearch")) {
104: return getSearch(req);
105: } else if (cmd.equals("getSearchCached")) {
106: return getSearchCached(req);
107: } else if (cmd.equals("moveMessages")) {
108: moveMessages(req);
109: } else if (cmd.equals("renameFolder")) {
110: renameFolder(req);
111: } else if (cmd.equals("updatePreferences")) {
112: updatePreferences(req);
113: }
114: } catch (Exception e) {
115: _log.error(e, e);
116: }
117:
118: return null;
119: }
120:
121: protected void addFolder(HttpServletRequest req) throws Exception {
122: String folderId = ParamUtil.getString(req, "folderId");
123:
124: MailUtil.createFolder(req, folderId);
125: }
126:
127: protected void deleteFolder(HttpServletRequest req)
128: throws Exception {
129: String folderId = ParamUtil.getString(req, "folderId");
130:
131: MailUtil.removeFolder(req, folderId);
132: }
133:
134: protected void deleteMessages(HttpServletRequest req)
135: throws Exception {
136: MultiValueMap messages = _convertMessages(req);
137:
138: MailUtil.deleteMessages(req, messages);
139: }
140:
141: protected String emptyFolder(HttpServletRequest req)
142: throws Exception {
143: JSONObject jsonObj = new JSONObject();
144:
145: String folderId = ParamUtil.getString(req, "folderId");
146:
147: MailUtil.emptyFolder(req, folderId);
148:
149: jsonObj.put("folderId", folderId);
150:
151: return jsonObj.toString();
152: }
153:
154: protected Comparator getComparator(HttpServletRequest req)
155: throws Exception {
156:
157: String sortBy = ParamUtil.getString(req, "sortBy");
158: boolean asc = ParamUtil.getBoolean(req, "asc");
159:
160: Comparator comparator;
161:
162: if (sortBy.equals("state")) {
163: comparator = new StateComparator(asc);
164: } else if (sortBy.equals("name")) {
165: comparator = new RecipientComparator(asc);
166: } else if (sortBy.equals("subject")) {
167: comparator = new SubjectComparator(asc);
168: } else if (sortBy.equals("size")) {
169: comparator = new SizeComparator(asc);
170: } else {
171: comparator = new DateComparator(asc);
172: }
173:
174: return comparator;
175: }
176:
177: protected String getFolders(HttpServletRequest req)
178: throws Exception {
179: JSONObject jsonObj = new JSONObject();
180:
181: _getFolders(req, jsonObj);
182:
183: return jsonObj.toString();
184: }
185:
186: protected String getMessage(HttpServletRequest req)
187: throws Exception {
188: JSONObject jsonObj = new JSONObject();
189:
190: String folderId = ParamUtil.getString(req, "folderId");
191: long messageId = ParamUtil.getLong(req, "messageId");
192:
193: MailUtil.setFolder(req, folderId);
194:
195: MailUtil.setMessageId(req, messageId);
196:
197: jsonObj.put("id", messageId);
198: jsonObj.put("folderId", folderId);
199:
200: return jsonObj.toString();
201: }
202:
203: protected String getPreview(HttpServletRequest req)
204: throws Exception {
205: StopWatch stopWatch = null;
206:
207: if (_log.isInfoEnabled()) {
208: stopWatch = new StopWatch();
209:
210: stopWatch.start();
211: }
212:
213: JSONObject jsonObj = new JSONObject();
214:
215: String folderId = ParamUtil.getString(req, "folderId");
216:
217: ThemeDisplay themeDisplay = (ThemeDisplay) req
218: .getAttribute(WebKeys.THEME_DISPLAY);
219:
220: MailUtil.setFolder(req, folderId);
221:
222: Set envelopes = MailUtil.getEnvelopes(req, getComparator(req));
223:
224: JSONArray jsonEnvelopes = _convertEnvelopes(envelopes,
225: themeDisplay);
226:
227: jsonObj.put("folderId", folderId);
228: jsonObj.put("headers", jsonEnvelopes);
229:
230: if (_log.isInfoEnabled()) {
231: _log.info("Total time to get preview "
232: + stopWatch.getTime() + "ms");
233: }
234:
235: return jsonObj.toString();
236: }
237:
238: protected String getRecipients(HttpServletRequest req)
239: throws Exception {
240: long userId = PortalUtil.getUserId(req);
241:
242: String data = ParamUtil.getString(req, "data");
243:
244: PortletPreferences prefs = PortalUtil.getPreferences(req);
245:
246: List finders = RecipientFinderLocator.getInstances();
247:
248: SortedSet recipients = new TreeSet();
249:
250: for (int i = 0; i < finders.size(); i++) {
251: RecipientFinder finder = (RecipientFinder) finders.get(i);
252:
253: boolean enabled = GetterUtil.getBoolean(prefs.getValue(
254: finder.getClass().getName(), null), true);
255:
256: if (enabled) {
257: recipients.addAll(finder.getRecipients(userId, data,
258: new HashMap()));
259: }
260: }
261:
262: String[] recipientsArray = (String[]) ListUtil.fromCollection(
263: recipients).toArray(new String[0]);
264:
265: JSONArray jsonArray = Autocomplete.arrayToJson(recipientsArray,
266: 50);
267:
268: return jsonArray.toString();
269: }
270:
271: protected String getSearch(HttpServletRequest req) throws Exception {
272: JSONObject jsonObj = new JSONObject();
273:
274: HttpSession ses = req.getSession();
275:
276: MailDisplayTerms displayTerms = new MailDisplayTerms(req);
277:
278: ThemeDisplay themeDisplay = (ThemeDisplay) req
279: .getAttribute(WebKeys.THEME_DISPLAY);
280:
281: Set envelopes = MailUtil.search(req, displayTerms,
282: getComparator(req));
283:
284: ses.setAttribute(WebKeys.MAIL_SEARCH_RESULTS, envelopes);
285:
286: JSONArray jsonEnvelopes = _convertEnvelopes(envelopes,
287: themeDisplay);
288:
289: jsonObj.put("headers", jsonEnvelopes);
290:
291: return jsonObj.toString();
292: }
293:
294: protected String getSearchCached(HttpServletRequest req)
295: throws Exception {
296: JSONObject jsonObj = new JSONObject();
297:
298: HttpSession ses = req.getSession();
299:
300: ThemeDisplay themeDisplay = (ThemeDisplay) req
301: .getAttribute(WebKeys.THEME_DISPLAY);
302:
303: Set envelopes = new TreeSet(getComparator(req));
304:
305: envelopes.addAll((Set) ses
306: .getAttribute(WebKeys.MAIL_SEARCH_RESULTS));
307:
308: ses.setAttribute(WebKeys.MAIL_SEARCH_RESULTS, envelopes);
309:
310: JSONArray jsonEnvelopes = _convertEnvelopes(envelopes,
311: themeDisplay);
312:
313: jsonObj.put("headers", jsonEnvelopes);
314:
315: return jsonObj.toString();
316: }
317:
318: protected void moveMessages(HttpServletRequest req)
319: throws Exception {
320: MultiValueMap messages = _convertMessages(req);
321:
322: String folderId = ParamUtil.getString(req, "folderId");
323:
324: MailUtil.moveMessages(req, messages, folderId);
325: }
326:
327: protected void renameFolder(HttpServletRequest req)
328: throws Exception {
329: String folderId = ParamUtil.getString(req, "folderId");
330: String newFolderId = ParamUtil.getString(req, "newFolderId");
331:
332: MailUtil.renameFolder(req, folderId, newFolderId);
333: }
334:
335: protected void updatePreferences(HttpServletRequest req)
336: throws Exception {
337: PortletPreferences prefs = PortalUtil.getPreferences(req);
338:
339: String[] keys = StringUtil.split(ParamUtil
340: .getString(req, "key"));
341: String[] values = StringUtil.split(ParamUtil.getString(req,
342: "value"));
343:
344: for (int i = 0; i < keys.length && i < values.length; i++) {
345: prefs.setValue(keys[i], values[i]);
346: }
347:
348: prefs.store();
349: }
350:
351: private JSONArray _convertEnvelopes(Set envelopes,
352: ThemeDisplay themeDisplay) {
353:
354: PrettyDateFormat dateFormat = new PrettyDateFormat(themeDisplay
355: .getCompanyId(), themeDisplay.getLocale(), themeDisplay
356: .getTimeZone());
357:
358: JSONArray jsonEnvelopes = new JSONArray();
359:
360: Iterator itr = envelopes.iterator();
361:
362: while (itr.hasNext()) {
363: MailEnvelope mailEnvelope = (MailEnvelope) itr.next();
364:
365: JSONObject jsonEnvelope = new JSONObject();
366:
367: String recipient = GetterUtil.getString(mailEnvelope
368: .getRecipient(), StringPool.NBSP);
369:
370: String subject = GetterUtil.getString(mailEnvelope
371: .getSubject(), StringPool.NBSP);
372:
373: jsonEnvelope.put("id", mailEnvelope.getMessageId());
374: jsonEnvelope.put("folderId", mailEnvelope.getFolderName());
375: jsonEnvelope.put("email", recipient);
376: jsonEnvelope.put("subject", subject);
377: jsonEnvelope.put("date", dateFormat.format(mailEnvelope
378: .getDate()));
379: jsonEnvelope.put("size", TextFormatter.formatKB(
380: mailEnvelope.getSize(), themeDisplay.getLocale())
381: + "k");
382: jsonEnvelope.put("read", mailEnvelope.isRead());
383: jsonEnvelope.put("replied", mailEnvelope.isAnswered());
384: jsonEnvelope.put("flagged", mailEnvelope.isFlagged());
385:
386: jsonEnvelopes.put(jsonEnvelope);
387: }
388:
389: return jsonEnvelopes;
390: }
391:
392: private MultiValueMap _convertMessages(HttpServletRequest req) {
393: String[] messagesArray = StringUtil.split(ParamUtil.getString(
394: req, "messages"), ",");
395:
396: MultiValueMap messages = new MultiValueMap();
397:
398: for (int i = 0; i < messagesArray.length; i += 2) {
399: messages.put(messagesArray[i], messagesArray[i + 1]);
400: }
401: return messages;
402: }
403:
404: private void _getFolders(HttpServletRequest req, JSONObject jsonObj)
405: throws Exception {
406:
407: StopWatch stopWatch = null;
408:
409: if (_log.isInfoEnabled()) {
410: stopWatch = new StopWatch();
411:
412: stopWatch.start();
413: }
414:
415: JSONArray jsonFolders = new JSONArray();
416:
417: int count = 1;
418:
419: Iterator itr = MailUtil.getFolders(req).iterator();
420:
421: while (itr.hasNext()) {
422: MailFolder folder = (MailFolder) itr.next();
423:
424: JSONObject jsonFolder = new JSONObject();
425:
426: String name = folder.getName();
427:
428: jsonFolder.put("name", name);
429: jsonFolder.put("id", name);
430: jsonFolder.put("newCount", folder.getUnreadMessageCount());
431: jsonFolder.put("totalCount", folder.getMessageCount());
432:
433: if (name.equals(MailUtil.MAIL_INBOX_NAME)) {
434: jsonFolders.put(0, jsonFolder);
435: } else {
436: jsonFolders.put(count++, jsonFolder);
437: }
438: }
439:
440: jsonObj.put("folders", jsonFolders);
441:
442: if (_log.isInfoEnabled()) {
443: _log.info("Total time to get folders "
444: + stopWatch.getTime() + "ms");
445: }
446: }
447:
448: private static Log _log = LogFactory.getLog(MailAction.class);
449:
450: }
|