/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/
package com.liferay.wiki.web.internal.social;
import com.liferay.portal.kernel.exception.NoSuchModelException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.portletfilerepository.PortletFileRepositoryUtil;
import com.liferay.portal.kernel.repository.model.FileEntry;
import com.liferay.portal.kernel.security.permission.ActionKeys;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.service.ServiceContext;
import com.liferay.portal.kernel.util.AggregateResourceBundleLoader;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.ResourceBundleLoader;
import com.liferay.portal.kernel.util.ResourceBundleLoaderUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.URLCodec;
import com.liferay.portal.kernel.util.Validator;
import com.liferay.social.kernel.model.BaseSocialActivityInterpreter;
import com.liferay.social.kernel.model.SocialActivity;
import com.liferay.social.kernel.model.SocialActivityConstants;
import com.liferay.social.kernel.model.SocialActivityInterpreter;
import com.liferay.wiki.constants.WikiPortletKeys;
import com.liferay.wiki.model.WikiPage;
import com.liferay.wiki.model.WikiPageResource;
import com.liferay.wiki.service.WikiPageLocalService;
import com.liferay.wiki.service.WikiPageResourceLocalService;
import com.liferay.wiki.service.permission.WikiPagePermissionChecker;
import com.liferay.wiki.social.WikiActivityKeys;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
/**
* @author Samuel Kong
* @author Ryan Park
* @author Zsolt Berentey
*/
@Component(
property = {"javax.portlet.name=" + WikiPortletKeys.WIKI},
service = SocialActivityInterpreter.class
)
public class WikiActivityInterpreter extends BaseSocialActivityInterpreter {
@Override
public String[] getClassNames() {
return _CLASS_NAMES;
}
protected String getAttachmentTitle(
SocialActivity activity, WikiPageResource pageResource,
ServiceContext serviceContext)
throws Exception {
int activityType = activity.getType();
if ((activityType == SocialActivityConstants.TYPE_ADD_ATTACHMENT) ||
(activityType ==
SocialActivityConstants.TYPE_MOVE_ATTACHMENT_TO_TRASH) ||
(activityType ==
SocialActivityConstants.TYPE_RESTORE_ATTACHMENT_FROM_TRASH)) {
String link = null;
FileEntry fileEntry = null;
try {
long fileEntryId = GetterUtil.getLong(
activity.getExtraDataValue("fileEntryId"));
fileEntry = PortletFileRepositoryUtil.getPortletFileEntry(
fileEntryId);
}
catch (NoSuchModelException nsme) {
// LPS-52675
if (_log.isDebugEnabled()) {
_log.debug(nsme, nsme);
}
}
String fileEntryTitle = activity.getExtraDataValue(
"fileEntryTitle");
if ((fileEntry != null) && !fileEntry.isInTrash()) {
StringBundler sb = new StringBundler(9);
sb.append(serviceContext.getPathMain());
sb.append("/wiki/get_page_attachment?p_l_id=");
sb.append(serviceContext.getPlid());
sb.append("&nodeId=");
sb.append(pageResource.getNodeId());
sb.append("&title=");
sb.append(URLCodec.encodeURL(pageResource.getTitle()));
sb.append("&fileName=");
sb.append(fileEntryTitle);
link = sb.toString();
}
return wrapLink(link, fileEntryTitle);
}
return StringPool.BLANK;
}
@Override
protected String getPath(
SocialActivity activity, ServiceContext serviceContext) {
return "/wiki/find_page?pageResourcePrimKey=" + activity.getClassPK();
}
@Override
protected ResourceBundleLoader getResourceBundleLoader() {
return _resourceBundleLoader;
}
@Override
protected Object[] getTitleArguments(
String groupName, SocialActivity activity, String link,
String title, ServiceContext serviceContext)
throws Exception {
WikiPageResource pageResource =
_wikiPageResourceLocalService.fetchWikiPageResource(
activity.getClassPK());
if (pageResource == null) {
return null;
}
String creatorUserName = getUserName(
activity.getUserId(), serviceContext);
title = wrapLink(link, title);
return new Object[] {
groupName, creatorUserName, title,
getAttachmentTitle(activity, pageResource, serviceContext)
};
}
@Override
protected String getTitlePattern(
String groupName, SocialActivity activity) {
int activityType = activity.getType();
if ((activityType == WikiActivityKeys.ADD_COMMENT) ||
(activityType == SocialActivityConstants.TYPE_ADD_COMMENT)) {
if (Validator.isNull(groupName)) {
return "activity-wiki-page-add-comment";
}
else {
return "activity-wiki-page-add-comment-in";
}
}
else if (activityType == WikiActivityKeys.ADD_PAGE) {
if (Validator.isNull(groupName)) {
return "activity-wiki-page-add-page";
}
else {
return "activity-wiki-page-add-page-in";
}
}
else if (activityType == SocialActivityConstants.TYPE_ADD_ATTACHMENT) {
if (Validator.isNull(groupName)) {
return "activity-wiki-page-add-attachment";
}
else {
return "activity-wiki-page-add-attachment-in";
}
}
else if (activityType ==
SocialActivityConstants.TYPE_MOVE_ATTACHMENT_TO_TRASH) {
if (Validator.isNull(groupName)) {
return "activity-wiki-page-remove-attachment";
}
else {
return "activity-wiki-page-remove-attachment-in";
}
}
else if (activityType ==
SocialActivityConstants.
TYPE_RESTORE_ATTACHMENT_FROM_TRASH) {
if (Validator.isNull(groupName)) {
return "activity-wiki-page-restore-attachment";
}
else {
return "activity-wiki-page-restore-attachment-in";
}
}
else if (activityType == SocialActivityConstants.TYPE_MOVE_TO_TRASH) {
if (Validator.isNull(groupName)) {
return "activity-wiki-page-move-to-trash";
}
else {
return "activity-wiki-page-move-to-trash-in";
}
}
else if (activityType ==
SocialActivityConstants.TYPE_RESTORE_FROM_TRASH) {
if (Validator.isNull(groupName)) {
return "activity-wiki-page-restore-from-trash";
}
else {
return "activity-wiki-page-restore-from-trash-in";
}
}
else if (activityType == WikiActivityKeys.UPDATE_PAGE) {
if (Validator.isNull(groupName)) {
return "activity-wiki-page-update-page";
}
else {
return "activity-wiki-page-update-page-in";
}
}
return null;
}
@Override
protected boolean hasPermissions(
PermissionChecker permissionChecker, SocialActivity activity,
String actionId, ServiceContext serviceContext)
throws Exception {
if (!WikiPagePermissionChecker.contains(
permissionChecker, activity.getClassPK(), ActionKeys.VIEW)) {
return false;
}
int activityType = activity.getType();
if (activityType == WikiActivityKeys.UPDATE_PAGE) {
WikiPageResource pageResource =
_wikiPageResourceLocalService.getPageResource(
activity.getClassPK());
double version = GetterUtil.getDouble(
activity.getExtraDataValue("version"));
WikiPage page = _wikiPageLocalService.getPage(
pageResource.getNodeId(), pageResource.getTitle(), version);
if (!page.isApproved() &&
!WikiPagePermissionChecker.contains(
permissionChecker, activity.getClassPK(),
ActionKeys.UPDATE)) {
return false;
}
}
return true;
}
@Reference(
target = "(bundle.symbolic.name=com.liferay.wiki.web)", unbind = "-"
)
protected void setResourceBundleLoader(
ResourceBundleLoader resourceBundleLoader) {
_resourceBundleLoader = new AggregateResourceBundleLoader(
resourceBundleLoader,
ResourceBundleLoaderUtil.getPortalResourceBundleLoader());
}
@Reference(unbind = "-")
protected void setWikiPageLocalService(
WikiPageLocalService wikiPageLocalService) {
_wikiPageLocalService = wikiPageLocalService;
}
@Reference(unbind = "-")
protected void setWikiPageResourceLocalService(
WikiPageResourceLocalService wikiPageResourceLocalService) {
_wikiPageResourceLocalService = wikiPageResourceLocalService;
}
private static final String[] _CLASS_NAMES = {WikiPage.class.getName()};
private static final Log _log = LogFactoryUtil.getLog(
WikiActivityInterpreter.class);
private ResourceBundleLoader _resourceBundleLoader;
private WikiPageLocalService _wikiPageLocalService;
private WikiPageResourceLocalService _wikiPageResourceLocalService;
}