protected String getThemeId() {
    String themeId = null;

    try {
      Layout layout = getLayout();

      Theme theme = layout.getTheme();

      if (theme != null) {
        themeId = theme.getThemeId();
      } else {
        themeId = layout.getThemeId();
      }
    } catch (Exception e) {
      _log.error(e, e);
    }

    return themeId;
  }
  public List<Theme> getThemes(long companyId, long groupId, long userId, boolean wapTheme)
      throws SystemException {

    List<Theme> themes = getThemes(companyId);

    themes = PluginUtil.restrictPlugins(themes, companyId, userId);

    Iterator<Theme> itr = themes.iterator();

    while (itr.hasNext()) {
      Theme theme = itr.next();

      if (theme.getThemeId().equals("controlpanel")
          || !theme.isGroupAvailable(groupId)
          || (theme.isWapTheme() != wapTheme)) {

        itr.remove();
      }
    }

    return themes;
  }
  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }

    Theme theme = null;

    try {
      theme = (Theme) obj;
    } catch (ClassCastException cce) {
      return false;
    }

    String themeId = theme.getThemeId();

    if (getThemeId().equals(themeId)) {
      return true;
    } else {
      return false;
    }
  }
  protected void exportTheme(LayoutSet layoutSet, ZipWriter zipWriter) throws Exception {

    Theme theme = layoutSet.getTheme();

    String lookAndFeelXML =
        ContentUtil.get("com/liferay/portal/dependencies/liferay-look-and-feel.xml.tmpl");

    lookAndFeelXML =
        StringUtil.replace(
            lookAndFeelXML,
            new String[] {"[$TEMPLATE_EXTENSION$]", "[$VIRTUAL_PATH$]"},
            new String[] {theme.getTemplateExtension(), theme.getVirtualPath()});

    String servletContextName = theme.getServletContextName();

    ServletContext servletContext = ServletContextPool.get(servletContextName);

    if (servletContext == null) {
      if (_log.isWarnEnabled()) {
        _log.warn("Servlet context not found for theme " + theme.getThemeId());
      }

      return;
    }

    File themeZip = new File(zipWriter.getPath() + "/theme.zip");

    ZipWriter themeZipWriter = ZipWriterFactoryUtil.getZipWriter(themeZip);

    themeZipWriter.addEntry("liferay-look-and-feel.xml", lookAndFeelXML);

    File cssPath = null;
    File imagesPath = null;
    File javaScriptPath = null;
    File templatesPath = null;

    if (!theme.isLoadFromServletContext()) {
      ThemeLoader themeLoader = ThemeLoaderFactory.getThemeLoader(servletContextName);

      if (themeLoader == null) {
        _log.error(servletContextName + " does not map to a theme loader");
      } else {
        String realPath =
            themeLoader.getFileStorage().getPath() + StringPool.SLASH + theme.getName();

        cssPath = new File(realPath + "/css");
        imagesPath = new File(realPath + "/images");
        javaScriptPath = new File(realPath + "/javascript");
        templatesPath = new File(realPath + "/templates");
      }
    } else {
      cssPath = new File(servletContext.getRealPath(theme.getCssPath()));
      imagesPath = new File(servletContext.getRealPath(theme.getImagesPath()));
      javaScriptPath = new File(servletContext.getRealPath(theme.getJavaScriptPath()));
      templatesPath = new File(servletContext.getRealPath(theme.getTemplatesPath()));
    }

    exportThemeFiles("css", cssPath, themeZipWriter);
    exportThemeFiles("images", imagesPath, themeZipWriter);
    exportThemeFiles("javascript", javaScriptPath, themeZipWriter);
    exportThemeFiles("templates", templatesPath, themeZipWriter);
  }
  @Override
  public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException {

    try {

      // Do not randomize look and feel unless the user is logged in

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

      if (!themeDisplay.isSignedIn()) {
        return;
      }

      // Do not randomize look and feel unless the user is accessing the
      // portal

      String requestURI = GetterUtil.getString(request.getRequestURI());

      if (!requestURI.endsWith("/portal/layout")) {
        return;
      }

      // Do not randomize look and feel unless the user is accessing a
      // personal layout

      Layout layout = themeDisplay.getLayout();

      if (layout == null) {
        return;
      }

      boolean wapTheme = BrowserSnifferUtil.isWap(request);

      List<Theme> themes =
          ThemeLocalServiceUtil.getPageThemes(
              themeDisplay.getCompanyId(),
              themeDisplay.getScopeGroupId(),
              themeDisplay.getUserId(),
              wapTheme);

      if (!themes.isEmpty()) {
        Theme theme = themes.get(RandomUtil.nextInt(themes.size()));

        List<ColorScheme> colorSchemes = theme.getColorSchemes();

        ColorScheme colorScheme = colorSchemes.get(RandomUtil.nextInt(colorSchemes.size()));

        LayoutServiceUtil.updateLookAndFeel(
            layout.getGroupId(),
            layout.isPrivateLayout(),
            layout.getPlid(),
            theme.getThemeId(),
            colorScheme.getColorSchemeId(),
            layout.getCss(),
            wapTheme);

        themeDisplay.setLookAndFeel(theme, colorScheme);

        request.setAttribute(WebKeys.THEME, theme);
        request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
      }
    } catch (Exception e) {
      _log.error(e, e);

      throw new ActionException(e);
    }
  }