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.calendar.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.calendar.model.CalEvent;
30  import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
31  import com.liferay.portlet.calendar.service.permission.CalEventPermission;
32  import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
33  import com.liferay.portlet.social.model.SocialActivity;
34  import com.liferay.portlet.social.model.SocialActivityFeedEntry;
35  
36  /**
37   * <a href="CalendarActivityInterpreter.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class CalendarActivityInterpreter extends BaseSocialActivityInterpreter {
43  
44      public String[] getClassNames() {
45          return _CLASS_NAMES;
46      }
47  
48      protected SocialActivityFeedEntry doInterpret(
49              SocialActivity activity, ThemeDisplay themeDisplay)
50          throws Exception {
51  
52          PermissionChecker permissionChecker =
53              themeDisplay.getPermissionChecker();
54  
55          if (!CalEventPermission.contains(
56                  permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
57  
58              return null;
59          }
60  
61          String creatorUserName = getUserName(
62              activity.getUserId(), themeDisplay);
63  
64          int activityType = activity.getType();
65  
66          // Link
67  
68          CalEvent event = CalEventLocalServiceUtil.getEvent(
69              activity.getClassPK());
70  
71          String link =
72              themeDisplay.getURLPortal() + themeDisplay.getPathMain() +
73                  "/calendar/find_event?eventId=" + activity.getClassPK();
74  
75          // Title
76  
77          String groupName = StringPool.BLANK;
78  
79          if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
80              Group group = GroupLocalServiceUtil.getGroup(activity.getGroupId());
81  
82              groupName = group.getDescriptiveName();
83          }
84  
85          String titlePattern = null;
86  
87          if (activityType == CalendarActivityKeys.ADD_EVENT) {
88              titlePattern = "activity-calendar-add-event";
89          }
90          else if (activityType == CalendarActivityKeys.UPDATE_EVENT) {
91              titlePattern = "activity-calendar-update-event";
92          }
93  
94          if (Validator.isNotNull(groupName)) {
95              titlePattern += "-in";
96          }
97  
98          Object[] titleArguments = new Object[] {creatorUserName, groupName};
99  
100         String title = themeDisplay.translate(titlePattern, titleArguments);
101 
102         // Body
103 
104         StringBuilder sb = new StringBuilder();
105 
106         sb.append("<a href=\"");
107         sb.append(link);
108         sb.append("\">");
109         sb.append(cleanContent(event.getTitle()));
110         sb.append("</a><br />");
111         sb.append(cleanContent(event.getDescription()));
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         CalEvent.class.getName()
120     };
121 
122 }