001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.blogs.social;
016    
017    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.security.permission.ActionKeys;
022    import com.liferay.portal.security.permission.PermissionChecker;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portlet.blogs.model.BlogsEntry;
025    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
026    import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
027    import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
028    import com.liferay.portlet.social.model.SocialActivity;
029    import com.liferay.portlet.social.model.SocialActivityConstants;
030    import com.liferay.portlet.social.model.SocialActivityFeedEntry;
031    
032    import java.text.Format;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     * @author Ryan Park
037     */
038    public class BlogsActivityInterpreter extends BaseSocialActivityInterpreter {
039    
040            public String[] getClassNames() {
041                    return _CLASS_NAMES;
042            }
043    
044            @Override
045            protected SocialActivityFeedEntry doInterpret(
046                            SocialActivity activity, ThemeDisplay themeDisplay)
047                    throws Exception {
048    
049                    PermissionChecker permissionChecker =
050                            themeDisplay.getPermissionChecker();
051    
052                    if (!BlogsEntryPermission.contains(
053                                    permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
054    
055                            return null;
056                    }
057    
058                    String groupName = StringPool.BLANK;
059    
060                    if (activity.getGroupId() != themeDisplay.getScopeGroupId()) {
061                            groupName = getGroupName(activity.getGroupId(), themeDisplay);
062                    }
063    
064                    String creatorUserName = getUserName(
065                            activity.getUserId(), themeDisplay);
066                    String receiverUserName = getUserName(
067                            activity.getReceiverUserId(), themeDisplay);
068    
069                    int activityType = activity.getType();
070    
071                    // Link
072    
073                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(
074                            activity.getClassPK());
075    
076                    String link =
077                            themeDisplay.getPortalURL() + themeDisplay.getPathMain() +
078                                    "/blogs/find_entry?entryId=" + activity.getClassPK();
079    
080                    // Title
081    
082                    String entryTitle = getValue(
083                            activity.getExtraData(), "title", entry.getTitle());
084    
085                    String displayTitle = wrapLink(link, entryTitle);
086                    String displayDate = StringPool.BLANK;
087    
088                    String titlePattern = null;
089    
090                    if ((activityType == BlogsActivityKeys.ADD_COMMENT) ||
091                            (activityType == SocialActivityConstants.TYPE_ADD_COMMENT)) {
092    
093                            if (Validator.isNull(groupName)) {
094                                    titlePattern = "activity-blogs-add-comment";
095                            }
096                            else {
097                                    titlePattern = "activity-blogs-add-comment-in";
098                            }
099                    }
100                    else if (activityType == BlogsActivityKeys.ADD_ENTRY) {
101                            if (entry.getStatus() == WorkflowConstants.STATUS_SCHEDULED) {
102                                    if (Validator.isNull(groupName)) {
103                                            titlePattern = "activity-blogs-scheduled-entry";
104                                    }
105                                    else {
106                                            titlePattern = "activity-blogs-scheduled-entry-in";
107                                    }
108    
109                                    Format dateFormatDate =
110                                            FastDateFormatFactoryUtil.getSimpleDateFormat(
111                                                    "MMMM d", themeDisplay.getLocale(),
112                                                    themeDisplay.getTimeZone());
113    
114                                    displayDate = dateFormatDate.format(entry.getDisplayDate());
115    
116                                    displayTitle = entryTitle;
117                            }
118                            else {
119                                    if (Validator.isNull(groupName)) {
120                                            titlePattern = "activity-blogs-add-entry";
121                                    }
122                                    else {
123                                            titlePattern = "activity-blogs-add-entry-in";
124                                    }
125                            }
126                    }
127                    else if (activityType == BlogsActivityKeys.UPDATE_ENTRY) {
128                            if (Validator.isNull(groupName)) {
129                                    titlePattern = "activity-blogs-update-entry";
130                            }
131                            else {
132                                    titlePattern = "activity-blogs-update-entry-in";
133                            }
134                    }
135    
136                    Object[] titleArguments = new Object[] {
137                            groupName, creatorUserName, receiverUserName, displayTitle,
138                            displayDate
139                    };
140    
141                    String title = themeDisplay.translate(titlePattern, titleArguments);
142    
143                    // Body
144    
145                    String body = StringPool.BLANK;
146    
147                    return new SocialActivityFeedEntry(link, title, body);
148            }
149    
150            private static final String[] _CLASS_NAMES = new String[] {
151                    BlogsEntry.class.getName()
152            };
153    
154    }