1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards.social;
21  
22  import com.liferay.portal.kernel.util.StringPool;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.model.Group;
25  import com.liferay.portal.security.permission.ActionKeys;
26  import com.liferay.portal.security.permission.PermissionChecker;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portlet.messageboards.model.MBMessage;
30  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
31  import com.liferay.portlet.messageboards.service.permission.MBMessagePermission;
32  import com.liferay.portlet.messageboards.util.BBCodeUtil;
33  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
34  import com.liferay.portlet.social.model.SocialActivity;
35  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
36  
37  /**
38   * <a href="MBActivityInterpreter.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class MBActivityInterpreter extends BaseSocialActivityInterpreter {
44  
45      public String[] getClassNames() {
46          return _CLASS_NAMES;
47      }
48  
49      protected SocialActivityFeedEntry doInterpret(
50              SocialActivity activity, ThemeDisplay themeDisplay)
51          throws Exception {
52  
53          PermissionChecker permissionChecker =
54              themeDisplay.getPermissionChecker();
55  
56          if (!MBMessagePermission.contains(
57                  permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
58  
59              return null;
60          }
61  
62          String creatorUserName = getUserName(
63              activity.getUserId(), themeDisplay);
64          String receiverUserName = getUserName(
65              activity.getReceiverUserId(), themeDisplay);
66  
67          int activityType = activity.getType();
68  
69          // Link
70  
71          MBMessage message = MBMessageLocalServiceUtil.getMessage(
72              activity.getClassPK());
73  
74          String link =
75              themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
76                  "/message_boards/find_message?messageId=" +
77                      activity.getClassPK();
78  
79          // Title
80  
81          String groupName = StringPool.BLANK;
82  
83          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
84              Group group = GroupLocalServiceUtil.getGroup(activity.getGroupId());
85  
86              groupName = group.getDescriptiveName();
87          }
88  
89          String titlePattern = null;
90          Object[] titleArguments = null;
91  
92          if (activityType == MBActivityKeys.ADD_MESSAGE) {
93              titlePattern = "activity-message-boards-add-message";
94  
95              if (Validator.isNotNull(groupName)) {
96                  titlePattern += "-in";
97              }
98  
99              titleArguments = new Object[] {creatorUserName, groupName};
100         }
101         else if (activityType == MBActivityKeys.REPLY_MESSAGE) {
102             titlePattern = "activity-message-boards-reply-message";
103 
104             if (Validator.isNotNull(groupName)) {
105                 titlePattern += "-in";
106             }
107 
108             titleArguments = new Object[] {
109                 creatorUserName, receiverUserName, groupName
110             };
111         }
112 
113         String title = themeDisplay.translate(titlePattern, titleArguments);
114 
115         // Body
116 
117         StringBuilder sb = new StringBuilder();
118 
119         sb.append("<a href=\"");
120         sb.append(link);
121         sb.append("\">");
122         sb.append(cleanContent(message.getSubject()));
123         sb.append("</a><br />");
124         sb.append(cleanContent(BBCodeUtil.getHTML(message)));
125 
126         String body = sb.toString();
127 
128         return new SocialActivityFeedEntry(link, title, body);
129     }
130 
131     private static final String[] _CLASS_NAMES = new String[] {
132         MBMessage.class.getName()
133     };
134 
135 }