1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.blogs.social;
24  
25  import com.liferay.portal.kernel.json.JSONFactoryUtil;
26  import com.liferay.portal.kernel.json.JSONObject;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.security.permission.ActionKeys;
30  import com.liferay.portal.security.permission.PermissionChecker;
31  import com.liferay.portal.theme.ThemeDisplay;
32  import com.liferay.portlet.blogs.model.BlogsEntry;
33  import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
34  import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
35  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
36  import com.liferay.portlet.social.model.SocialActivity;
37  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
38  
39  /**
40   * <a href="BlogsActivityInterpreter.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Ryan Park
44   *
45   */
46  public class BlogsActivityInterpreter extends BaseSocialActivityInterpreter {
47  
48      public String[] getClassNames() {
49          return _CLASS_NAMES;
50      }
51  
52      protected SocialActivityFeedEntry doInterpret(
53              SocialActivity activity, ThemeDisplay themeDisplay)
54          throws Exception {
55  
56          PermissionChecker permissionChecker =
57              themeDisplay.getPermissionChecker();
58  
59          if (!BlogsEntryPermission.contains(
60                  permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
61  
62              return null;
63          }
64  
65          String groupName = StringPool.BLANK;
66  
67          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
68              groupName = getGroupName(activity.getGroupId(), themeDisplay);
69          }
70  
71          String creatorUserName = getUserName(
72              activity.getUserId(), themeDisplay);
73          String receiverUserName = getUserName(
74              activity.getReceiverUserId(), themeDisplay);
75  
76          int activityType = activity.getType();
77  
78          JSONObject extraData = null;
79  
80          if (Validator.isNotNull(activity.getExtraData())) {
81              extraData = JSONFactoryUtil.createJSONObject(
82                  activity.getExtraData());
83          }
84  
85          // Link
86  
87          BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(
88              activity.getClassPK());
89  
90          String link =
91              themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
92                  "/blogs/find_entry?entryId=" + activity.getClassPK();
93  
94          // Title
95  
96          String titlePattern = null;
97  
98          if (activityType == BlogsActivityKeys.ADD_COMMENT) {
99              titlePattern = "activity-blogs-add-comment";
100         }
101         else if (activityType == BlogsActivityKeys.ADD_ENTRY) {
102             titlePattern = "activity-blogs-add-entry";
103         }
104 
105         if (Validator.isNotNull(groupName)) {
106             titlePattern += "-in";
107         }
108 
109         String entryTitle = wrapLink(link, cleanContent(entry.getTitle()));
110 
111         Object[] titleArguments = new Object[] {
112             groupName, creatorUserName, receiverUserName, entryTitle
113         };
114 
115         String title = themeDisplay.translate(titlePattern, titleArguments);
116 
117         // Body
118 
119         String body = StringPool.BLANK;
120 
121         return new SocialActivityFeedEntry(link, title, body);
122     }
123 
124     private static final String[] _CLASS_NAMES = new String[] {
125         BlogsEntry.class.getName()
126     };
127 
128 }