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.wiki.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.social.model.BaseSocialActivityInterpreter;
30  import com.liferay.portlet.social.model.SocialActivity;
31  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
32  import com.liferay.portlet.wiki.model.WikiPage;
33  import com.liferay.portlet.wiki.model.WikiPageResource;
34  import com.liferay.portlet.wiki.service.WikiPageResourceLocalServiceUtil;
35  import com.liferay.portlet.wiki.service.permission.WikiPagePermission;
36  
37  /**
38   * <a href="WikiActivityInterpreter.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Samuel Kong
41   *
42   */
43  public class WikiActivityInterpreter 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 (!WikiPagePermission.contains(
57                  permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
58  
59              return null;
60          }
61  
62          String creatorUserName = getUserName(
63              activity.getUserId(), themeDisplay);
64  
65          int activityType = activity.getType();
66  
67          // Link
68  
69          WikiPageResource pageResource =
70              WikiPageResourceLocalServiceUtil.getPageResource(
71                  activity.getClassPK());
72  
73          String link =
74              themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
75                  "/wiki/find_page?pageResourcePrimKey=" + activity.getClassPK();
76  
77          // Title
78  
79          String groupName = StringPool.BLANK;
80  
81          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
82              Group group = GroupLocalServiceUtil.getGroup(activity.getGroupId());
83              groupName = group.getDescriptiveName();
84          }
85  
86          String titlePattern = null;
87  
88          if (activityType == WikiActivityKeys.ADD_PAGE) {
89              titlePattern = "activity-wiki-add-page";
90          }
91          else if (activityType == WikiActivityKeys.UPDATE_PAGE) {
92              titlePattern = "activity-wiki-update-page";
93          }
94  
95          if (Validator.isNotNull(groupName)) {
96              titlePattern += "-in";
97          }
98  
99          Object[] titleArguments = new Object[] {creatorUserName, groupName};
100 
101         String title = themeDisplay.translate(titlePattern, titleArguments);
102 
103         // Body
104 
105         StringBuilder sb = new StringBuilder();
106 
107         sb.append("<a href=\"");
108         sb.append(link);
109         sb.append("\">");
110         sb.append(cleanContent(pageResource.getTitle()));
111         sb.append("</a>");
112 
113         String body = sb.toString();
114 
115         return new SocialActivityFeedEntry(link, title, body);
116     }
117 
118     private static final String[] _CLASS_NAMES = new String[] {
119         WikiPage.class.getName()
120     };
121 
122 }