/**
* 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.knowledge.base.web.internal.search;
import com.liferay.knowledge.base.exception.NoSuchArticleException;
import com.liferay.knowledge.base.model.KBArticle;
import com.liferay.knowledge.base.model.KBFolder;
import com.liferay.knowledge.base.service.KBArticleServiceUtil;
import com.liferay.knowledge.base.service.KBFolderServiceUtil;
import com.liferay.knowledge.base.service.permission.KBArticlePermission;
import com.liferay.knowledge.base.service.permission.KBFolderPermission;
import com.liferay.portal.kernel.dao.search.EmptyOnClickRowChecker;
import com.liferay.portal.kernel.dao.search.RowChecker;
import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
import com.liferay.portal.kernel.security.permission.ActionKeys;
import com.liferay.portal.kernel.security.permission.PermissionChecker;
import com.liferay.portal.kernel.theme.ThemeDisplay;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.kernel.workflow.WorkflowConstants;
import javax.servlet.http.HttpServletRequest;
/**
* @author Roberto Díaz
*/
public class EntriesChecker extends EmptyOnClickRowChecker {
public EntriesChecker(
LiferayPortletRequest liferayPortletRequest,
LiferayPortletResponse liferayPortletResponse) {
super(liferayPortletResponse);
_liferayPortletResponse = liferayPortletResponse;
ThemeDisplay themeDisplay =
(ThemeDisplay)liferayPortletRequest.getAttribute(
WebKeys.THEME_DISPLAY);
_permissionChecker = themeDisplay.getPermissionChecker();
}
@Override
public String getAllRowsCheckBox() {
return null;
}
@Override
public String getAllRowsCheckBox(HttpServletRequest request) {
return null;
}
@Override
public String getRowCheckBox(
HttpServletRequest request, boolean checked, boolean disabled,
String primaryKey) {
KBArticle kbArticle = null;
KBFolder kbFolder = null;
long entryId = GetterUtil.getLong(primaryKey);
try {
kbArticle = KBArticleServiceUtil.getLatestKBArticle(
entryId, WorkflowConstants.STATUS_ANY);
}
catch (Exception e) {
if (e instanceof NoSuchArticleException) {
try {
kbFolder = KBFolderServiceUtil.getKBFolder(entryId);
}
catch (Exception e1) {
return StringPool.BLANK;
}
}
else {
return StringPool.BLANK;
}
}
boolean showInput = false;
String name = null;
if (kbArticle != null) {
name = KBArticle.class.getSimpleName();
try {
if (KBArticlePermission.contains(
_permissionChecker, kbArticle, ActionKeys.DELETE)) {
showInput = true;
}
}
catch (Exception e) {
}
}
else {
name = KBFolder.class.getSimpleName();
try {
if (KBFolderPermission.contains(
_permissionChecker, kbFolder, ActionKeys.DELETE)) {
showInput = true;
}
}
catch (Exception e) {
}
}
if (!showInput) {
return StringPool.BLANK;
}
String checkBoxRowIds = getEntryRowIds();
String checkBoxAllRowIds = "'#" + getAllRowIds() + "'";
String checkBoxPostOnClick =
_liferayPortletResponse.getNamespace() + "toggleActionsButton();";
return getRowCheckBox(
request, checked, disabled,
_liferayPortletResponse.getNamespace() + RowChecker.ROW_IDS + name,
primaryKey, checkBoxRowIds, checkBoxAllRowIds, checkBoxPostOnClick);
}
protected String getEntryRowIds() {
StringBundler sb = new StringBundler(9);
sb.append("['");
sb.append(_liferayPortletResponse.getNamespace());
sb.append(RowChecker.ROW_IDS);
sb.append(KBArticle.class.getSimpleName());
sb.append("', '");
sb.append(_liferayPortletResponse.getNamespace());
sb.append(RowChecker.ROW_IDS);
sb.append(KBFolder.class.getSimpleName());
sb.append("']");
return sb.toString();
}
private final LiferayPortletResponse _liferayPortletResponse;
private final PermissionChecker _permissionChecker;
}