PageRenderTime 35ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 1ms

/portlets/so-portlet/docroot/WEB-INF/src/com/liferay/so/activities/portlet/ActivitiesPortlet.java

http://github.com/liferay/liferay-plugins
Java | 422 lines | 315 code | 88 blank | 19 comment | 33 complexity | 873d574e08038f35866d8fc0bfdb7c47 MD5 | raw file
  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This file is part of Liferay Social Office. Liferay Social Office is free
  5. * software: you can redistribute it and/or modify it under the terms of the GNU
  6. * Affero General Public License as published by the Free Software Foundation,
  7. * either version 3 of the License, or (at your option) any later version.
  8. *
  9. * Liferay Social Office is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
  12. * for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * Liferay Social Office. If not, see http://www.gnu.org/licenses/agpl-3.0.html.
  16. */
  17. package com.liferay.so.activities.portlet;
  18. import com.liferay.message.boards.kernel.model.MBMessage;
  19. import com.liferay.message.boards.kernel.model.MBMessageDisplay;
  20. import com.liferay.message.boards.kernel.model.MBThread;
  21. import com.liferay.message.boards.kernel.model.MBTreeWalker;
  22. import com.liferay.message.boards.kernel.service.MBMessageLocalServiceUtil;
  23. import com.liferay.message.boards.kernel.service.MBMessageServiceUtil;
  24. import com.liferay.message.boards.kernel.util.comparator.MessageCreateDateComparator;
  25. import com.liferay.microblogs.model.MicroblogsEntry;
  26. import com.liferay.microblogs.model.MicroblogsEntryConstants;
  27. import com.liferay.microblogs.service.MicroblogsEntryLocalServiceUtil;
  28. import com.liferay.microblogs.service.MicroblogsEntryServiceUtil;
  29. import com.liferay.portal.kernel.dao.orm.QueryUtil;
  30. import com.liferay.portal.kernel.json.JSONArray;
  31. import com.liferay.portal.kernel.json.JSONFactoryUtil;
  32. import com.liferay.portal.kernel.json.JSONObject;
  33. import com.liferay.portal.kernel.model.User;
  34. import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
  35. import com.liferay.portal.kernel.service.ServiceContext;
  36. import com.liferay.portal.kernel.service.ServiceContextFactory;
  37. import com.liferay.portal.kernel.service.UserLocalServiceUtil;
  38. import com.liferay.portal.kernel.theme.ThemeDisplay;
  39. import com.liferay.portal.kernel.util.Constants;
  40. import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
  41. import com.liferay.portal.kernel.util.HtmlUtil;
  42. import com.liferay.portal.kernel.util.ListUtil;
  43. import com.liferay.portal.kernel.util.ParamUtil;
  44. import com.liferay.portal.kernel.util.StringPool;
  45. import com.liferay.portal.kernel.util.Time;
  46. import com.liferay.portal.kernel.util.WebKeys;
  47. import com.liferay.portal.kernel.workflow.WorkflowConstants;
  48. import com.liferay.so.activities.util.ActivitiesUtil;
  49. import com.liferay.social.kernel.model.SocialActivitySet;
  50. import com.liferay.social.kernel.service.SocialActivitySetLocalServiceUtil;
  51. import java.io.IOException;
  52. import java.text.DateFormat;
  53. import java.text.Format;
  54. import java.util.Date;
  55. import java.util.List;
  56. import javax.portlet.ActionRequest;
  57. import javax.portlet.ActionResponse;
  58. import javax.portlet.PortletException;
  59. import javax.portlet.ResourceRequest;
  60. import javax.portlet.ResourceResponse;
  61. /**
  62. * @author Matthew Kong
  63. */
  64. public class ActivitiesPortlet extends MVCPortlet {
  65. public void getMBComments(
  66. SocialActivitySet activitySet, ResourceRequest resourceRequest,
  67. ResourceResponse resourceResponse)
  68. throws Exception {
  69. ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(
  70. WebKeys.THEME_DISPLAY);
  71. JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
  72. JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
  73. Object[] commentsClassNameAndClassPK =
  74. ActivitiesUtil.getCommentsClassNameAndClassPK(activitySet);
  75. String className = (String)commentsClassNameAndClassPK[0];
  76. long classPK = (Long)commentsClassNameAndClassPK[1];
  77. MBMessageDisplay mbMessageDisplay =
  78. MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
  79. themeDisplay.getUserId(), activitySet.getGroupId(), className,
  80. classPK, WorkflowConstants.STATUS_APPROVED);
  81. MBTreeWalker mbTeeWalker = mbMessageDisplay.getTreeWalker();
  82. List<MBMessage> mbMessages = mbTeeWalker.getMessages();
  83. mbMessages = ListUtil.sort(
  84. mbMessages, new MessageCreateDateComparator(true));
  85. for (int i = 1; i < mbMessages.size(); i++) {
  86. MBMessage mbMessage = mbMessages.get(i);
  87. JSONObject messageJSONObject = getJSONObject(
  88. mbMessage.getMessageId(), mbMessage.getBody(),
  89. mbMessage.getModifiedDate(), mbMessage.getUserId(),
  90. mbMessage.getUserName(), themeDisplay);
  91. jsonArray.put(messageJSONObject);
  92. }
  93. jsonObject.put("comments", jsonArray);
  94. writeJSON(resourceRequest, resourceResponse, jsonObject);
  95. }
  96. public void getMicroblogsComments(
  97. SocialActivitySet activitySet, ResourceRequest resourceRequest,
  98. ResourceResponse resourceResponse)
  99. throws Exception {
  100. ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(
  101. WebKeys.THEME_DISPLAY);
  102. JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
  103. JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
  104. List<MicroblogsEntry> microblogsEntries =
  105. MicroblogsEntryLocalServiceUtil.
  106. getParentMicroblogsEntryMicroblogsEntries(
  107. MicroblogsEntryConstants.TYPE_REPLY,
  108. activitySet.getClassPK(), QueryUtil.ALL_POS,
  109. QueryUtil.ALL_POS);
  110. for (MicroblogsEntry microblogsEntry : microblogsEntries) {
  111. JSONObject microblogsEntryJSONObject = getJSONObject(
  112. microblogsEntry.getMicroblogsEntryId(),
  113. microblogsEntry.getContent(), microblogsEntry.getModifiedDate(),
  114. microblogsEntry.getUserId(), microblogsEntry.getUserName(),
  115. themeDisplay);
  116. jsonArray.put(microblogsEntryJSONObject);
  117. }
  118. jsonObject.put("comments", jsonArray);
  119. jsonObject.put("commentsCount", microblogsEntries.size());
  120. writeJSON(resourceRequest, resourceResponse, jsonObject);
  121. }
  122. @Override
  123. public void processAction(
  124. ActionRequest actionRequest, ActionResponse actionResponse)
  125. throws IOException, PortletException {
  126. String actionName = ParamUtil.getString(
  127. actionRequest, ActionRequest.ACTION_NAME);
  128. try {
  129. if (actionName.equals("repostMicroblogsEntry")) {
  130. repostMicroblogsEntry(actionRequest, actionResponse);
  131. }
  132. else if (actionName.equals("updateComment")) {
  133. String className = ParamUtil.getString(
  134. actionRequest, "className");
  135. if (className.equals(MicroblogsEntry.class.getName())) {
  136. updateMicroblogsComment(actionRequest, actionResponse);
  137. }
  138. else {
  139. updateMBComment(actionRequest, actionResponse);
  140. }
  141. }
  142. }
  143. catch (Exception e) {
  144. throw new PortletException(e);
  145. }
  146. }
  147. public void repostMicroblogsEntry(
  148. ActionRequest actionRequest, ActionResponse actionResponse)
  149. throws Exception {
  150. ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
  151. WebKeys.THEME_DISPLAY);
  152. long microblogsEntryId = ParamUtil.getLong(
  153. actionRequest, "microblogsEntryId");
  154. MicroblogsEntry microblogsEntry =
  155. MicroblogsEntryLocalServiceUtil.getMicroblogsEntry(
  156. microblogsEntryId);
  157. ServiceContext serviceContext = ServiceContextFactory.getInstance(
  158. MicroblogsEntry.class.getName(), actionRequest);
  159. MicroblogsEntryServiceUtil.addMicroblogsEntry(
  160. themeDisplay.getUserId(), microblogsEntry.getContent(),
  161. MicroblogsEntryConstants.TYPE_REPOST,
  162. microblogsEntry.getMicroblogsEntryId(),
  163. microblogsEntry.getSocialRelationType(), serviceContext);
  164. }
  165. @Override
  166. public void serveResource(
  167. ResourceRequest resourceRequest, ResourceResponse resourceResponse)
  168. throws PortletException {
  169. try {
  170. String resourceID = resourceRequest.getResourceID();
  171. if (resourceID.equals("getComments")) {
  172. long activitySetId = ParamUtil.getLong(
  173. resourceRequest, "activitySetId");
  174. SocialActivitySet activitySet =
  175. SocialActivitySetLocalServiceUtil.fetchSocialActivitySet(
  176. activitySetId);
  177. if (activitySet == null) {
  178. return;
  179. }
  180. String className = activitySet.getClassName();
  181. if (className.equals(MicroblogsEntry.class.getName())) {
  182. getMicroblogsComments(
  183. activitySet, resourceRequest, resourceResponse);
  184. }
  185. else {
  186. getMBComments(
  187. activitySet, resourceRequest, resourceResponse);
  188. }
  189. }
  190. else {
  191. super.serveResource(resourceRequest, resourceResponse);
  192. }
  193. }
  194. catch (Exception e) {
  195. throw new PortletException(e);
  196. }
  197. }
  198. public void updateMBComment(
  199. ActionRequest actionRequest, ActionResponse actionResponse)
  200. throws Exception {
  201. ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
  202. WebKeys.THEME_DISPLAY);
  203. String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
  204. String className = ParamUtil.getString(actionRequest, "className");
  205. long classPK = ParamUtil.getLong(actionRequest, "classPK");
  206. long mbMessageId = ParamUtil.getLong(
  207. actionRequest, "mbMessageIdOrMicroblogsEntryId");
  208. String body = ParamUtil.getString(actionRequest, "body");
  209. JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
  210. try {
  211. MBMessage mbMessage = null;
  212. long groupId = themeDisplay.getScopeGroupId();
  213. ServiceContext serviceContext = ServiceContextFactory.getInstance(
  214. MBMessage.class.getName(), actionRequest);
  215. if (cmd.equals(Constants.DELETE)) {
  216. MBMessageServiceUtil.deleteDiscussionMessage(
  217. groupId, className, classPK, className, classPK,
  218. themeDisplay.getUserId(), mbMessageId);
  219. }
  220. else if (cmd.equals(Constants.EDIT) && (mbMessageId > 0)) {
  221. mbMessage = MBMessageServiceUtil.updateDiscussionMessage(
  222. className, classPK, mbMessageId, StringPool.BLANK, body,
  223. serviceContext);
  224. }
  225. else {
  226. MBMessageDisplay mbMessageDisplay =
  227. MBMessageLocalServiceUtil.getDiscussionMessageDisplay(
  228. themeDisplay.getUserId(), groupId, className, classPK,
  229. WorkflowConstants.STATUS_APPROVED);
  230. MBThread mbThread = mbMessageDisplay.getThread();
  231. MBTreeWalker mbTreeWalker = mbMessageDisplay.getTreeWalker();
  232. MBMessage rootMBMessage = mbTreeWalker.getRoot();
  233. mbMessage = MBMessageServiceUtil.addDiscussionMessage(
  234. groupId, className, classPK, mbThread.getThreadId(),
  235. rootMBMessage.getMessageId(), StringPool.BLANK, body,
  236. serviceContext);
  237. }
  238. if (mbMessage != null) {
  239. jsonObject = getJSONObject(
  240. mbMessage.getMessageId(), mbMessage.getBody(),
  241. mbMessage.getModifiedDate(), mbMessage.getUserId(),
  242. mbMessage.getUserName(), themeDisplay);
  243. }
  244. jsonObject.put("success", Boolean.TRUE);
  245. }
  246. catch (Exception e) {
  247. jsonObject.put("success", Boolean.FALSE);
  248. }
  249. writeJSON(actionRequest, actionResponse, jsonObject);
  250. }
  251. public void updateMicroblogsComment(
  252. ActionRequest actionRequest, ActionResponse actionResponse)
  253. throws Exception {
  254. ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
  255. WebKeys.THEME_DISPLAY);
  256. String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
  257. long classPK = ParamUtil.getLong(actionRequest, "classPK");
  258. long microblogsEntryId = ParamUtil.getLong(
  259. actionRequest, "mbMessageIdOrMicroblogsEntryId");
  260. String body = ParamUtil.getString(actionRequest, "body");
  261. JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
  262. try {
  263. MicroblogsEntry microblogsEntry = null;
  264. if (cmd.equals(Constants.DELETE)) {
  265. MicroblogsEntryServiceUtil.deleteMicroblogsEntry(
  266. microblogsEntryId);
  267. }
  268. else if (classPK > 0) {
  269. MicroblogsEntry currentMicroblogsEntry =
  270. MicroblogsEntryLocalServiceUtil.getMicroblogsEntry(classPK);
  271. ServiceContext serviceContext =
  272. ServiceContextFactory.getInstance(
  273. MicroblogsEntry.class.getName(), actionRequest);
  274. if (cmd.equals(Constants.EDIT) && (microblogsEntryId > 0)) {
  275. microblogsEntry =
  276. MicroblogsEntryServiceUtil.updateMicroblogsEntry(
  277. microblogsEntryId, body,
  278. currentMicroblogsEntry.getSocialRelationType(),
  279. serviceContext);
  280. }
  281. else {
  282. microblogsEntry =
  283. MicroblogsEntryServiceUtil.addMicroblogsEntry(
  284. themeDisplay.getUserId(), body,
  285. MicroblogsEntryConstants.TYPE_REPLY, classPK,
  286. currentMicroblogsEntry.getSocialRelationType(),
  287. serviceContext);
  288. }
  289. }
  290. if (microblogsEntry != null) {
  291. jsonObject = getJSONObject(
  292. microblogsEntry.getMicroblogsEntryId(),
  293. microblogsEntry.getContent(),
  294. microblogsEntry.getModifiedDate(),
  295. microblogsEntry.getUserId(), microblogsEntry.getUserName(),
  296. themeDisplay);
  297. }
  298. jsonObject.put("success", Boolean.TRUE);
  299. }
  300. catch (Exception e) {
  301. jsonObject.put("success", Boolean.FALSE);
  302. }
  303. writeJSON(actionRequest, actionResponse, jsonObject);
  304. }
  305. protected JSONObject getJSONObject(
  306. long mbMessageIdOrMicroblogsEntryId, String body, Date modifiedDate,
  307. long userId, String userName, ThemeDisplay themeDisplay)
  308. throws Exception {
  309. JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
  310. jsonObject.put("body", HtmlUtil.escape(body));
  311. if ((userId <= 0) || (userId != themeDisplay.getUserId())) {
  312. jsonObject.put("commentControlsClass", "hide");
  313. }
  314. jsonObject.put(
  315. "mbMessageIdOrMicroblogsEntryId", mbMessageIdOrMicroblogsEntryId);
  316. Format dateFormat = FastDateFormatFactoryUtil.getDate(
  317. DateFormat.FULL, themeDisplay.getLocale(),
  318. themeDisplay.getTimeZone());
  319. jsonObject.put(
  320. "modifiedDate",
  321. Time.getRelativeTimeDescription(
  322. modifiedDate.getTime(), themeDisplay.getLocale(),
  323. themeDisplay.getTimeZone(), dateFormat));
  324. User user = UserLocalServiceUtil.fetchUser(userId);
  325. if (user != null) {
  326. jsonObject.put("userDisplayURL", user.getDisplayURL(themeDisplay));
  327. jsonObject.put(
  328. "userPortraitURL",
  329. HtmlUtil.escape(user.getPortraitURL(themeDisplay)));
  330. }
  331. jsonObject.put("userName", HtmlUtil.escape(userName));
  332. return jsonObject;
  333. }
  334. }