@Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, BlogsEntry entry)
      throws Exception {

    Element entryElement = portletDataContext.getExportDataElement(entry);

    if (entry.isSmallImage()) {
      Image smallImage = _imageLocalService.fetchImage(entry.getSmallImageId());

      if (smallImage != null) {
        String smallImagePath =
            ExportImportPathUtil.getModelPath(
                entry, smallImage.getImageId() + StringPool.PERIOD + smallImage.getType());

        entryElement.addAttribute("small-image-path", smallImagePath);

        entry.setSmallImageType(smallImage.getType());

        portletDataContext.addZipEntry(smallImagePath, smallImage.getTextObj());
      }
    }

    if (entry.getSmallImageFileEntryId() != 0) {
      FileEntry fileEntry =
          PortletFileRepositoryUtil.getPortletFileEntry(entry.getSmallImageFileEntryId());

      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, entry, fileEntry, PortletDataContext.REFERENCE_TYPE_WEAK);
    }

    if (entry.getCoverImageFileEntryId() != 0) {
      FileEntry fileEntry =
          PortletFileRepositoryUtil.getPortletFileEntry(entry.getCoverImageFileEntryId());

      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, entry, fileEntry, PortletDataContext.REFERENCE_TYPE_WEAK);
    }

    String content =
        _blogsEntryExportImportContentProcessor.replaceExportContentReferences(
            portletDataContext,
            entry,
            entry.getContent(),
            portletDataContext.getBooleanParameter("blogs", "referenced-content"),
            true);

    entry.setContent(content);

    portletDataContext.addClassedModel(
        entryElement, ExportImportPathUtil.getModelPath(entry), entry);
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, BlogsEntry entry)
      throws Exception {

    long userId = portletDataContext.getUserId(entry.getUserUuid());

    Element entryElement = portletDataContext.getImportDataStagedModelElement(entry);

    String content =
        _blogsEntryExportImportContentProcessor.replaceImportContentReferences(
            portletDataContext, entry, entry.getContent());

    entry.setContent(content);

    Calendar displayDateCal = CalendarFactoryUtil.getCalendar();

    displayDateCal.setTime(entry.getDisplayDate());

    int displayDateMonth = displayDateCal.get(Calendar.MONTH);
    int displayDateDay = displayDateCal.get(Calendar.DATE);
    int displayDateYear = displayDateCal.get(Calendar.YEAR);
    int displayDateHour = displayDateCal.get(Calendar.HOUR);
    int displayDateMinute = displayDateCal.get(Calendar.MINUTE);

    if (displayDateCal.get(Calendar.AM_PM) == Calendar.PM) {
      displayDateHour += 12;
    }

    boolean allowPingbacks = entry.isAllowPingbacks();
    boolean allowTrackbacks = entry.isAllowTrackbacks();
    String[] trackbacks = StringUtil.split(entry.getTrackbacks());

    ServiceContext serviceContext = portletDataContext.createServiceContext(entry);

    BlogsEntry importedEntry = null;

    if (portletDataContext.isDataStrategyMirror()) {
      serviceContext.setAttribute("urlTitle", entry.getUrlTitle());

      BlogsEntry existingEntry =
          fetchStagedModelByUuidAndGroupId(entry.getUuid(), portletDataContext.getScopeGroupId());

      if (existingEntry == null) {
        serviceContext.setUuid(entry.getUuid());

        importedEntry =
            _blogsEntryLocalService.addEntry(
                userId,
                entry.getTitle(),
                entry.getSubtitle(),
                entry.getDescription(),
                entry.getContent(),
                displayDateMonth,
                displayDateDay,
                displayDateYear,
                displayDateHour,
                displayDateMinute,
                allowPingbacks,
                allowTrackbacks,
                trackbacks,
                entry.getCoverImageCaption(),
                null,
                null,
                serviceContext);
      } else {
        importedEntry =
            _blogsEntryLocalService.updateEntry(
                userId,
                existingEntry.getEntryId(),
                entry.getTitle(),
                entry.getSubtitle(),
                entry.getDescription(),
                entry.getContent(),
                displayDateMonth,
                displayDateDay,
                displayDateYear,
                displayDateHour,
                displayDateMinute,
                allowPingbacks,
                allowTrackbacks,
                trackbacks,
                entry.getCoverImageCaption(),
                new ImageSelector(),
                new ImageSelector(),
                serviceContext);
      }
    } else {
      importedEntry =
          _blogsEntryLocalService.addEntry(
              userId,
              entry.getTitle(),
              entry.getSubtitle(),
              entry.getDescription(),
              entry.getContent(),
              displayDateMonth,
              displayDateDay,
              displayDateYear,
              displayDateHour,
              displayDateMinute,
              allowPingbacks,
              allowTrackbacks,
              trackbacks,
              entry.getCoverImageCaption(),
              null,
              null,
              serviceContext);
    }

    if ((entry.getCoverImageFileEntryId() == 0)
        && Validator.isNull(entry.getCoverImageURL())
        && (entry.getSmallImageFileEntryId() == 0)
        && Validator.isNull(entry.getSmallImageURL())
        && !entry.isSmallImage()) {

      portletDataContext.importClassedModel(entry, importedEntry);

      return;
    }

    // Cover image

    ImageSelector coverImageSelector = null;

    List<Element> attachmentElements =
        portletDataContext.getReferenceDataElements(
            entry, DLFileEntry.class, PortletDataContext.REFERENCE_TYPE_WEAK);

    if (Validator.isNotNull(entry.getCoverImageURL())) {
      coverImageSelector = new ImageSelector(entry.getCoverImageURL());
    } else if (entry.getCoverImageFileEntryId() != 0) {
      coverImageSelector =
          _getImageSelector(
              portletDataContext, entry.getCoverImageFileEntryId(), attachmentElements);
    }

    if (coverImageSelector != null) {
      _blogsEntryLocalService.addCoverImage(importedEntry.getEntryId(), coverImageSelector);
    }

    // Small image

    ImageSelector smallImageSelector = null;

    if (entry.isSmallImage()) {
      String smallImagePath = entryElement.attributeValue("small-image-path");

      if (Validator.isNotNull(entry.getSmallImageURL())) {
        smallImageSelector = new ImageSelector(entry.getSmallImageURL());
      } else if (Validator.isNotNull(smallImagePath)) {
        String smallImageFileName =
            entry.getSmallImageId() + StringPool.PERIOD + entry.getSmallImageType();

        InputStream inputStream = null;

        try {
          inputStream = portletDataContext.getZipEntryAsInputStream(smallImagePath);

          smallImageSelector =
              new ImageSelector(
                  FileUtil.getBytes(inputStream),
                  smallImageFileName,
                  MimeTypesUtil.getContentType(smallImageFileName),
                  null);
        } finally {
          StreamUtil.cleanUp(inputStream);
        }
      } else if (entry.getSmallImageFileEntryId() != 0) {
        smallImageSelector =
            _getImageSelector(
                portletDataContext, entry.getSmallImageFileEntryId(), attachmentElements);
      }
    }

    if (smallImageSelector != null) {
      _blogsEntryLocalService.addSmallImage(importedEntry.getEntryId(), smallImageSelector);
    }

    if ((coverImageSelector != null) || (smallImageSelector != null)) {
      importedEntry = _blogsEntryLocalService.getBlogsEntry(importedEntry.getEntryId());
    }

    portletDataContext.importClassedModel(entry, importedEntry);
  }
  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};
  }