protected long getResourcePrimKey(RenderRequest renderRequest) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);

    PortletPreferences preferences = renderRequest.getPreferences();

    long defaultValue = GetterUtil.getLong(preferences.getValue("resourcePrimKey", null));

    String mvcPath = ParamUtil.getString(renderRequest, "mvcPath");

    if (((defaultValue == 0) && mvcPath.equals(viewTemplate))
        || mvcPath.equals("/display/select_configuration_article.jsp")) {

      return 0;
    }

    long resourcePrimKey = ParamUtil.getLong(renderRequest, "resourcePrimKey", defaultValue);

    if ((resourcePrimKey == 0) || (resourcePrimKey != defaultValue)) {
      return resourcePrimKey;
    }

    PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();

    if (!KBArticlePermission.contains(permissionChecker, defaultValue, ActionKeys.VIEW)) {

      return 0;
    }

    return defaultValue;
  }
  protected int getStatus(RenderRequest renderRequest) throws Exception {
    ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);

    if (!themeDisplay.isSignedIn()) {
      return WorkflowConstants.STATUS_APPROVED;
    }

    String value = renderRequest.getParameter("status");
    int status = GetterUtil.getInteger(value);

    if ((value != null) && (status == WorkflowConstants.STATUS_APPROVED)) {
      return WorkflowConstants.STATUS_APPROVED;
    }

    long resourcePrimKey = getResourcePrimKey(renderRequest);

    if (resourcePrimKey == 0) {
      return WorkflowConstants.STATUS_APPROVED;
    }

    PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();

    if (KBArticlePermission.contains(permissionChecker, resourcePrimKey, ActionKeys.UPDATE)) {

      return ParamUtil.getInteger(renderRequest, "status", WorkflowConstants.STATUS_ANY);
    }

    return WorkflowConstants.STATUS_APPROVED;
  }
  @Override
  public boolean hasPermission(
      PermissionChecker permissionChecker,
      String entryClassName,
      long entryClassPK,
      String actionId)
      throws Exception {

    return KBArticlePermission.contains(permissionChecker, entryClassPK, ActionKeys.VIEW);
  }
  protected KBArticle getKBArticle(long resourcePrimKey, int status) throws Exception {

    KBArticle kbArticle = KBArticleLocalServiceUtil.fetchLatestKBArticle(resourcePrimKey, status);

    if (kbArticle == null) {
      return null;
    }

    PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();

    if (!KBArticlePermission.contains(permissionChecker, kbArticle, ActionKeys.VIEW)) {

      return null;
    }

    return kbArticle;
  }
  protected int getStatus(RenderRequest renderRequest) throws Exception {
    ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);

    if (!themeDisplay.isSignedIn()) {
      return WorkflowConstants.STATUS_APPROVED;
    }

    String statusString = renderRequest.getParameter("status");

    int status = GetterUtil.getInteger(statusString);

    if ((statusString != null) && (status == WorkflowConstants.STATUS_APPROVED)) {

      return WorkflowConstants.STATUS_APPROVED;
    }

    Tuple resourceTuple = getResourceTuple(renderRequest);

    long resourcePrimKey = (Long) resourceTuple.getObject(1);

    if (resourcePrimKey == KBFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
      return WorkflowConstants.STATUS_APPROVED;
    }

    long resourceClassNameId = (Long) resourceTuple.getObject(0);

    long kbArticleClassNameId = PortalUtil.getClassNameId(KBArticleConstants.getClassName());

    if (resourceClassNameId == kbArticleClassNameId) {
      KBArticle kbArticle =
          KBArticleServiceUtil.fetchLatestKBArticle(resourcePrimKey, WorkflowConstants.STATUS_ANY);

      if (kbArticle == null) {
        return WorkflowConstants.STATUS_APPROVED;
      }

      if (KBArticlePermission.contains(
          themeDisplay.getPermissionChecker(), resourcePrimKey, ActionKeys.UPDATE)) {

        return ParamUtil.getInteger(renderRequest, "status", WorkflowConstants.STATUS_ANY);
      }
    }

    return WorkflowConstants.STATUS_APPROVED;
  }
  public void updateRootKBFolderId(ActionRequest actionRequest, ActionResponse actionResponse)
      throws PortalException {

    long kbFolderId = ParamUtil.getLong(actionRequest, "rootKBFolderId");

    if (kbFolderId == KBFolderConstants.DEFAULT_PARENT_FOLDER_ID) {
      return;
    }

    KBFolder kbFolder = KBFolderServiceUtil.getKBFolder(kbFolderId);

    PortalPreferences portalPreferences =
        PortletPreferencesFactoryUtil.getPortalPreferences(
            PortalUtil.getLiferayPortletRequest(actionRequest));

    portalPreferences.setValue(
        PortletKeys.KNOWLEDGE_BASE_DISPLAY, "preferredKBFolderURLTitle", kbFolder.getUrlTitle());

    String urlTitle = ParamUtil.getString(actionRequest, "urlTitle");

    if (Validator.isNull(urlTitle)) {
      return;
    }

    KBArticle kbArticle =
        KBArticleLocalServiceUtil.fetchKBArticleByUrlTitle(
            kbFolder.getGroupId(), kbFolder.getUrlTitle(), urlTitle);

    if (kbArticle == null) {
      return;
    }

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    if (!KBArticlePermission.contains(
        themeDisplay.getPermissionChecker(), kbArticle, ActionKeys.VIEW)) {

      return;
    }

    actionResponse.setRenderParameter("kbFolderUrlTitle", kbFolder.getUrlTitle());
    actionResponse.setRenderParameter("urlTitle", urlTitle);
  }
  @Override
  public boolean hasPermission(PermissionChecker permissionChecker, long classPK, String actionId)
      throws Exception {

    return KBArticlePermission.contains(permissionChecker, classPK, actionId);
  }
  protected Tuple getResourceTuple(RenderRequest renderRequest) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) renderRequest.getAttribute(WebKeys.THEME_DISPLAY);

    String urlTitle = ParamUtil.getString(renderRequest, "urlTitle");

    if (Validator.isNotNull(urlTitle)) {
      String kbFolderUrlTitle = ParamUtil.getString(renderRequest, "kbFolderUrlTitle");

      KBArticle kbArticle = null;

      if (Validator.isNotNull(kbFolderUrlTitle)) {
        kbArticle =
            KBArticleLocalServiceUtil.fetchKBArticleByUrlTitle(
                themeDisplay.getScopeGroupId(), kbFolderUrlTitle, urlTitle);
      } else {
        kbArticle =
            KBArticleLocalServiceUtil.fetchKBArticleByUrlTitle(
                themeDisplay.getScopeGroupId(),
                KBFolderConstants.DEFAULT_PARENT_FOLDER_ID,
                urlTitle);
      }

      if ((kbArticle != null)
          && KBArticlePermission.contains(
              themeDisplay.getPermissionChecker(), kbArticle, ActionKeys.VIEW)) {

        return new Tuple(kbArticle.getClassNameId(), kbArticle.getResourcePrimKey());
      }
    }

    PortletPreferences portletPreferences = renderRequest.getPreferences();

    long defaultResourcePrimKey =
        GetterUtil.getLong(portletPreferences.getValue("resourcePrimKey", null));

    String mvcPath = ParamUtil.getString(renderRequest, "mvcPath");

    if (((defaultResourcePrimKey == 0) && mvcPath.equals(viewTemplate))
        || mvcPath.equals("/display/select_configuration_article.jsp")) {

      return new Tuple(
          PortalUtil.getClassNameId(KBFolderConstants.getClassName()),
          KBFolderConstants.DEFAULT_PARENT_FOLDER_ID);
    }

    long defaultResourceClassNameId =
        GetterUtil.getLong(portletPreferences.getValue("resourceClassNameId", null));

    long resourceClassNameId =
        ParamUtil.getLong(renderRequest, "resourceClassNameId", defaultResourceClassNameId);

    long resourcePrimKey =
        ParamUtil.getLong(renderRequest, "resourcePrimKey", defaultResourcePrimKey);

    if ((resourcePrimKey == 0) || (resourcePrimKey != defaultResourcePrimKey)) {

      return new Tuple(resourceClassNameId, resourcePrimKey);
    }

    long kbFolderClassNameId = PortalUtil.getClassNameId(KBFolderConstants.getClassName());

    if (resourceClassNameId == kbFolderClassNameId) {
      KBFolder kbFolder = KBFolderLocalServiceUtil.fetchKBFolder(resourcePrimKey);

      if ((kbFolder != null)
          && !KBFolderPermission.contains(
              themeDisplay.getPermissionChecker(),
              themeDisplay.getScopeGroupId(),
              defaultResourcePrimKey,
              ActionKeys.VIEW)) {

        return new Tuple(
            PortalUtil.getClassNameId(KBFolderConstants.getClassName()),
            KBFolderConstants.DEFAULT_PARENT_FOLDER_ID);
      }
    } else {
      KBArticle kbArticle =
          KBArticleLocalServiceUtil.fetchLatestKBArticle(
              defaultResourcePrimKey, WorkflowConstants.STATUS_ANY);

      if ((kbArticle != null)
          && !KBArticlePermission.contains(
              themeDisplay.getPermissionChecker(), defaultResourcePrimKey, ActionKeys.VIEW)) {

        return new Tuple(
            PortalUtil.getClassNameId(KBFolderConstants.getClassName()),
            KBFolderConstants.DEFAULT_PARENT_FOLDER_ID);
      }
    }

    return new Tuple(defaultResourceClassNameId, defaultResourcePrimKey);
  }