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