@Override
  protected FileEntry addFileEntry(
      long userId,
      long groupId,
      long folderId,
      String fileName,
      String contentType,
      InputStream inputStream,
      long size,
      ServiceContext serviceContext)
      throws PortalException {

    Folder folder = BlogsEntryLocalServiceUtil.addAttachmentsFolder(userId, groupId);

    return PortletFileRepositoryUtil.addPortletFileEntry(
        groupId,
        userId,
        BlogsEntry.class.getName(),
        0,
        BlogsConstants.SERVICE_NAME,
        folder.getFolderId(),
        inputStream,
        fileName,
        contentType,
        true);
  }
  @Override
  protected FileEntry fetchFileEntry(long userId, long groupId, long folderId, String fileName)
      throws PortalException {

    Folder folder = BlogsEntryLocalServiceUtil.addAttachmentsFolder(userId, groupId);

    try {
      return PortletFileRepositoryUtil.getPortletFileEntry(groupId, folder.getFolderId(), fileName);
    } catch (PortalException pe) {
      if (_log.isDebugEnabled()) {
        _log.debug(pe, pe);
      }

      return null;
    }
  }
  protected Object[] updateEntry(ActionRequest actionRequest) throws Exception {

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

    long entryId = ParamUtil.getLong(actionRequest, "entryId");

    String title = ParamUtil.getString(actionRequest, "title");
    String subtitle = ParamUtil.getString(actionRequest, "subtitle");

    String description = StringPool.BLANK;

    boolean customAbstract = ParamUtil.getBoolean(actionRequest, "customAbstract");

    if (customAbstract) {
      description = ParamUtil.getString(actionRequest, "description");

      if (Validator.isNull(description)) {
        throw new EntryDescriptionException();
      }
    }

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

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

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

    boolean allowPingbacks = ParamUtil.getBoolean(actionRequest, "allowPingbacks");
    boolean allowTrackbacks = ParamUtil.getBoolean(actionRequest, "allowTrackbacks");
    String[] trackbacks = StringUtil.split(ParamUtil.getString(actionRequest, "trackbacks"));

    long coverImageFileEntryId = ParamUtil.getLong(actionRequest, "coverImageFileEntryId");
    String coverImageURL = ParamUtil.getString(actionRequest, "coverImageURL");
    String coverImageFileEntryCropRegion =
        ParamUtil.getString(actionRequest, "coverImageFileEntryCropRegion");

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

    ImageSelector coverImageImageSelector =
        new ImageSelector(coverImageFileEntryId, coverImageURL, coverImageFileEntryCropRegion);

    long smallImageFileEntryId = ParamUtil.getLong(actionRequest, "smallImageFileEntryId");
    String smallImageURL = ParamUtil.getString(actionRequest, "smallImageURL");

    ImageSelector smallImageImageSelector =
        new ImageSelector(smallImageFileEntryId, smallImageURL, null);

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

    BlogsEntry entry = null;
    String oldUrlTitle = StringPool.BLANK;
    List<BlogsEntryAttachmentFileEntryReference> blogsEntryAttachmentFileEntryReferences =
        new ArrayList<>();

    if (entryId <= 0) {

      // Add entry

      entry =
          BlogsEntryServiceUtil.addEntry(
              title,
              subtitle,
              description,
              content,
              displayDateMonth,
              displayDateDay,
              displayDateYear,
              displayDateHour,
              displayDateMinute,
              allowPingbacks,
              allowTrackbacks,
              trackbacks,
              coverImageCaption,
              coverImageImageSelector,
              smallImageImageSelector,
              serviceContext);

      BlogsEntryAttachmentFileEntryHelper blogsEntryAttachmentFileEntryHelper =
          new BlogsEntryAttachmentFileEntryHelper();

      List<FileEntry> tempBlogsEntryAttachments =
          blogsEntryAttachmentFileEntryHelper.getTempBlogsEntryAttachmentFileEntries(content);

      if (!tempBlogsEntryAttachments.isEmpty()) {
        Folder folder =
            BlogsEntryLocalServiceUtil.addAttachmentsFolder(
                themeDisplay.getUserId(), entry.getGroupId());

        blogsEntryAttachmentFileEntryReferences =
            blogsEntryAttachmentFileEntryHelper.addBlogsEntryAttachmentFileEntries(
                entry.getGroupId(),
                themeDisplay.getUserId(),
                entry.getEntryId(),
                folder.getFolderId(),
                tempBlogsEntryAttachments);

        content =
            blogsEntryAttachmentFileEntryHelper.updateContent(
                content, blogsEntryAttachmentFileEntryReferences);

        entry.setContent(content);

        BlogsEntryLocalServiceUtil.updateBlogsEntry(entry);
      }

      for (FileEntry tempBlogsEntryAttachment : tempBlogsEntryAttachments) {

        PortletFileRepositoryUtil.deletePortletFileEntry(tempBlogsEntryAttachment.getFileEntryId());
      }
    } else {

      // Update entry

      boolean sendEmailEntryUpdated = ParamUtil.getBoolean(actionRequest, "sendEmailEntryUpdated");

      serviceContext.setAttribute("sendEmailEntryUpdated", sendEmailEntryUpdated);

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

      serviceContext.setAttribute("emailEntryUpdatedComment", emailEntryUpdatedComment);

      entry = BlogsEntryLocalServiceUtil.getEntry(entryId);

      String tempOldUrlTitle = entry.getUrlTitle();

      BlogsEntryAttachmentFileEntryHelper blogsEntryAttachmentHelper =
          new BlogsEntryAttachmentFileEntryHelper();

      List<FileEntry> tempBlogsEntryAttachmentFileEntries =
          blogsEntryAttachmentHelper.getTempBlogsEntryAttachmentFileEntries(content);

      if (!tempBlogsEntryAttachmentFileEntries.isEmpty()) {
        Folder folder =
            BlogsEntryLocalServiceUtil.addAttachmentsFolder(
                themeDisplay.getUserId(), entry.getGroupId());

        blogsEntryAttachmentFileEntryReferences =
            blogsEntryAttachmentHelper.addBlogsEntryAttachmentFileEntries(
                entry.getGroupId(),
                themeDisplay.getUserId(),
                entry.getEntryId(),
                folder.getFolderId(),
                tempBlogsEntryAttachmentFileEntries);

        content =
            blogsEntryAttachmentHelper.updateContent(
                content, blogsEntryAttachmentFileEntryReferences);
      }

      entry =
          BlogsEntryServiceUtil.updateEntry(
              entryId,
              title,
              subtitle,
              description,
              content,
              displayDateMonth,
              displayDateDay,
              displayDateYear,
              displayDateHour,
              displayDateMinute,
              allowPingbacks,
              allowTrackbacks,
              trackbacks,
              coverImageCaption,
              coverImageImageSelector,
              smallImageImageSelector,
              serviceContext);

      for (FileEntry tempBlogsEntryAttachmentFileEntry : tempBlogsEntryAttachmentFileEntries) {

        PortletFileRepositoryUtil.deletePortletFileEntry(
            tempBlogsEntryAttachmentFileEntry.getFileEntryId());
      }

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

    return new Object[] {entry, oldUrlTitle, blogsEntryAttachmentFileEntryReferences};
  }