protected String importTheme(LayoutSet layoutSet, InputStream themeZip) throws Exception {

    ThemeLoader themeLoader = ThemeLoaderFactory.getDefaultThemeLoader();

    if (themeLoader == null) {
      _log.error("No theme loaders are deployed");

      return null;
    }

    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(themeZip);

    String lookAndFeelXML = zipReader.getEntryAsString("liferay-look-and-feel.xml");

    String themeId = String.valueOf(layoutSet.getGroupId());

    if (layoutSet.isPrivateLayout()) {
      themeId += "-private";
    } else {
      themeId += "-public";
    }

    if (PropsValues.THEME_LOADER_NEW_THEME_ID_ON_IMPORT) {
      Date now = new Date();

      themeId += "-" + Time.getShortTimestamp(now);
    }

    String themeName = themeId;

    lookAndFeelXML =
        StringUtil.replace(
            lookAndFeelXML,
            new String[] {"[$GROUP_ID$]", "[$THEME_ID$]", "[$THEME_NAME$]"},
            new String[] {String.valueOf(layoutSet.getGroupId()), themeId, themeName});

    FileUtil.deltree(themeLoader.getFileStorage() + StringPool.SLASH + themeId);

    List<String> zipEntries = zipReader.getEntries();

    for (String zipEntry : zipEntries) {
      String key = zipEntry;

      if (key.equals("liferay-look-and-feel.xml")) {
        FileUtil.write(
            themeLoader.getFileStorage() + StringPool.SLASH + themeId + StringPool.SLASH + key,
            lookAndFeelXML.getBytes());
      } else {
        InputStream is = zipReader.getEntryAsInputStream(zipEntry);

        FileUtil.write(
            themeLoader.getFileStorage() + StringPool.SLASH + themeId + StringPool.SLASH + key, is);
      }
    }

    themeLoader.loadThemes();

    ClusterRequest clusterRequest =
        ClusterRequest.createMulticastRequest(_loadThemesMethodHandler, true);

    clusterRequest.setFireAndForget(true);

    ClusterExecutorUtil.execute(clusterRequest);

    themeId += PortletConstants.WAR_SEPARATOR + themeLoader.getServletContextName();

    return PortalUtil.getJsSafePortletId(themeId);
  }
  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);
  }