001    /**
002     * Copyright (c) 2000-2013 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            @Override
037            public String[] getClassNames() {
038                    return _CLASS_NAMES;
039            }
040    
041            @Override
042            protected SocialActivityFeedEntry doInterpret(
043                            SocialActivity activity, ThemeDisplay themeDisplay)
044                    throws Exception {
045    
046                    PermissionChecker permissionChecker =
047                            themeDisplay.getPermissionChecker();
048    
049                    if (!CalEventPermission.contains(
050                                    permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
051    
052                            return null;
053                    }
054    
055                    String groupName = StringPool.BLANK;
056    
057                    if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
058                            groupName = getGroupName(activity.getGroupId(), themeDisplay);
059                    }
060    
061                    String creatorUserName = getUserName(
062                            activity.getUserId(), themeDisplay);
063    
064                    int activityType = activity.getType();
065    
066                    // Link
067    
068                    CalEvent event = CalEventLocalServiceUtil.getEvent(
069                            activity.getClassPK());
070    
071                    String link =
072                            themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
073                                    "/calendar/find_event?redirect=" +
074                                            HtmlUtil.escapeURL(themeDisplay.getURLCurrent()) +
075                                                    "&eventId=" + activity.getClassPK();
076    
077                    // Title
078    
079                    String titlePattern = null;
080    
081                    if (activityType == CalendarActivityKeys.ADD_EVENT) {
082                            titlePattern = "activity-calendar-add-event";
083                    }
084                    else if (activityType == CalendarActivityKeys.UPDATE_EVENT) {
085                            titlePattern = "activity-calendar-update-event";
086                    }
087    
088                    if (Validator.isNotNull(groupName)) {
089                            titlePattern += "-in";
090                    }
091    
092                    String eventTitle = getValue(
093                            activity.getExtraData(), "title", event.getTitle());
094    
095                    Object[] titleArguments = new Object[] {
096                            groupName, creatorUserName, wrapLink(link, eventTitle)
097                    };
098    
099                    String title = themeDisplay.translate(titlePattern, titleArguments);
100    
101                    // Body
102    
103                    String body = StringPool.BLANK;
104    
105                    return new SocialActivityFeedEntry(link, title, body);
106            }
107    
108            private static final String[] _CLASS_NAMES = new String[] {
109                    CalEvent.class.getName()
110            };
111    
112    }