@Override
  protected String getBody(SocialActivity activity, ServiceContext serviceContext)
      throws Exception {

    String link = getLink(activity, serviceContext);

    String text = StringPool.BLANK;

    int activityType = activity.getType();

    if (activityType == JIRAActivityKeys.ADD_CHANGE) {
      JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject(activity.getExtraData());

      text =
          interpretJIRAChangeItems(
              extraDataJSONObject.getJSONArray("jiraChangeItems"), serviceContext);
    } else if (activityType == JIRAActivityKeys.ADD_COMMENT) {
      long jiraActionId = GetterUtil.getLong(getJSONValue(activity.getExtraData(), "jiraActionId"));

      JIRAAction jiraAction = JIRAActionLocalServiceUtil.getJIRAAction(jiraActionId);

      text = HtmlUtil.escape(jiraAction.getBody());
    } else if (activityType == JIRAActivityKeys.ADD_ISSUE) {
      JIRAIssue jiraIssue = JIRAIssueLocalServiceUtil.getJIRAIssue(activity.getClassPK());

      text = HtmlUtil.escape(jiraIssue.getSummary());
    }

    return wrapLink(link, text);
  }
  protected void addLayout(long parentLayoutId, JSONObject layoutJSONObject) throws Exception {

    String name = layoutJSONObject.getString("name");
    String title = layoutJSONObject.getString("title");
    boolean hidden = layoutJSONObject.getBoolean("hidden");

    String friendlyURL = layoutJSONObject.getString("friendlyURL");

    if (Validator.isNotNull(friendlyURL) && !friendlyURL.startsWith(StringPool.SLASH)) {

      friendlyURL = StringPool.SLASH + friendlyURL;
    }

    Layout layout =
        LayoutLocalServiceUtil.addLayout(
            userId,
            groupId,
            privateLayout,
            parentLayoutId,
            name,
            title,
            StringPool.BLANK,
            LayoutConstants.TYPE_PORTLET,
            hidden,
            friendlyURL,
            serviceContext);

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

    String layoutTemplateId =
        layoutJSONObject.getString("layoutTemplateId", _defaultLayoutTemplateId);

    if (Validator.isNotNull(layoutTemplateId)) {
      layoutTypePortlet.setLayoutTemplateId(userId, layoutTemplateId, false);
    }

    JSONArray columnsJSONArray = layoutJSONObject.getJSONArray("columns");

    addLayoutColumns(layout, columnsJSONArray);

    LayoutLocalServiceUtil.updateLayout(
        groupId, layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());

    JSONArray layoutsJSONArray = layoutJSONObject.getJSONArray("layouts");

    addLayouts(layout.getLayoutId(), layoutsJSONArray);
  }
  protected void updateBasicConfiguration(
      PortletConfig portletConfig, ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    boolean displayScopeFacet =
        GetterUtil.getBoolean(getParameter(actionRequest, "displayScopeFacet"));
    boolean displayAssetCategoriesFacet =
        GetterUtil.getBoolean(getParameter(actionRequest, "displayAssetCategoriesFacet"));
    boolean displayAssetTagsFacet =
        GetterUtil.getBoolean(getParameter(actionRequest, "displayAssetTagsFacet"));
    boolean displayAssetTypeFacet =
        GetterUtil.getBoolean(getParameter(actionRequest, "displayAssetTypeFacet"));
    boolean displayFolderFacet =
        GetterUtil.getBoolean(getParameter(actionRequest, "displayFolderFacet"));
    boolean displayUserFacet =
        GetterUtil.getBoolean(getParameter(actionRequest, "displayUserFacet"));
    boolean displayModifiedRangeFacet =
        GetterUtil.getBoolean(getParameter(actionRequest, "displayModifiedRangeFacet"));

    String searchConfiguration = ContentUtil.get(SearchWebConfigurationValues.FACET_CONFIGURATION);

    JSONObject configurationJSONObject = JSONFactoryUtil.createJSONObject(searchConfiguration);

    JSONArray oldFacetsJSONArray = configurationJSONObject.getJSONArray("facets");

    if (oldFacetsJSONArray == null) {
      if (_log.isWarnEnabled()) {
        _log.warn(
            "The resource "
                + SearchWebConfigurationValues.FACET_CONFIGURATION
                + " is missing a valid facets JSON array");
      }
    }

    JSONArray newFacetsJSONArray = JSONFactoryUtil.createJSONArray();

    for (int i = 0; i < oldFacetsJSONArray.length(); i++) {
      JSONObject oldFacetJSONObject = oldFacetsJSONArray.getJSONObject(i);

      String fieldName = oldFacetJSONObject.getString("fieldName");

      if ((displayScopeFacet && fieldName.equals("groupId"))
          || (displayAssetCategoriesFacet && fieldName.equals("assetCategoryIds"))
          || (displayAssetTagsFacet && fieldName.equals("assetTagNames"))
          || (displayAssetTypeFacet && fieldName.equals("entryClassName"))
          || (displayFolderFacet && fieldName.equals("folderId"))
          || (displayUserFacet && fieldName.equals("userId"))
          || (displayModifiedRangeFacet && fieldName.equals("modified"))) {

        newFacetsJSONArray.put(oldFacetJSONObject);
      }
    }

    configurationJSONObject.put("facets", newFacetsJSONArray);

    searchConfiguration = configurationJSONObject.toString();

    setPreference(actionRequest, "searchConfiguration", searchConfiguration);
  }
  public static String toHTMLTable(JSONObject json, int limit, String tableId, String tableClass) {
    if (!json.isNull("data")) {

      JSONArray columns = null;
      if (!(json.isNull("columns"))) {
        columns = json.getJSONArray("columns");
      } else if (!json.isNull("columnDefs")) {
        _log.info("TODO: support columnDefs");
      }

      return (toHTMLTable(json.getJSONArray("data"), columns, limit, tableId, tableClass));
    } else if (!json.isNull("error")) {
      return (json.getString("error"));
    }

    return (null);
  }
  @Override
  protected BooleanClause doGetFacetClause() {
    SearchContext searchContext = getSearchContext();

    FacetConfiguration facetConfiguration = getFacetConfiguration();

    JSONObject dataJSONObject = facetConfiguration.getData();

    String[] values = null;

    if (isStatic() && dataJSONObject.has("values")) {
      JSONArray valuesJSONArray = dataJSONObject.getJSONArray("values");

      values = new String[valuesJSONArray.length()];

      for (int i = 0; i < valuesJSONArray.length(); i++) {
        values[i] = valuesJSONArray.getString(i);
      }
    }

    String[] valuesParam =
        StringUtil.split(GetterUtil.getString(searchContext.getAttribute(getFieldName())));

    if (!isStatic() && (valuesParam != null) && (valuesParam.length > 0)) {
      values = valuesParam;
    }

    if ((values == null) || (values.length == 0)) {
      return null;
    }

    BooleanQuery facetQuery = BooleanQueryFactoryUtil.create(searchContext);

    for (String value : values) {
      FacetValueValidator facetValueValidator = getFacetValueValidator();

      if ((searchContext.getUserId() > 0) && !facetValueValidator.check(searchContext, value)) {

        continue;
      }

      TermQuery termQuery = TermQueryFactoryUtil.create(searchContext, getFieldName(), value);

      try {
        facetQuery.add(termQuery, BooleanClauseOccur.SHOULD);
      } catch (ParseException pe) {
        _log.error(pe, pe);
      }
    }

    if (!facetQuery.hasClauses()) {
      return null;
    }

    return BooleanClauseFactoryUtil.create(
        searchContext, facetQuery, BooleanClauseOccur.MUST.getName());
  }
  protected void updateActivitySettings(ActionRequest actionRequest) throws Exception {

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

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

    JSONObject settingsJSONObject = JSONFactoryUtil.createJSONObject(settingsJSON);

    JSONArray actionsJSONArray = settingsJSONObject.getJSONArray("actions");

    String modelName = settingsJSONObject.getString("modelName");

    for (int i = 0; i < actionsJSONArray.length(); i++) {
      JSONObject actionJSONObject = actionsJSONArray.getJSONObject(i);

      int activityType = actionJSONObject.getInt("activityType");

      SocialActivityDefinition activityDefinition =
          SocialActivitySettingServiceUtil.getActivityDefinition(
              themeDisplay.getSiteGroupIdOrLiveGroupId(), modelName, activityType);

      if (activityDefinition == null) {
        continue;
      }

      List<SocialActivityCounterDefinition> activityCounterDefinitions = new ArrayList<>();

      activityCounterDefinitions.add(
          updateActivityCounterDefinition(
              actionJSONObject,
              activityDefinition,
              SocialActivityCounterConstants.NAME_CONTRIBUTION));

      activityCounterDefinitions.add(
          updateActivityCounterDefinition(
              actionJSONObject,
              activityDefinition,
              SocialActivityCounterConstants.NAME_PARTICIPATION));

      activityCounterDefinitions.add(
          updateActivityCounterDefinition(
              actionJSONObject,
              activityDefinition,
              SocialActivityCounterConstants.NAME_POPULARITY));

      SocialActivitySettingServiceUtil.updateActivitySettings(
          themeDisplay.getSiteGroupIdOrLiveGroupId(),
          modelName,
          activityType,
          activityCounterDefinitions);
    }
  }
  protected void doAddLayouts(InputStream inputStream) throws Exception {
    String sitemapJSON = getSitemapJSON(inputStream);

    JSONObject sitemapJSONObject = JSONFactoryUtil.createJSONObject(sitemapJSON);

    LayoutLocalServiceUtil.deleteLayouts(groupId, privateLayout, new ServiceContext());

    _defaultLayoutTemplateId = sitemapJSONObject.getString("layoutTemplateId", StringPool.BLANK);

    updateLayoutSetThemeId(sitemapJSONObject);

    JSONArray layoutsJSONArray = sitemapJSONObject.getJSONArray("layouts");

    addLayouts(LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, layoutsJSONArray);
  }