protected FileEntry updateFileEntry(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

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

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    long fileEntryId = ParamUtil.getLong(uploadPortletRequest, "fileEntryId");

    String sourceFileName = uploadPortletRequest.getFileName("imageEditorFileName");

    FileEntry fileEntry = _dlAppLocalService.getFileEntry(fileEntryId);

    InputStream inputStream = uploadPortletRequest.getFileAsStream("imageEditorFileName");
    String contentType = uploadPortletRequest.getContentType("imageEditorFileName");
    long size = uploadPortletRequest.getSize("imageEditorFileName");

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

    fileEntry =
        _dlAppService.updateFileEntry(
            fileEntryId,
            sourceFileName,
            contentType,
            fileEntry.getTitle(),
            fileEntry.getDescription(),
            StringPool.BLANK,
            false,
            inputStream,
            size,
            serviceContext);

    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

    jsonObject.put("success", Boolean.TRUE);

    ResourceBundle resourceBundle =
        _resourceBundleLoader.loadResourceBundle(
            LanguageUtil.getLanguageId(themeDisplay.getLocale()));

    SessionMessages.add(
        actionRequest,
        "requestProcessed",
        LanguageUtil.get(resourceBundle, "the-image-was-edited-successfully"));

    JSONPortletResponseUtil.writeJSON(actionRequest, actionResponse, jsonObject);

    return fileEntry;
  }
  protected DDMTemplate addTemplate(ActionRequest actionRequest) throws Exception {

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

    long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
    long classNameId = ParamUtil.getLong(uploadPortletRequest, "classNameId");
    long classPK = ParamUtil.getLong(uploadPortletRequest, "classPK");
    long resourceClassNameId = ParamUtil.getLong(uploadPortletRequest, "resourceClassNameId");
    String templateKey = ParamUtil.getString(uploadPortletRequest, "templateKey");
    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(uploadPortletRequest, "name");
    Map<Locale, String> descriptionMap =
        LocalizationUtil.getLocalizationMap(uploadPortletRequest, "description");
    String type = ParamUtil.getString(uploadPortletRequest, "type");
    String mode = ParamUtil.getString(uploadPortletRequest, "mode");
    String language =
        ParamUtil.getString(uploadPortletRequest, "language", TemplateConstants.LANG_TYPE_VM);

    String script = getScript(uploadPortletRequest);

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

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

    return _ddmTemplateService.addTemplate(
        groupId,
        classNameId,
        classPK,
        resourceClassNameId,
        templateKey,
        nameMap,
        descriptionMap,
        type,
        mode,
        language,
        script,
        cacheable,
        smallImage,
        smallImageURL,
        smallImageFile,
        serviceContext);
  }
  protected MBMessage updateMessage(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

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

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

    long groupId = themeDisplay.getScopeGroupId();
    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
    long threadId = ParamUtil.getLong(actionRequest, "threadId");
    long parentMessageId = ParamUtil.getLong(actionRequest, "parentMessageId");
    String subject = ParamUtil.getString(actionRequest, "subject");
    String body = ParamUtil.getString(actionRequest, "body");

    MBGroupServiceSettings mbGroupServiceSettings = MBGroupServiceSettings.getInstance(groupId);

    List<ObjectValuePair<String, InputStream>> inputStreamOVPs = new ArrayList<>(5);

    try {
      UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

      for (int i = 1; i <= 5; i++) {
        String fileName = uploadPortletRequest.getFileName("msgFile" + i);
        InputStream inputStream = uploadPortletRequest.getFileAsStream("msgFile" + i);

        if ((inputStream == null) || Validator.isNull(fileName)) {
          continue;
        }

        ObjectValuePair<String, InputStream> inputStreamOVP =
            new ObjectValuePair<>(fileName, inputStream);

        inputStreamOVPs.add(inputStreamOVP);
      }

      boolean question = ParamUtil.getBoolean(actionRequest, "question");
      boolean anonymous = ParamUtil.getBoolean(actionRequest, "anonymous");
      double priority = ParamUtil.getDouble(actionRequest, "priority");
      boolean allowPingbacks = ParamUtil.getBoolean(actionRequest, "allowPingbacks");

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

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

      serviceContext.setAttribute("preview", preview);

      MBMessage message = null;

      if (messageId <= 0) {
        if (PropsValues.CAPTCHA_CHECK_PORTLET_MESSAGE_BOARDS_EDIT_MESSAGE) {

          CaptchaUtil.check(actionRequest);
        }

        if (threadId <= 0) {

          // Post new thread

          message =
              _mbMessageService.addMessage(
                  groupId,
                  categoryId,
                  subject,
                  body,
                  mbGroupServiceSettings.getMessageFormat(),
                  inputStreamOVPs,
                  anonymous,
                  priority,
                  allowPingbacks,
                  serviceContext);

          if (question) {
            _mbThreadLocalService.updateQuestion(message.getThreadId(), true);
          }
        } else {

          // Post reply

          message =
              _mbMessageService.addMessage(
                  parentMessageId,
                  subject,
                  body,
                  mbGroupServiceSettings.getMessageFormat(),
                  inputStreamOVPs,
                  anonymous,
                  priority,
                  allowPingbacks,
                  serviceContext);
        }
      } else {
        List<String> existingFiles = new ArrayList<>();

        for (int i = 1; i <= 5; i++) {
          String path = ParamUtil.getString(actionRequest, "existingPath" + i);

          if (Validator.isNotNull(path)) {
            existingFiles.add(path);
          }
        }

        // Update message

        message =
            _mbMessageService.updateMessage(
                messageId,
                subject,
                body,
                inputStreamOVPs,
                existingFiles,
                priority,
                allowPingbacks,
                serviceContext);

        if (message.isRoot()) {
          _mbThreadLocalService.updateQuestion(message.getThreadId(), question);
        }
      }

      PermissionChecker permissionChecker = themeDisplay.getPermissionChecker();

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

      if (!preview
          && subscribe
          && MBMessagePermission.contains(permissionChecker, message, ActionKeys.SUBSCRIBE)) {

        _mbMessageService.subscribeMessage(message.getMessageId());
      }

      return message;
    } finally {
      for (ObjectValuePair<String, InputStream> inputStreamOVP : inputStreamOVPs) {

        InputStream inputStream = inputStreamOVP.getValue();

        StreamUtil.cleanUp(inputStream);
      }
    }
  }
  public void editLayout(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

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

    long groupId = ParamUtil.getLong(actionRequest, "groupId");
    long liveGroupId = ParamUtil.getLong(actionRequest, "liveGroupId");
    long stagingGroupId = ParamUtil.getLong(actionRequest, "stagingGroupId");
    boolean privateLayout = ParamUtil.getBoolean(actionRequest, "privateLayout");
    long layoutId = ParamUtil.getLong(actionRequest, "layoutId");
    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(actionRequest, "name");
    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(actionRequest, "title");
    Map<Locale, String> descriptionMap =
        LocalizationUtil.getLocalizationMap(actionRequest, "description");
    Map<Locale, String> keywordsMap =
        LocalizationUtil.getLocalizationMap(actionRequest, "keywords");
    Map<Locale, String> robotsMap = LocalizationUtil.getLocalizationMap(actionRequest, "robots");
    String type = ParamUtil.getString(uploadPortletRequest, "type");
    boolean hidden = ParamUtil.getBoolean(uploadPortletRequest, "hidden");
    Map<Locale, String> friendlyURLMap =
        LocalizationUtil.getLocalizationMap(actionRequest, "friendlyURL");
    boolean deleteLogo = ParamUtil.getBoolean(actionRequest, "deleteLogo");

    byte[] iconBytes = null;

    long fileEntryId = ParamUtil.getLong(uploadPortletRequest, "fileEntryId");

    if (fileEntryId > 0) {
      FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

      iconBytes = FileUtil.getBytes(fileEntry.getContentStream());
    }

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

    Layout layout = layoutLocalService.getLayout(groupId, privateLayout, layoutId);

    layout =
        layoutService.updateLayout(
            groupId,
            privateLayout,
            layoutId,
            layout.getParentLayoutId(),
            nameMap,
            titleMap,
            descriptionMap,
            keywordsMap,
            robotsMap,
            type,
            hidden,
            friendlyURLMap,
            !deleteLogo,
            iconBytes,
            serviceContext);

    UnicodeProperties layoutTypeSettingsProperties = layout.getTypeSettingsProperties();

    UnicodeProperties formTypeSettingsProperties =
        PropertiesParamUtil.getProperties(actionRequest, "TypeSettingsProperties--");

    LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

    if (type.equals(LayoutConstants.TYPE_PORTLET)) {
      String layoutTemplateId =
          ParamUtil.getString(
              uploadPortletRequest, "layoutTemplateId", PropsValues.DEFAULT_LAYOUT_TEMPLATE_ID);

      layoutTypePortlet.setLayoutTemplateId(themeDisplay.getUserId(), layoutTemplateId);

      layoutTypeSettingsProperties.putAll(formTypeSettingsProperties);

      layoutService.updateLayout(
          groupId, privateLayout, layoutId, layoutTypeSettingsProperties.toString());
    } else {
      layout.setTypeSettingsProperties(formTypeSettingsProperties);

      layoutTypeSettingsProperties.putAll(layout.getTypeSettingsProperties());

      layoutService.updateLayout(groupId, privateLayout, layoutId, layout.getTypeSettings());
    }

    HttpServletResponse response = PortalUtil.getHttpServletResponse(actionResponse);

    EventsProcessorUtil.process(
        PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
        layoutTypePortlet.getConfigurationActionUpdate(),
        uploadPortletRequest,
        response);

    updateLookAndFeel(
        actionRequest,
        themeDisplay.getCompanyId(),
        liveGroupId,
        stagingGroupId,
        privateLayout,
        layout.getLayoutId(),
        layoutTypeSettingsProperties);
  }
  public void addLayout(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);

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

    long groupId = ParamUtil.getLong(actionRequest, "groupId");
    long liveGroupId = ParamUtil.getLong(actionRequest, "liveGroupId");
    long stagingGroupId = ParamUtil.getLong(actionRequest, "stagingGroupId");
    boolean privateLayout = ParamUtil.getBoolean(actionRequest, "privateLayout");
    long parentLayoutId = ParamUtil.getLong(uploadPortletRequest, "parentLayoutId");
    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(actionRequest, "name");
    Map<Locale, String> titleMap = LocalizationUtil.getLocalizationMap(actionRequest, "title");
    Map<Locale, String> descriptionMap =
        LocalizationUtil.getLocalizationMap(actionRequest, "description");
    Map<Locale, String> keywordsMap =
        LocalizationUtil.getLocalizationMap(actionRequest, "keywords");
    Map<Locale, String> robotsMap = LocalizationUtil.getLocalizationMap(actionRequest, "robots");
    String type = ParamUtil.getString(uploadPortletRequest, "type");
    boolean hidden = ParamUtil.getBoolean(uploadPortletRequest, "hidden");
    Map<Locale, String> friendlyURLMap =
        LocalizationUtil.getLocalizationMap(actionRequest, "friendlyURL");

    long layoutPrototypeId = ParamUtil.getLong(uploadPortletRequest, "layoutPrototypeId");

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

    Layout layout = null;

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

    UnicodeProperties typeSettingsProperties =
        PropertiesParamUtil.getProperties(actionRequest, "TypeSettingsProperties--");

    if (inheritFromParentLayoutId && (parentLayoutId > 0)) {
      Layout parentLayout = layoutLocalService.getLayout(groupId, privateLayout, parentLayoutId);

      layout =
          layoutService.addLayout(
              groupId,
              privateLayout,
              parentLayoutId,
              nameMap,
              titleMap,
              parentLayout.getDescriptionMap(),
              parentLayout.getKeywordsMap(),
              parentLayout.getRobotsMap(),
              parentLayout.getType(),
              parentLayout.getTypeSettings(),
              hidden,
              friendlyURLMap,
              serviceContext);

      inheritMobileRuleGroups(layout, serviceContext);

      if (parentLayout.isTypePortlet()) {
        ActionUtil.copyPreferences(actionRequest, layout, parentLayout);

        SitesUtil.copyLookAndFeel(layout, parentLayout);
      }
    } else if (layoutPrototypeId > 0) {
      LayoutPrototype layoutPrototype =
          layoutPrototypeService.getLayoutPrototype(layoutPrototypeId);

      boolean layoutPrototypeLinkEnabled =
          ParamUtil.getBoolean(
              uploadPortletRequest, "layoutPrototypeLinkEnabled" + layoutPrototype.getUuid());

      serviceContext.setAttribute("layoutPrototypeLinkEnabled", layoutPrototypeLinkEnabled);

      serviceContext.setAttribute("layoutPrototypeUuid", layoutPrototype.getUuid());

      layout =
          layoutService.addLayout(
              groupId,
              privateLayout,
              parentLayoutId,
              nameMap,
              titleMap,
              descriptionMap,
              keywordsMap,
              robotsMap,
              LayoutConstants.TYPE_PORTLET,
              typeSettingsProperties.toString(),
              hidden,
              friendlyURLMap,
              serviceContext);

      // Force propagation from page template to page.
      // See LPS-48430.

      SitesUtil.mergeLayoutPrototypeLayout(layout.getGroup(), layout);
    } else {
      long copyLayoutId = ParamUtil.getLong(uploadPortletRequest, "copyLayoutId");

      Layout copyLayout = null;

      String layoutTemplateId =
          ParamUtil.getString(
              uploadPortletRequest, "layoutTemplateId", PropsValues.DEFAULT_LAYOUT_TEMPLATE_ID);

      if (copyLayoutId > 0) {
        copyLayout = layoutLocalService.fetchLayout(groupId, privateLayout, copyLayoutId);

        if ((copyLayout != null) && copyLayout.isTypePortlet()) {
          LayoutTypePortlet copyLayoutTypePortlet = (LayoutTypePortlet) copyLayout.getLayoutType();

          layoutTemplateId = copyLayoutTypePortlet.getLayoutTemplateId();

          typeSettingsProperties = copyLayout.getTypeSettingsProperties();
        }
      }

      layout =
          layoutService.addLayout(
              groupId,
              privateLayout,
              parentLayoutId,
              nameMap,
              titleMap,
              descriptionMap,
              keywordsMap,
              robotsMap,
              type,
              typeSettingsProperties.toString(),
              hidden,
              friendlyURLMap,
              serviceContext);

      LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();

      layoutTypePortlet.setLayoutTemplateId(themeDisplay.getUserId(), layoutTemplateId);

      layoutService.updateLayout(
          groupId, privateLayout, layout.getLayoutId(), layout.getTypeSettings());

      if ((copyLayout != null) && copyLayout.isTypePortlet()) {
        ActionUtil.copyPreferences(actionRequest, layout, copyLayout);

        SitesUtil.copyLookAndFeel(layout, copyLayout);
      }
    }

    updateLookAndFeel(
        actionRequest,
        themeDisplay.getCompanyId(),
        liveGroupId,
        stagingGroupId,
        privateLayout,
        layout.getLayoutId(),
        layout.getTypeSettingsProperties());
  }