public String transform(
      ThemeDisplay themeDisplay, Map<String, Object> contextObjects, String script, String langType)
      throws Exception {

    if (Validator.isNull(langType)) {
      return null;
    }

    long companyId = 0;
    long companyGroupId = 0;
    long scopeGroupId = 0;
    long siteGroupId = 0;

    if (themeDisplay != null) {
      companyId = themeDisplay.getCompanyId();
      companyGroupId = themeDisplay.getCompanyGroupId();
      scopeGroupId = themeDisplay.getScopeGroupId();
      siteGroupId = themeDisplay.getSiteGroupId();
    }

    String templateId = String.valueOf(contextObjects.get("template_id"));

    templateId = getTemplateId(templateId, companyId, companyGroupId, scopeGroupId);

    Template template = getTemplate(templateId, script, langType);

    UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

    try {
      if (contextObjects != null) {
        for (String key : contextObjects.keySet()) {
          template.put(key, contextObjects.get(key));
        }
      }

      template.put("company", getCompany(themeDisplay, companyId));
      template.put("companyId", companyId);
      template.put("device", getDevice(themeDisplay));

      String templatesPath = getTemplatesPath(companyId, scopeGroupId);

      template.put("journalTemplatesPath", templatesPath);
      template.put("permissionChecker", PermissionThreadLocal.getPermissionChecker());
      template.put(
          "randomNamespace", PwdGenerator.getPassword(PwdGenerator.KEY3, 4) + StringPool.UNDERLINE);
      template.put("scopeGroupId", scopeGroupId);
      template.put("siteGroupId", siteGroupId);
      template.put("templatesPath", templatesPath);

      // Deprecated variables

      template.put("groupId", scopeGroupId);

      mergeTemplate(template, unsyncStringWriter);
    } catch (Exception e) {
      throw new TransformException("Unhandled exception", e);
    }

    return unsyncStringWriter.toString();
  }
  @Override
  public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) {

    try {
      ThemeDisplay themeDisplay =
          (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);

      if (!MentionsUtil.isMentionsEnabled(themeDisplay.getSiteGroupId())) {

        return;
      }

      HttpServletRequest request = PortalUtil.getHttpServletRequest(resourceRequest);

      JSONArray jsonArray = getJSONArray(request);

      HttpServletResponse response = PortalUtil.getHttpServletResponse(resourceResponse);

      response.setContentType(ContentTypes.APPLICATION_JSON);

      ServletResponseUtil.write(response, jsonArray.toString());
    } catch (Exception e) {
      _log.error(e, e);
    }

    return;
  }
  public long getDisplayStyleGroupId() {
    if (_displayStyleGroupId != 0) {
      return _displayStyleGroupId;
    }

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

    _displayStyleGroupId =
        PrefsParamUtil.getLong(
            _portletPreferences, _request, "displayStyleGroupId", themeDisplay.getSiteGroupId());

    return _displayStyleGroupId;
  }
  @Override
  protected boolean isRSSFeedsEnabled(HttpServletRequest request) throws Exception {

    SettingsFactory settingsFactory = SettingsFactoryUtil.getSettingsFactory();

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

    BlogsGroupServiceSettings rssBlogsGroupServiceSettings =
        settingsFactory.getSettings(
            BlogsGroupServiceSettings.class,
            new GroupServiceSettingsLocator(
                themeDisplay.getSiteGroupId(), BlogsConstants.SERVICE_NAME));

    return rssBlogsGroupServiceSettings.enableRss();
  }
  @Override
  public PortletPreferences getPortletSetup(
      HttpServletRequest request, String portletId, String defaultPreferences) {

    PortletRequest portletRequest =
        (PortletRequest) request.getAttribute(JavaConstants.JAVAX_PORTLET_REQUEST);

    if (portletRequest instanceof ConfigurationPortletRequest) {
      PortletRequestWrapper portletRequestWrapper = (PortletRequestWrapper) portletRequest;

      return portletRequestWrapper.getPreferences();
    }

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

    return getPortletSetup(
        themeDisplay.getSiteGroupId(), themeDisplay.getLayout(), portletId, defaultPreferences);
  }
  @Override
  public PortletPreferencesIds getPortletPreferencesIds(
      HttpServletRequest request, Layout layout, String portletId) throws PortalException {

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

    long siteGroupId = themeDisplay.getSiteGroupId();
    long userId = PortalUtil.getUserId(request);
    LayoutTypePortlet layoutTypePortlet = themeDisplay.getLayoutTypePortlet();

    boolean modeEditGuest = false;

    String portletMode = ParamUtil.getString(request, "p_p_mode");

    if (portletMode.equals(LiferayPortletMode.EDIT_GUEST.toString())
        || ((layoutTypePortlet != null)
            && layoutTypePortlet.hasModeEditGuestPortletId(portletId))) {

      modeEditGuest = true;
    }

    return getPortletPreferencesIds(siteGroupId, userId, layout, portletId, modeEditGuest);
  }
  public String transform(
      ThemeDisplay themeDisplay,
      Map<String, String> tokens,
      String viewMode,
      String languageId,
      String xml,
      String script,
      String langType)
      throws Exception {

    // Setup listeners

    if (_log.isDebugEnabled()) {
      _log.debug("Language " + languageId);
    }

    if (Validator.isNull(viewMode)) {
      viewMode = Constants.VIEW;
    }

    if (_logTokens.isDebugEnabled()) {
      String tokensString = PropertiesUtil.list(tokens);

      _logTokens.debug(tokensString);
    }

    if (_logTransformBefore.isDebugEnabled()) {
      _logTransformBefore.debug(xml);
    }

    List<TransformerListener> transformerListeners = new ArrayList<TransformerListener>();

    for (String transformerListenersClassName : _transformerListenerClassNames) {

      TransformerListener transformerListener = null;

      try {
        if (_log.isDebugEnabled()) {
          _log.debug("Instantiate listener " + transformerListenersClassName);
        }

        ClassLoader classLoader = PortalClassLoaderUtil.getClassLoader();

        transformerListener =
            (TransformerListener)
                InstanceFactory.newInstance(classLoader, transformerListenersClassName);

        transformerListeners.add(transformerListener);
      } catch (Exception e) {
        _log.error(e, e);
      }

      // Modify XML

      if (_logXmlBeforeListener.isDebugEnabled()) {
        _logXmlBeforeListener.debug(xml);
      }

      if (transformerListener != null) {
        xml = transformerListener.onXml(xml, languageId, tokens);

        if (_logXmlAfterListener.isDebugEnabled()) {
          _logXmlAfterListener.debug(xml);
        }
      }

      // Modify script

      if (_logScriptBeforeListener.isDebugEnabled()) {
        _logScriptBeforeListener.debug(script);
      }

      if (transformerListener != null) {
        script = transformerListener.onScript(script, xml, languageId, tokens);

        if (_logScriptAfterListener.isDebugEnabled()) {
          _logScriptAfterListener.debug(script);
        }
      }
    }

    // Transform

    String output = null;

    if (Validator.isNull(langType)) {
      output = LocalizationUtil.getLocalization(xml, languageId);
    } else {
      long companyId = 0;
      long companyGroupId = 0;
      long articleGroupId = 0;

      if (tokens != null) {
        companyId = GetterUtil.getLong(tokens.get("company_id"));
        companyGroupId = GetterUtil.getLong(tokens.get("company_group_id"));
        articleGroupId = GetterUtil.getLong(tokens.get("article_group_id"));
      }

      long scopeGroupId = 0;
      long siteGroupId = 0;

      if (themeDisplay != null) {
        companyId = themeDisplay.getCompanyId();
        companyGroupId = themeDisplay.getCompanyGroupId();
        scopeGroupId = themeDisplay.getScopeGroupId();
        siteGroupId = themeDisplay.getSiteGroupId();
      }

      String templateId = tokens.get("template_id");

      templateId = getTemplateId(templateId, companyId, companyGroupId, articleGroupId);

      Template template = getTemplate(templateId, tokens, languageId, xml, script, langType);

      UnsyncStringWriter unsyncStringWriter = new UnsyncStringWriter();

      try {
        if (Validator.isNotNull(xml)) {
          Document document = SAXReaderUtil.read(xml);

          Element rootElement = document.getRootElement();

          List<TemplateNode> templateNodes = getTemplateNodes(themeDisplay, rootElement);

          if (templateNodes != null) {
            for (TemplateNode templateNode : templateNodes) {
              template.put(templateNode.getName(), templateNode);
            }
          }

          Element requestElement = rootElement.element("request");

          template.put("request", insertRequestVariables(requestElement));

          template.put("xmlRequest", requestElement.asXML());
        }

        template.put("articleGroupId", articleGroupId);
        template.put("company", getCompany(themeDisplay, companyId));
        template.put("companyId", companyId);
        template.put("device", getDevice(themeDisplay));

        String templatesPath = getTemplatesPath(companyId, articleGroupId);

        template.put("journalTemplatesPath", templatesPath);

        Locale locale = LocaleUtil.fromLanguageId(languageId);

        template.put("locale", locale);

        template.put("permissionChecker", PermissionThreadLocal.getPermissionChecker());
        template.put(
            "randomNamespace",
            PwdGenerator.getPassword(PwdGenerator.KEY3, 4) + StringPool.UNDERLINE);
        template.put("scopeGroupId", scopeGroupId);
        template.put("siteGroupId", siteGroupId);
        template.put("templatesPath", templatesPath);
        template.put("viewMode", viewMode);

        // Deprecated variables

        template.put("groupId", articleGroupId);

        mergeTemplate(template, unsyncStringWriter);
      } catch (Exception e) {
        if (e instanceof DocumentException) {
          throw new TransformException("Unable to read XML document", e);
        } else if (e instanceof IOException) {
          throw new TransformException("Error reading template", e);
        } else if (e instanceof TransformException) {
          throw (TransformException) e;
        } else {
          throw new TransformException("Unhandled exception", e);
        }
      }

      output = unsyncStringWriter.toString();
    }

    // Postprocess output

    for (TransformerListener transformerListener : transformerListeners) {

      // Modify output

      if (_logOutputBeforeListener.isDebugEnabled()) {
        _logOutputBeforeListener.debug(output);
      }

      output = transformerListener.onOutput(output, languageId, tokens);

      if (_logOutputAfterListener.isDebugEnabled()) {
        _logOutputAfterListener.debug(output);
      }
    }

    if (_logTransfromAfter.isDebugEnabled()) {
      _logTransfromAfter.debug(output);
    }

    return output;
  }