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.blogs.social;
21  
22  import com.liferay.portal.kernel.json.JSONFactoryUtil;
23  import com.liferay.portal.kernel.json.JSONObject;
24  import com.liferay.portal.kernel.util.StringPool;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.Group;
27  import com.liferay.portal.security.permission.ActionKeys;
28  import com.liferay.portal.security.permission.PermissionChecker;
29  import com.liferay.portal.service.GroupLocalServiceUtil;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portlet.blogs.model.BlogsEntry;
32  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
33  import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
34  import com.liferay.portlet.messageboards.NoSuchMessageException;
35  import com.liferay.portlet.messageboards.model.MBMessage;
36  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
37  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
38  import com.liferay.portlet.social.model.SocialActivity;
39  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
40  import com.liferay.portlet.social.service.SocialActivityLocalServiceUtil;
41  
42  /**
43   * <a href="BlogsActivityInterpreter.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class BlogsActivityInterpreter extends BaseSocialActivityInterpreter {
49  
50      public String[] getClassNames() {
51          return _CLASS_NAMES;
52      }
53  
54      protected SocialActivityFeedEntry doInterpret(
55              SocialActivity activity, ThemeDisplay themeDisplay)
56          throws Exception {
57  
58          PermissionChecker permissionChecker =
59              themeDisplay.getPermissionChecker();
60  
61          if (!BlogsEntryPermission.contains(
62                  permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
63  
64              return null;
65          }
66  
67          String creatorUserName = getUserName(
68              activity.getUserId(), themeDisplay);
69          String receiverUserName = getUserName(
70              activity.getReceiverUserId(), themeDisplay);
71  
72          int activityType = activity.getType();
73  
74          JSONObject extraData = null;
75  
76          if (Validator.isNotNull(activity.getExtraData())) {
77              extraData = JSONFactoryUtil.createJSONObject(
78                  activity.getExtraData());
79          }
80  
81          // Link
82  
83          BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(
84              activity.getClassPK());
85  
86          String link =
87              themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
88                  "/blogs/find_entry?entryId=" + activity.getClassPK();
89  
90          // Title
91  
92          String groupName = StringPool.BLANK;
93  
94          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
95              Group group = GroupLocalServiceUtil.getGroup(activity.getGroupId());
96  
97              groupName = group.getDescriptiveName();
98          }
99  
100         String titlePattern = null;
101         Object[] titleArguments = null;
102 
103         if (activityType == BlogsActivityKeys.ADD_COMMENT) {
104             titlePattern = "activity-blogs-add-comment";
105 
106             if (Validator.isNotNull(groupName)) {
107                 titlePattern += "-in";
108             }
109 
110             titleArguments = new Object[] {
111                 creatorUserName, receiverUserName, groupName
112             };
113         }
114         else if (activityType == BlogsActivityKeys.ADD_ENTRY) {
115             titlePattern = "activity-blogs-add-entry";
116 
117             if (Validator.isNotNull(groupName)) {
118                 titlePattern += "-in";
119             }
120 
121             titleArguments = new Object[] {creatorUserName, groupName};
122         }
123 
124         String title = themeDisplay.translate(titlePattern, titleArguments);
125 
126         // Body
127 
128         StringBuilder sb = new StringBuilder();
129 
130         sb.append("<a href=\"");
131         sb.append(link);
132         sb.append("\">");
133 
134         if (activityType == BlogsActivityKeys.ADD_COMMENT) {
135             long messageId = extraData.getInt("messageId");
136 
137             try {
138                 MBMessage message = MBMessageLocalServiceUtil.getMessage(
139                     messageId);
140 
141                 sb.append(cleanContent(message.getBody()));
142             }
143             catch (NoSuchMessageException nsme) {
144                 SocialActivityLocalServiceUtil.deleteActivity(
145                     activity.getActivityId());
146 
147                 return null;
148             }
149         }
150         else if (activityType == BlogsActivityKeys.ADD_ENTRY) {
151             sb.append(entry.getTitle());
152         }
153 
154         sb.append("</a><br />");
155 
156         if (activityType == BlogsActivityKeys.ADD_ENTRY) {
157             sb.append(cleanContent(entry.getContent()));
158         }
159 
160         String body = sb.toString();
161 
162         return new SocialActivityFeedEntry(link, title, body);
163     }
164 
165     private static final String[] _CLASS_NAMES = new String[] {
166         BlogsEntry.class.getName()
167     };
168 
169 }