public JournalArticle getArticle() throws PortalException {
    if (_article != null) {
      return _article;
    }

    _article = ActionUtil.getArticle(_request);

    return _article;
  }
  public void expireArticles(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

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

    if (Validator.isNotNull(articleId)) {
      ActionUtil.expireArticle(actionRequest, articleId);
    } else {
      String[] expireArticleIds =
          StringUtil.split(ParamUtil.getString(actionRequest, "expireArticleIds"));

      for (String expireArticleId : expireArticleIds) {
        ActionUtil.expireArticle(actionRequest, HtmlUtil.unescape(expireArticleId));
      }
    }

    sendEditArticleRedirect(actionRequest, actionResponse);
  }
  public void expireEntries(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

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

    long[] expireFolderIds = StringUtil.split(ParamUtil.getString(actionRequest, "folderIds"), 0L);

    ServiceContext serviceContext =
        ServiceContextFactory.getInstance(JournalArticle.class.getName(), actionRequest);

    for (long expireFolderId : expireFolderIds) {
      ActionUtil.expireFolder(themeDisplay.getScopeGroupId(), expireFolderId, serviceContext);
    }

    String[] expireArticleIds = StringUtil.split(ParamUtil.getString(actionRequest, "articleIds"));

    for (String expireArticleId : expireArticleIds) {
      ActionUtil.expireArticle(actionRequest, HtmlUtil.unescape(expireArticleId));
    }

    sendEditEntryRedirect(actionRequest, actionResponse);
  }
  public JournalFolder getFolder() throws PortalException {
    if (_folder != null) {
      return _folder;
    }

    _folder = (JournalFolder) _request.getAttribute(WebKeys.JOURNAL_FOLDER);

    if (_folder != null) {
      return _folder;
    }

    _folder = ActionUtil.getFolder(_request);

    return _folder;
  }
  protected void deleteEntries(
      ActionRequest actionRequest, ActionResponse actionResponse, boolean moveToTrash)
      throws Exception {

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

    List<TrashedModel> trashedModels = new ArrayList<>();

    long[] deleteFolderIds = StringUtil.split(ParamUtil.getString(actionRequest, "folderIds"), 0L);

    for (long deleteFolderId : deleteFolderIds) {
      if (moveToTrash) {
        JournalFolder folder = _journalFolderService.moveFolderToTrash(deleteFolderId);

        trashedModels.add(folder);
      } else {
        _journalFolderService.deleteFolder(deleteFolderId);
      }
    }

    String[] deleteArticleIds = StringUtil.split(ParamUtil.getString(actionRequest, "articleIds"));

    for (String deleteArticleId : deleteArticleIds) {
      if (moveToTrash) {
        JournalArticle article =
            _journalArticleService.moveArticleToTrash(
                themeDisplay.getScopeGroupId(), HtmlUtil.unescape(deleteArticleId));

        trashedModels.add(article);
      } else {
        ActionUtil.deleteArticle(actionRequest, HtmlUtil.unescape(deleteArticleId));
      }
    }

    if (moveToTrash && !trashedModels.isEmpty()) {
      TrashUtil.addTrashSessionMessages(actionRequest, trashedModels);

      hideDefaultSuccessMessage(actionRequest);
    }

    sendEditEntryRedirect(actionRequest, actionResponse);
  }
  protected void deleteArticles(
      ActionRequest actionRequest, ActionResponse actionResponse, boolean moveToTrash)
      throws Exception {

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

    String[] deleteArticleIds = null;

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

    if (Validator.isNotNull(articleId)) {
      deleteArticleIds = new String[] {articleId};
    } else {
      deleteArticleIds = StringUtil.split(ParamUtil.getString(actionRequest, "articleIds"));
    }

    List<TrashedModel> trashedModels = new ArrayList<>();

    for (String deleteArticleId : deleteArticleIds) {
      if (moveToTrash) {
        JournalArticle article =
            _journalArticleService.moveArticleToTrash(
                themeDisplay.getScopeGroupId(), HtmlUtil.unescape(deleteArticleId));

        trashedModels.add(article);
      } else {
        ActionUtil.deleteArticle(actionRequest, HtmlUtil.unescape(deleteArticleId));
      }
    }

    if (moveToTrash && !trashedModels.isEmpty()) {
      TrashUtil.addTrashSessionMessages(actionRequest, trashedModels);

      hideDefaultSuccessMessage(actionRequest);
    }

    sendEditArticleRedirect(actionRequest, actionResponse);
  }
  public void updateArticle(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    UploadException uploadException =
        (UploadException) actionRequest.getAttribute(WebKeys.UPLOAD_EXCEPTION);

    if (uploadException != null) {
      if (uploadException.isExceededLiferayFileItemSizeLimit()) {
        throw new LiferayFileItemException();
      } else if (uploadException.isExceededSizeLimit()) {
        throw new ArticleContentSizeException();
      }

      throw new PortalException(uploadException.getCause());
    }

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    if (_log.isDebugEnabled()) {
      _log.debug("Updating article " + MapUtil.toString(uploadPortletRequest.getParameterMap()));
    }

    String actionName = ParamUtil.getString(actionRequest, ActionRequest.ACTION_NAME);

    long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
    long folderId = ParamUtil.getLong(uploadPortletRequest, "folderId");
    long classNameId = ParamUtil.getLong(uploadPortletRequest, "classNameId");
    long classPK = ParamUtil.getLong(uploadPortletRequest, "classPK");

    String articleId = ParamUtil.getString(uploadPortletRequest, "articleId");

    boolean autoArticleId = ParamUtil.getBoolean(uploadPortletRequest, "autoArticleId");
    double version = ParamUtil.getDouble(uploadPortletRequest, "version");

    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(actionRequest, "title");
    Map<Locale, String> descriptionMap =
        LocalizationUtil.getLocalizationMap(actionRequest, "description");

    ServiceContext serviceContext =
        ServiceContextFactory.getInstance(JournalArticle.class.getName(), uploadPortletRequest);

    String ddmStructureKey = ParamUtil.getString(uploadPortletRequest, "ddmStructureKey");

    DDMStructure ddmStructure =
        _ddmStructureLocalService.getStructure(
            PortalUtil.getSiteGroupId(groupId),
            PortalUtil.getClassNameId(JournalArticle.class),
            ddmStructureKey,
            true);

    Object[] contentAndImages = ActionUtil.getContentAndImages(ddmStructure, serviceContext);

    String content = (String) contentAndImages[0];
    Map<String, byte[]> images = (HashMap<String, byte[]>) contentAndImages[1];

    Boolean fileItemThresholdSizeExceeded =
        (Boolean) uploadPortletRequest.getAttribute(WebKeys.FILE_ITEM_THRESHOLD_SIZE_EXCEEDED);

    if ((fileItemThresholdSizeExceeded != null) && fileItemThresholdSizeExceeded.booleanValue()) {

      throw new ArticleContentSizeException();
    }

    String ddmTemplateKey = ParamUtil.getString(uploadPortletRequest, "ddmTemplateKey");
    String layoutUuid = ParamUtil.getString(uploadPortletRequest, "layoutUuid");

    Layout targetLayout = JournalUtil.getArticleLayout(layoutUuid, groupId);

    if (targetLayout == null) {
      layoutUuid = null;
    }

    int displayDateMonth = ParamUtil.getInteger(uploadPortletRequest, "displayDateMonth");
    int displayDateDay = ParamUtil.getInteger(uploadPortletRequest, "displayDateDay");
    int displayDateYear = ParamUtil.getInteger(uploadPortletRequest, "displayDateYear");
    int displayDateHour = ParamUtil.getInteger(uploadPortletRequest, "displayDateHour");
    int displayDateMinute = ParamUtil.getInteger(uploadPortletRequest, "displayDateMinute");
    int displayDateAmPm = ParamUtil.getInteger(uploadPortletRequest, "displayDateAmPm");

    if (displayDateAmPm == Calendar.PM) {
      displayDateHour += 12;
    }

    int expirationDateMonth = ParamUtil.getInteger(uploadPortletRequest, "expirationDateMonth");
    int expirationDateDay = ParamUtil.getInteger(uploadPortletRequest, "expirationDateDay");
    int expirationDateYear = ParamUtil.getInteger(uploadPortletRequest, "expirationDateYear");
    int expirationDateHour = ParamUtil.getInteger(uploadPortletRequest, "expirationDateHour");
    int expirationDateMinute = ParamUtil.getInteger(uploadPortletRequest, "expirationDateMinute");
    int expirationDateAmPm = ParamUtil.getInteger(uploadPortletRequest, "expirationDateAmPm");
    boolean neverExpire = ParamUtil.getBoolean(uploadPortletRequest, "neverExpire");

    if (expirationDateAmPm == Calendar.PM) {
      expirationDateHour += 12;
    }

    int reviewDateMonth = ParamUtil.getInteger(uploadPortletRequest, "reviewDateMonth");
    int reviewDateDay = ParamUtil.getInteger(uploadPortletRequest, "reviewDateDay");
    int reviewDateYear = ParamUtil.getInteger(uploadPortletRequest, "reviewDateYear");
    int reviewDateHour = ParamUtil.getInteger(uploadPortletRequest, "reviewDateHour");
    int reviewDateMinute = ParamUtil.getInteger(uploadPortletRequest, "reviewDateMinute");
    int reviewDateAmPm = ParamUtil.getInteger(uploadPortletRequest, "reviewDateAmPm");
    boolean neverReview = ParamUtil.getBoolean(uploadPortletRequest, "neverReview");

    if (reviewDateAmPm == Calendar.PM) {
      reviewDateHour += 12;
    }

    boolean indexable = ParamUtil.getBoolean(uploadPortletRequest, "indexable");

    boolean smallImage = ParamUtil.getBoolean(uploadPortletRequest, "smallImage");
    String smallImageURL = ParamUtil.getString(uploadPortletRequest, "smallImageURL");
    File smallFile = uploadPortletRequest.getFile("smallFile");

    String articleURL = ParamUtil.getString(uploadPortletRequest, "articleURL");

    JournalArticle article = null;
    String oldUrlTitle = StringPool.BLANK;

    if (actionName.equals("addArticle")) {

      // Add article

      article =
          _journalArticleService.addArticle(
              groupId,
              folderId,
              classNameId,
              classPK,
              articleId,
              autoArticleId,
              titleMap,
              descriptionMap,
              content,
              ddmStructureKey,
              ddmTemplateKey,
              layoutUuid,
              displayDateMonth,
              displayDateDay,
              displayDateYear,
              displayDateHour,
              displayDateMinute,
              expirationDateMonth,
              expirationDateDay,
              expirationDateYear,
              expirationDateHour,
              expirationDateMinute,
              neverExpire,
              reviewDateMonth,
              reviewDateDay,
              reviewDateYear,
              reviewDateHour,
              reviewDateMinute,
              neverReview,
              indexable,
              smallImage,
              smallImageURL,
              smallFile,
              images,
              articleURL,
              serviceContext);
    } else {

      // Update article

      article = _journalArticleService.getArticle(groupId, articleId, version);

      String tempOldUrlTitle = article.getUrlTitle();

      if (actionName.equals("previewArticle") || actionName.equals("updateArticle")) {

        article =
            _journalArticleService.updateArticle(
                groupId,
                folderId,
                articleId,
                version,
                titleMap,
                descriptionMap,
                content,
                ddmStructureKey,
                ddmTemplateKey,
                layoutUuid,
                displayDateMonth,
                displayDateDay,
                displayDateYear,
                displayDateHour,
                displayDateMinute,
                expirationDateMonth,
                expirationDateDay,
                expirationDateYear,
                expirationDateHour,
                expirationDateMinute,
                neverExpire,
                reviewDateMonth,
                reviewDateDay,
                reviewDateYear,
                reviewDateHour,
                reviewDateMinute,
                neverReview,
                indexable,
                smallImage,
                smallImageURL,
                smallFile,
                images,
                articleURL,
                serviceContext);
      }

      if (!tempOldUrlTitle.equals(article.getUrlTitle())) {
        oldUrlTitle = tempOldUrlTitle;
      }
    }

    // Recent articles

    JournalUtil.addRecentArticle(actionRequest, article);

    // Journal content

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

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

    PortletPreferences portletPreferences =
        PortletPreferencesFactoryUtil.getStrictPortletSetup(
            themeDisplay.getLayout(), portletResource);

    if (portletPreferences != null) {
      portletPreferences.setValue("groupId", String.valueOf(article.getGroupId()));
      portletPreferences.setValue("articleId", article.getArticleId());

      portletPreferences.store();

      updateContentSearch(actionRequest, portletResource, article.getArticleId());
    }

    sendEditArticleRedirect(actionRequest, actionResponse, article, oldUrlTitle);
  }
  protected void sendEditArticleRedirect(
      ActionRequest actionRequest,
      ActionResponse actionResponse,
      JournalArticle article,
      String oldUrlTitle)
      throws Exception {

    String actionName = ParamUtil.getString(actionRequest, ActionRequest.ACTION_NAME);

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

    int workflowAction =
        ParamUtil.getInteger(actionRequest, "workflowAction", WorkflowConstants.ACTION_PUBLISH);

    String portletId = HttpUtil.getParameter(redirect, "p_p_id", false);

    String namespace = PortalUtil.getPortletNamespace(portletId);

    if (Validator.isNotNull(oldUrlTitle)) {
      String oldRedirectParam = namespace + "redirect";

      String oldRedirect = HttpUtil.getParameter(redirect, oldRedirectParam, false);

      if (Validator.isNotNull(oldRedirect)) {
        String newRedirect = HttpUtil.decodeURL(oldRedirect);

        newRedirect = StringUtil.replace(newRedirect, oldUrlTitle, article.getUrlTitle());
        newRedirect = StringUtil.replace(newRedirect, oldRedirectParam, "redirect");

        redirect = StringUtil.replace(redirect, oldRedirect, newRedirect);
      }
    }

    if ((actionName.equals("deleteArticle") || actionName.equals("deleteArticles"))
        && !ActionUtil.hasArticle(actionRequest)) {

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

      PortletURL portletURL =
          PortletURLFactoryUtil.create(
              actionRequest,
              themeDisplay.getPpid(),
              themeDisplay.getPlid(),
              PortletRequest.RENDER_PHASE);

      redirect = portletURL.toString();
    }

    if ((article != null) && (workflowAction == WorkflowConstants.ACTION_SAVE_DRAFT)) {

      redirect = getSaveAndContinueRedirect(actionRequest, article, redirect);

      if (actionName.equals("previewArticle")) {
        SessionMessages.add(actionRequest, "previewRequested");

        hideDefaultSuccessMessage(actionRequest);
      }
    } else {
      WindowState windowState = actionRequest.getWindowState();

      if (windowState.equals(LiferayWindowState.POP_UP)) {
        redirect = PortalUtil.escapeRedirect(redirect);

        if (Validator.isNotNull(redirect)) {
          if (actionName.equals("addArticle") && (article != null)) {
            redirect =
                HttpUtil.addParameter(
                    redirect, namespace + "className", JournalArticle.class.getName());
            redirect =
                HttpUtil.addParameter(
                    redirect,
                    namespace + "classPK",
                    JournalArticleAssetRenderer.getClassPK(article));
          }

          actionResponse.sendRedirect(redirect);
        }
      }
    }

    actionRequest.setAttribute(WebKeys.REDIRECT, redirect);
  }