001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.calendar.social;
016    
017    import com.liferay.portal.kernel.util.HtmlUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.security.permission.PermissionChecker;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portlet.calendar.model.CalEvent;
024    import com.liferay.portlet.calendar.service.CalEventLocalServiceUtil;
025    import com.liferay.portlet.calendar.service.permission.CalEventPermission;
026    import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
027    import com.liferay.portlet.social.model.SocialActivity;
028    import com.liferay.portlet.social.model.SocialActivityFeedEntry;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Ryan Park
033     */
034    public class CalendarActivityInterpreter extends BaseSocialActivityInterpreter {
035    
036            public String[] getClassNames() {
037                    return _CLASS_NAMES;
038            }
039    
040            @Override
041            protected SocialActivityFeedEntry doInterpret(
042                            SocialActivity activity, ThemeDisplay themeDisplay)
043                    throws Exception {
044    
045                    PermissionChecker permissionChecker =
046                            themeDisplay.getPermissionChecker();
047    
048                    if (!CalEventPermission.contains(
049                                    permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
050    
051                            return null;
052                    }
053    
054                    String groupName = StringPool.BLANK;
055    
056                    if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
057                            groupName = getGroupName(activity.getGroupId(), themeDisplay);
058                    }
059    
060                    String creatorUserName = getUserName(
061                            activity.getUserId(), themeDisplay);
062    
063                    int activityType = activity.getType();
064    
065                    // Link
066    
067                    CalEvent event = CalEventLocalServiceUtil.getEvent(
068                            activity.getClassPK());
069    
070                    String link =
071                            themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
072                                    "/calendar/find_event?redirect=" +
073                                            HtmlUtil.escapeURL(themeDisplay.getURLCurrent()) +
074                                                    "&eventId=" + activity.getClassPK();
075    
076                    // Title
077    
078                    String titlePattern = null;
079    
080                    if (activityType == CalendarActivityKeys.ADD_EVENT) {
081                            if (Validator.isNull(groupName)) {
082                                    titlePattern = "activity-calendar-add-event";
083                            }
084                            else {
085                                    titlePattern = "activity-calendar-add-event-in";
086                            }
087                    }
088                    else if (activityType == CalendarActivityKeys.UPDATE_EVENT) {
089                            if (Validator.isNull(groupName)) {
090                                    titlePattern = "activity-calendar-update-event";
091                            }
092                            else {
093                                    titlePattern = "activity-calendar-update-event-in";
094                            }
095                    }
096    
097                    String eventTitle = getValue(
098                            activity.getExtraData(), "title", event.getTitle());
099    
100                    Object[] titleArguments = new Object[] {
101                            groupName, creatorUserName, wrapLink(link, eventTitle)
102                    };
103    
104                    String title = themeDisplay.translate(titlePattern, titleArguments);
105    
106                    // Body
107    
108                    String body = StringPool.BLANK;
109    
110                    return new SocialActivityFeedEntry(link, title, body);
111            }
112    
113            private static final String[] _CLASS_NAMES = new String[] {
114                    CalEvent.class.getName()
115            };
116    
117    }