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.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.PermissionChecker;
022    import com.liferay.portal.service.ServiceContext;
023    import com.liferay.portlet.blogs.model.BlogsEntry;
024    import com.liferay.portlet.blogs.service.BlogsEntryLocalServiceUtil;
025    import com.liferay.portlet.blogs.service.permission.BlogsEntryPermission;
026    import com.liferay.portlet.social.model.BaseSocialActivityInterpreter;
027    import com.liferay.portlet.social.model.SocialActivity;
028    import com.liferay.portlet.social.model.SocialActivityConstants;
029    
030    import java.text.Format;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Ryan Park
035     * @author Zsolt Berentey
036     */
037    public class BlogsActivityInterpreter extends BaseSocialActivityInterpreter {
038    
039            public String[] getClassNames() {
040                    return _CLASS_NAMES;
041            }
042    
043            @Override
044            protected String getPath(
045                    SocialActivity activity, ServiceContext serviceContext) {
046    
047                    return "/blogs/find_entry?entryId=" + activity.getClassPK();
048            }
049    
050            @Override
051            protected Object[] getTitleArguments(
052                            String groupName, SocialActivity activity, String link,
053                            String title, ServiceContext serviceContext)
054                    throws Exception {
055    
056                    String creatorUserName = getUserName(
057                            activity.getUserId(), serviceContext);
058                    String receiverUserName = getUserName(
059                            activity.getReceiverUserId(), serviceContext);
060    
061                    BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(
062                            activity.getClassPK());
063    
064                    String displayDate = StringPool.BLANK;
065    
066                    if ((activity.getType() == BlogsActivityKeys.ADD_ENTRY) &&
067                            (entry.getStatus() == WorkflowConstants.STATUS_SCHEDULED)) {
068    
069                            link = null;
070    
071                            Format dateFormatDate =
072                                    FastDateFormatFactoryUtil.getSimpleDateFormat(
073                                            "MMMM d", serviceContext.getLocale(),
074                                            serviceContext.getTimeZone());
075    
076                            displayDate = dateFormatDate.format(entry.getDisplayDate());
077                    }
078    
079                    String displayTitle = wrapLink(link, entry.getTitle());
080    
081                    return new Object[] {
082                            groupName, creatorUserName, receiverUserName, displayTitle,
083                            displayDate
084                    };
085            }
086    
087            @Override
088            protected String getTitlePattern(String groupName, SocialActivity activity)
089                    throws Exception {
090    
091                    int activityType = activity.getType();
092    
093                    if ((activityType == BlogsActivityKeys.ADD_COMMENT) ||
094                            (activityType == SocialActivityConstants.TYPE_ADD_COMMENT)) {
095    
096                            if (Validator.isNull(groupName)) {
097                                    return "activity-blogs-entry-add-comment";
098                            }
099                            else {
100                                    return "activity-blogs-entry-add-comment-in";
101                            }
102                    }
103                    else if (activityType == BlogsActivityKeys.ADD_ENTRY) {
104                            BlogsEntry entry = BlogsEntryLocalServiceUtil.getEntry(
105                                    activity.getClassPK());
106    
107                            if (entry.getStatus() == WorkflowConstants.STATUS_SCHEDULED) {
108                                    if (Validator.isNull(groupName)) {
109                                            return "activity-blogs-entry-schedule-entry";
110                                    }
111                                    else {
112                                            return "activity-blogs-entry-schedule-entry-in";
113                                    }
114                            }
115                            else {
116                                    if (Validator.isNull(groupName)) {
117                                            return "activity-blogs-entry-add-entry";
118                                    }
119                                    else {
120                                            return "activity-blogs-entry-add-entry-in";
121                                    }
122                            }
123                    }
124                    else if (activityType == SocialActivityConstants.TYPE_MOVE_TO_TRASH) {
125                            if (Validator.isNull(groupName)) {
126                                    return "activity-blogs-entry-move-to-trash";
127                            }
128                            else {
129                                    return "activity-blogs-entry-move-to-trash-in";
130                            }
131                    }
132                    else if (activityType ==
133                                            SocialActivityConstants.TYPE_RESTORE_FROM_TRASH) {
134    
135                            if (Validator.isNull(groupName)) {
136                                    return "activity-blogs-entry-restore-from-trash";
137                            }
138                            else {
139                                    return "activity-blogs-entry-restore-from-trash-in";
140                            }
141                    }
142                    else if (activityType == BlogsActivityKeys.UPDATE_ENTRY) {
143                            if (Validator.isNull(groupName)) {
144                                    return "activity-blogs-entry-update-entry";
145                            }
146                            else {
147                                    return "activity-blogs-entry-update-entry-in";
148                            }
149                    }
150    
151                    return null;
152            }
153    
154            @Override
155            protected boolean hasPermissions(
156                            PermissionChecker permissionChecker, SocialActivity activity,
157                            String actionId, ServiceContext serviceContext)
158                    throws Exception {
159    
160                    return BlogsEntryPermission.contains(
161                            permissionChecker, activity.getClassPK(), actionId);
162            }
163    
164            private static final String[] _CLASS_NAMES = {BlogsEntry.class.getName()};
165    
166    }