protected void mergePortlets(Layout layout, String newTypeSettings, String portletsMergeMode) {

    try {
      UnicodeProperties previousTypeSettingsProperties = layout.getTypeSettingsProperties();

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

      LayoutTemplate previousLayoutTemplate = previousLayoutType.getLayoutTemplate();

      List<String> previousColumns = previousLayoutTemplate.getColumns();

      UnicodeProperties newTypeSettingsProperties = new UnicodeProperties(true);

      newTypeSettingsProperties.load(newTypeSettings);

      String layoutTemplateId =
          newTypeSettingsProperties.getProperty(LayoutTypePortletConstants.LAYOUT_TEMPLATE_ID);

      previousTypeSettingsProperties.setProperty(
          LayoutTypePortletConstants.LAYOUT_TEMPLATE_ID, layoutTemplateId);

      LayoutTemplate newLayoutTemplate =
          LayoutTemplateLocalServiceUtil.getLayoutTemplate(layoutTemplateId, false, null);

      String[] newPortletIds = new String[0];

      for (String columnId : newLayoutTemplate.getColumns()) {
        String columnValue = newTypeSettingsProperties.getProperty(columnId);

        String[] portletIds = StringUtil.split(columnValue);

        if (!previousColumns.contains(columnId)) {
          newPortletIds = ArrayUtil.append(newPortletIds, portletIds);
        } else {
          String[] previousPortletIds =
              StringUtil.split(previousTypeSettingsProperties.getProperty(columnId));

          portletIds = appendPortletIds(previousPortletIds, portletIds, portletsMergeMode);

          previousTypeSettingsProperties.setProperty(columnId, StringUtil.merge(portletIds));
        }
      }

      // Add portlets in non-existent column to the first column

      String columnId = previousColumns.get(0);

      String[] portletIds = StringUtil.split(previousTypeSettingsProperties.getProperty(columnId));

      appendPortletIds(portletIds, newPortletIds, portletsMergeMode);

      previousTypeSettingsProperties.setProperty(columnId, StringUtil.merge(portletIds));

      layout.setTypeSettings(previousTypeSettingsProperties.toString());
    } catch (IOException ioe) {
      layout.setTypeSettings(newTypeSettings);
    }
  }
  protected void setThemeSettingProperties(
      ActionRequest actionRequest,
      UnicodeProperties typeSettingsProperties,
      Map<String, ThemeSetting> themeSettings,
      String device,
      boolean isLayout)
      throws PortalException {

    Layout layout = null;

    if (isLayout) {
      long groupId = ParamUtil.getLong(actionRequest, "groupId");
      boolean privateLayout = ParamUtil.getBoolean(actionRequest, "privateLayout");
      long layoutId = ParamUtil.getLong(actionRequest, "layoutId");

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

    for (String key : themeSettings.keySet()) {
      ThemeSetting themeSetting = themeSettings.get(key);

      String property = device + "ThemeSettingsProperties--" + key + StringPool.DOUBLE_DASH;

      String value = ParamUtil.getString(actionRequest, property, themeSetting.getValue());

      if ((isLayout && !Validator.equals(value, layout.getDefaultThemeSetting(key, device, false)))
          || (!isLayout && !value.equals(themeSetting.getValue()))) {

        typeSettingsProperties.setProperty(ThemeSettingImpl.namespaceProperty(device, key), value);
      }
    }
  }
  public static void updateSetup(HttpServletRequest request, HttpServletResponse response)
      throws Exception {

    UnicodeProperties unicodeProperties =
        PropertiesParamUtil.getProperties(request, _PROPERTIES_PREFIX);

    unicodeProperties.setProperty(
        PropsKeys.LIFERAY_HOME, SystemProperties.get(PropsKeys.LIFERAY_HOME));

    boolean databaseConfigured = _isDatabaseConfigured(unicodeProperties);

    _processDatabaseProperties(request, unicodeProperties, databaseConfigured);

    updateLanguage(request, response);

    unicodeProperties.put(PropsKeys.SETUP_WIZARD_ENABLED, String.valueOf(false));

    PropsUtil.addProperties(unicodeProperties);

    if (!databaseConfigured) {
      _reloadServletContext(request, unicodeProperties);
    }

    _updateCompany(request);
    _updateAdminUser(request, unicodeProperties);

    _initPlugins();

    boolean propertiesFileCreated = _writePropertiesFile(unicodeProperties);

    HttpSession session = request.getSession();

    session.setAttribute(WebKeys.SETUP_WIZARD_PROPERTIES, unicodeProperties);
    session.setAttribute(WebKeys.SETUP_WIZARD_PROPERTIES_FILE_CREATED, propertiesFileCreated);
  }
  protected void fixImportTypeSettings(Layout layout) throws Exception {
    Object[] friendlyURLInfo = extractFriendlyURLInfo(layout);

    if (friendlyURLInfo == null) {
      return;
    }

    String friendlyURL = (String) friendlyURLInfo[0];

    if (!friendlyURL.equals(_SAME_GROUP_FRIENDLY_URL)) {
      return;
    }

    Group group = layout.getGroup();

    UnicodeProperties typeSettings = layout.getTypeSettingsProperties();

    String url = (String) friendlyURLInfo[1];

    int x = (Integer) friendlyURLInfo[2];
    int y = (Integer) friendlyURLInfo[3];

    typeSettings.setProperty(
        "url", url.substring(0, x) + group.getFriendlyURL() + url.substring(y));
  }
  public static void checkRepository(
      long repositoryId,
      Map<String, String> parameters,
      UnicodeProperties typeSettingsProperties,
      String typeSettingsKey)
      throws PortalException, RepositoryException {

    if (!typeSettingsProperties.containsKey(typeSettingsKey)) {
      org.apache.chemistry.opencmis.client.api.Repository cmisRepository =
          _sessionFactory.getRepositories(parameters).get(0);

      typeSettingsProperties.setProperty(typeSettingsKey, cmisRepository.getId());

      try {
        Repository repository = RepositoryUtil.findByPrimaryKey(repositoryId);

        repository.setTypeSettingsProperties(typeSettingsProperties);

        RepositoryUtil.update(repository, false);
      } catch (Exception e) {
        throw new RepositoryException(e);
      }
    }

    parameters.put(
        SessionParameter.REPOSITORY_ID,
        getTypeSettingsValue(typeSettingsProperties, typeSettingsKey));
  }
  @Override
  public void removeNestedColumns(String portletNamespace) {
    UnicodeProperties typeSettingsProperties = getTypeSettingsProperties();

    UnicodeProperties newTypeSettingsProperties = new UnicodeProperties();

    for (Map.Entry<String, String> entry : typeSettingsProperties.entrySet()) {

      String key = entry.getKey();

      if (!key.startsWith(portletNamespace)) {
        newTypeSettingsProperties.setProperty(key, entry.getValue());
      }
    }

    Layout layout = getLayout();

    layout.setTypeSettingsProperties(newTypeSettingsProperties);

    String nestedColumnIds =
        GetterUtil.getString(getTypeSettingsProperty(LayoutTypePortletConstants.NESTED_COLUMN_IDS));

    String[] nestedColumnIdsArray =
        ArrayUtil.removeByPrefix(StringUtil.split(nestedColumnIds), portletNamespace);

    setTypeSettingsProperty(
        LayoutTypePortletConstants.NESTED_COLUMN_IDS, StringUtil.merge(nestedColumnIdsArray));
  }
  @Override
  public Group disableTrash(Group group) {
    UnicodeProperties typeSettingsProperties = group.getParentLiveGroupTypeSettingsProperties();

    typeSettingsProperties.setProperty("trashEnabled", StringPool.FALSE);

    group.setTypeSettingsProperties(typeSettingsProperties);

    return GroupLocalServiceUtil.updateGroup(group);
  }
  protected UnicodeProperties addLDAPServer(long companyId, UnicodeProperties properties)
      throws Exception {

    String defaultPostfix = LDAPSettingsUtil.getPropertyPostfix(0);

    String[] defaultKeys = new String[_KEYS.length];

    for (int i = 0; i < _KEYS.length; i++) {
      defaultKeys[i] = _KEYS[i] + defaultPostfix;
    }

    long ldapServerId = CounterLocalServiceUtil.increment();

    String postfix = LDAPSettingsUtil.getPropertyPostfix(ldapServerId);

    String[] keys = properties.keySet().toArray(new String[0]);

    for (String key : keys) {
      if (ArrayUtil.contains(defaultKeys, key)) {
        String value = properties.remove(key);

        if (key.equals(PropsKeys.LDAP_SECURITY_CREDENTIALS + defaultPostfix)
            && value.equals(Portal.TEMP_OBFUSCATION_VALUE)) {

          value = PrefsPropsUtil.getString(PropsKeys.LDAP_SECURITY_CREDENTIALS);
        }

        properties.setProperty(key.replace(defaultPostfix, postfix), value);
      }
    }

    PortletPreferences preferences = PrefsPropsUtil.getPreferences(companyId);

    String ldapServerIds = preferences.getValue("ldap.server.ids", StringPool.BLANK);

    ldapServerIds = StringUtil.add(ldapServerIds, String.valueOf(ldapServerId));

    properties.setProperty("ldap.server.ids", ldapServerIds);

    return properties;
  }
  protected void updateMergePages(ActionRequest actionRequest, long liveGroupId) throws Exception {

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

    Group liveGroup = groupLocalService.getGroup(liveGroupId);

    UnicodeProperties typeSettingsProperties = liveGroup.getTypeSettingsProperties();

    typeSettingsProperties.setProperty(
        "mergeGuestPublicPages", String.valueOf(mergeGuestPublicPages));

    groupService.updateGroup(liveGroupId, liveGroup.getTypeSettings());
  }
  public static void updateLastPublishDate(LayoutSet layoutSet, long lastPublishDate)
      throws Exception {

    UnicodeProperties settingsProperties = layoutSet.getSettingsProperties();

    if (lastPublishDate <= 0) {
      settingsProperties.remove("last-publish-date");
    } else {
      settingsProperties.setProperty("last-publish-date", String.valueOf(lastPublishDate));
    }

    LayoutSetLocalServiceUtil.updateSettings(
        layoutSet.getGroupId(), layoutSet.isPrivateLayout(), settingsProperties.toString());
  }
  protected void importJournalArticle(
      PortletDataContext portletDataContext, Layout layout, Element layoutElement)
      throws Exception {

    UnicodeProperties typeSettingsProperties = layout.getTypeSettingsProperties();

    String articleId = typeSettingsProperties.getProperty("article-id", StringPool.BLANK);

    if (Validator.isNull(articleId)) {
      return;
    }

    JournalPortletDataHandlerImpl.importReferencedData(portletDataContext, layoutElement);

    Element structureElement = layoutElement.element("structure");

    if (structureElement != null) {
      JournalPortletDataHandlerImpl.importStructure(portletDataContext, structureElement);
    }

    Element templateElement = layoutElement.element("template");

    if (templateElement != null) {
      JournalPortletDataHandlerImpl.importTemplate(portletDataContext, templateElement);
    }

    Element articleElement = layoutElement.element("article");

    if (articleElement != null) {
      JournalPortletDataHandlerImpl.importArticle(portletDataContext, articleElement);
    }

    Map<String, String> articleIds =
        (Map<String, String>)
            portletDataContext.getNewPrimaryKeysMap(JournalArticle.class + ".articleId");

    articleId = MapUtil.getString(articleIds, articleId, articleId);

    typeSettingsProperties.setProperty("article-id", articleId);

    JournalContentSearchLocalServiceUtil.updateContentSearch(
        portletDataContext.getScopeGroupId(),
        layout.isPrivateLayout(),
        layout.getLayoutId(),
        StringPool.BLANK,
        articleId,
        true);
  }
  @Override
  public void updateStagedPortlets(long groupId, Map<String, String> stagedPortletIds)
      throws PortalException {

    Group group = groupPersistence.findByPrimaryKey(groupId);

    GroupPermissionUtil.check(getPermissionChecker(), group, ActionKeys.UPDATE);

    UnicodeProperties typeSettingsProperties = group.getTypeSettingsProperties();

    for (String stagedPortletId : stagedPortletIds.keySet()) {
      typeSettingsProperties.setProperty(
          StagingUtil.getStagedPortletId(stagedPortletId), stagedPortletIds.get(stagedPortletId));
    }

    groupLocalService.updateGroup(group);
  }
  protected void fixTypeSettings(Layout layout) throws Exception {
    if (!layout.isTypeURL()) {
      return;
    }

    UnicodeProperties typeSettings = layout.getTypeSettingsProperties();

    String url = GetterUtil.getString(typeSettings.getProperty("url"));

    String friendlyURLPrivateGroupPath =
        PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_GROUP_SERVLET_MAPPING;
    String friendlyURLPrivateUserPath =
        PropsValues.LAYOUT_FRIENDLY_URL_PRIVATE_USER_SERVLET_MAPPING;
    String friendlyURLPublicPath = PropsValues.LAYOUT_FRIENDLY_URL_PUBLIC_SERVLET_MAPPING;

    if (!url.startsWith(friendlyURLPrivateGroupPath)
        && !url.startsWith(friendlyURLPrivateUserPath)
        && !url.startsWith(friendlyURLPublicPath)) {

      return;
    }

    int x = url.indexOf(CharPool.SLASH, 1);

    if (x == -1) {
      return;
    }

    int y = url.indexOf(CharPool.SLASH, x + 1);

    if (y == -1) {
      return;
    }

    String friendlyURL = url.substring(x, y);

    if (!friendlyURL.equals(LayoutExporter.SAME_GROUP_FRIENDLY_URL)) {
      return;
    }

    Group group = layout.getGroup();

    typeSettings.setProperty(
        "url", url.substring(0, x) + group.getFriendlyURL() + url.substring(y));
  }
  protected void addLayout(Class<?> clazz, Layout layout) throws Exception {
    List<Layout> layouts = _layouts.get(clazz.getSimpleName());

    if (layouts == null) {
      layouts = new ArrayList<>();

      _layouts.put(clazz.getSimpleName(), layouts);
    }

    layouts.add(layout);

    UnicodeProperties typeSettings = layout.getTypeSettingsProperties();

    typeSettings.setProperty(
        LayoutSetPrototypeStagedModelDataHandlerTest.class.getName(), Boolean.TRUE.toString());

    LayoutLocalServiceUtil.updateLayout(layout);
  }
  public void applyAction(
      MDRAction mdrAction, HttpServletRequest request, HttpServletResponse response) {

    UnicodeProperties mdrActionTypeSettingsProperties = mdrAction.getTypeSettingsProperties();

    String layoutTemplateId =
        GetterUtil.getString(mdrActionTypeSettingsProperties.getProperty("layoutTemplateId"));

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

    Layout layout = themeDisplay.getLayout();

    UnicodeProperties layoutTypeSettingsProperties = layout.getTypeSettingsProperties();

    layoutTypeSettingsProperties.setProperty(
        LayoutTypePortletConstants.LAYOUT_TEMPLATE_ID, layoutTemplateId);

    layout.setTypeSettingsProperties(layoutTypeSettingsProperties);
  }
  protected void updateRobots(ActionRequest actionRequest, long liveGroupId, boolean privateLayout)
      throws Exception {

    Group liveGroup = groupLocalService.getGroup(liveGroupId);

    UnicodeProperties typeSettingsProperties = liveGroup.getTypeSettingsProperties();

    String propertyName = "false-robots.txt";

    if (privateLayout) {
      propertyName = "true-robots.txt";
    }

    String robots =
        ParamUtil.getString(
            actionRequest, "robots", liveGroup.getTypeSettingsProperty(propertyName));

    typeSettingsProperties.setProperty(propertyName, robots);

    groupService.updateGroup(liveGroup.getGroupId(), typeSettingsProperties.toString());
  }
  protected void validateLayout(Element actionElement, MDRAction action) {
    String type = action.getType();

    if (!type.equals(SiteRedirectActionHandler.class.getName())) {
      return;
    }

    String layoutUuid = actionElement.attributeValue("layout-uuid");

    if (Validator.isNull(layoutUuid)) {
      return;
    }

    UnicodeProperties typeSettingsProperties = action.getTypeSettingsProperties();

    long groupId = GetterUtil.getLong(typeSettingsProperties.getProperty("groupId"));
    boolean privateLayout = GetterUtil.getBoolean(actionElement.attributeValue("private-layout"));

    try {
      Layout layout =
          _layoutLocalService.getLayoutByUuidAndGroupId(layoutUuid, groupId, privateLayout);

      typeSettingsProperties.setProperty("plid", String.valueOf(layout.getPlid()));
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        StringBundler sb = new StringBundler(5);

        sb.append("Unable to find layout with uuid ");
        sb.append(layoutUuid);
        sb.append(" in group ");
        sb.append(groupId);
        sb.append(". Site redirect may not match the target layout.");

        _log.warn(sb.toString(), e);
      }
    }
  }
  protected void importLinkedLayout(
      PortletDataContext portletDataContext,
      Layout layout,
      Layout importedLayout,
      Element layoutElement,
      Map<Long, Layout> layouts)
      throws Exception {

    UnicodeProperties typeSettingsProperties = layout.getTypeSettingsProperties();

    long linkToLayoutId =
        GetterUtil.getLong(typeSettingsProperties.getProperty("linkToLayoutId", StringPool.BLANK));

    String linkedToLayoutUuid = layoutElement.attributeValue("linked-to-layout-uuid");

    if (Validator.isNull(linkedToLayoutUuid)) {
      return;
    }

    if (linkToLayoutId <= 0) {
      updateTypeSettings(importedLayout, layout);

      return;
    }

    Element linkedToLayoutElement =
        portletDataContext.getReferenceDataElement(
            layout, Layout.class, layout.getGroupId(), linkedToLayoutUuid);

    if (linkedToLayoutElement != null) {
      String linkedToLayoutPath = linkedToLayoutElement.attributeValue("path");

      Layout linkedToLayout = (Layout) portletDataContext.getZipEntryAsObject(linkedToLayoutPath);

      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, linkedToLayout);

      Layout importedLinkedLayout = layouts.get(linkToLayoutId);

      if (importedLinkedLayout == null) {
        throw new NoSuchLayoutException(
            "Layout with layout id "
                + linkToLayoutId
                + " that is linked from layout with layout id "
                + layout.getLayoutId()
                + " does not exist");
      }

      typeSettingsProperties.setProperty(
          "privateLayout", String.valueOf(importedLinkedLayout.isPrivateLayout()));
      typeSettingsProperties.setProperty(
          "linkToLayoutId", String.valueOf(importedLinkedLayout.getLayoutId()));
    } else {
      if (_log.isWarnEnabled()) {
        StringBundler sb = new StringBundler(6);

        sb.append("Unable to link layout with friendly URL ");
        sb.append(layout.getFriendlyURL());
        sb.append(" and layout id ");
        sb.append(layout.getLayoutId());
        sb.append(" to layout with layout id ");
        sb.append(linkToLayoutId);

        _log.warn(sb.toString());
      }
    }

    updateTypeSettings(importedLayout, layout);
  }
  protected Object[] updateGroup(ActionRequest actionRequest) throws Exception {

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

    long userId = PortalUtil.getUserId(actionRequest);

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

    long parentGroupId =
        ParamUtil.getLong(
            actionRequest,
            "parentGroupSearchContainerPrimaryKeys",
            GroupConstants.DEFAULT_PARENT_GROUP_ID);
    String name = null;
    String description = null;
    int type = 0;
    String friendlyURL = null;
    boolean active = false;

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

    Group liveGroup = null;
    String oldFriendlyURL = null;
    String oldStagingFriendlyURL = null;

    if (liveGroupId <= 0) {

      // Add group

      name = ParamUtil.getString(actionRequest, "name");
      description = ParamUtil.getString(actionRequest, "description");
      type = ParamUtil.getInteger(actionRequest, "type");
      friendlyURL = ParamUtil.getString(actionRequest, "friendlyURL");
      active = ParamUtil.getBoolean(actionRequest, "active");

      liveGroup =
          GroupServiceUtil.addGroup(
              parentGroupId,
              GroupConstants.DEFAULT_LIVE_GROUP_ID,
              name,
              description,
              type,
              friendlyURL,
              true,
              active,
              serviceContext);

      LiveUsers.joinGroup(themeDisplay.getCompanyId(), liveGroup.getGroupId(), userId);
    } else {

      // Update group

      liveGroup = GroupLocalServiceUtil.getGroup(liveGroupId);

      oldFriendlyURL = liveGroup.getFriendlyURL();

      name = ParamUtil.getString(actionRequest, "name", liveGroup.getName());
      description = ParamUtil.getString(actionRequest, "description", liveGroup.getDescription());
      type = ParamUtil.getInteger(actionRequest, "type", liveGroup.getType());
      friendlyURL = ParamUtil.getString(actionRequest, "friendlyURL", liveGroup.getFriendlyURL());
      active = ParamUtil.getBoolean(actionRequest, "active", liveGroup.getActive());

      liveGroup =
          GroupServiceUtil.updateGroup(
              liveGroupId,
              parentGroupId,
              name,
              description,
              type,
              friendlyURL,
              active,
              serviceContext);

      if (type == GroupConstants.TYPE_SITE_OPEN) {
        List<MembershipRequest> membershipRequests =
            MembershipRequestLocalServiceUtil.search(
                liveGroupId,
                MembershipRequestConstants.STATUS_PENDING,
                QueryUtil.ALL_POS,
                QueryUtil.ALL_POS);

        for (MembershipRequest membershipRequest : membershipRequests) {
          MembershipRequestServiceUtil.updateStatus(
              membershipRequest.getMembershipRequestId(),
              themeDisplay.translate("your-membership-has-been-approved"),
              MembershipRequestConstants.STATUS_APPROVED,
              serviceContext);

          LiveUsers.joinGroup(
              themeDisplay.getCompanyId(),
              membershipRequest.getGroupId(),
              new long[] {membershipRequest.getUserId()});
        }
      }
    }

    // Settings

    UnicodeProperties typeSettingsProperties = liveGroup.getTypeSettingsProperties();

    String customJspServletContextName =
        ParamUtil.getString(
            actionRequest,
            "customJspServletContextName",
            typeSettingsProperties.getProperty("customJspServletContextName"));

    typeSettingsProperties.setProperty("customJspServletContextName", customJspServletContextName);

    typeSettingsProperties.setProperty(
        "defaultSiteRoleIds",
        ListUtil.toString(getRoles(actionRequest), Role.ROLE_ID_ACCESSOR, StringPool.COMMA));
    typeSettingsProperties.setProperty(
        "defaultTeamIds",
        ListUtil.toString(getTeams(actionRequest), Team.TEAM_ID_ACCESSOR, StringPool.COMMA));

    String[] analyticsTypes =
        PrefsPropsUtil.getStringArray(
            themeDisplay.getCompanyId(), PropsKeys.ADMIN_ANALYTICS_TYPES, StringPool.NEW_LINE);

    for (String analyticsType : analyticsTypes) {
      if (analyticsType.equalsIgnoreCase("google")) {
        String googleAnalyticsId =
            ParamUtil.getString(
                actionRequest,
                "googleAnalyticsId",
                typeSettingsProperties.getProperty("googleAnalyticsId"));

        typeSettingsProperties.setProperty("googleAnalyticsId", googleAnalyticsId);
      } else {
        String analyticsScript =
            ParamUtil.getString(
                actionRequest,
                SitesUtil.ANALYTICS_PREFIX + analyticsType,
                typeSettingsProperties.getProperty(analyticsType));

        typeSettingsProperties.setProperty(
            SitesUtil.ANALYTICS_PREFIX + analyticsType, analyticsScript);
      }
    }

    String publicRobots =
        ParamUtil.getString(
            actionRequest, "publicRobots", liveGroup.getTypeSettingsProperty("false-robots.txt"));
    String privateRobots =
        ParamUtil.getString(
            actionRequest, "privateRobots", liveGroup.getTypeSettingsProperty("true-robots.txt"));

    typeSettingsProperties.setProperty("false-robots.txt", publicRobots);
    typeSettingsProperties.setProperty("true-robots.txt", privateRobots);

    int trashEnabled =
        ParamUtil.getInteger(
            actionRequest,
            "trashEnabled",
            GetterUtil.getInteger(typeSettingsProperties.getProperty("trashEnabled")));

    typeSettingsProperties.setProperty("trashEnabled", String.valueOf(trashEnabled));

    int trashEntriesMaxAgeCompany =
        PrefsPropsUtil.getInteger(themeDisplay.getCompanyId(), PropsKeys.TRASH_ENTRIES_MAX_AGE);

    int defaultTrashEntriesMaxAgeGroup =
        GetterUtil.getInteger(
            typeSettingsProperties.getProperty("trashEntriesMaxAge"), trashEntriesMaxAgeCompany);

    int trashEntriesMaxAgeGroup =
        ParamUtil.getInteger(actionRequest, "trashEntriesMaxAge", defaultTrashEntriesMaxAgeGroup);

    if (trashEntriesMaxAgeGroup != trashEntriesMaxAgeCompany) {
      typeSettingsProperties.setProperty(
          "trashEntriesMaxAge", String.valueOf(trashEntriesMaxAgeGroup));
    } else {
      typeSettingsProperties.remove("trashEntriesMaxAge");
    }

    // Virtual hosts

    LayoutSet publicLayoutSet = liveGroup.getPublicLayoutSet();

    String publicVirtualHost =
        ParamUtil.getString(
            actionRequest, "publicVirtualHost", publicLayoutSet.getVirtualHostname());

    LayoutSetServiceUtil.updateVirtualHost(liveGroup.getGroupId(), false, publicVirtualHost);

    LayoutSet privateLayoutSet = liveGroup.getPrivateLayoutSet();

    String privateVirtualHost =
        ParamUtil.getString(
            actionRequest, "privateVirtualHost", privateLayoutSet.getVirtualHostname());

    LayoutSetServiceUtil.updateVirtualHost(liveGroup.getGroupId(), true, privateVirtualHost);

    // Staging

    if (liveGroup.hasStagingGroup()) {
      Group stagingGroup = liveGroup.getStagingGroup();

      oldStagingFriendlyURL = stagingGroup.getFriendlyURL();

      friendlyURL =
          ParamUtil.getString(actionRequest, "stagingFriendlyURL", stagingGroup.getFriendlyURL());

      GroupServiceUtil.updateFriendlyURL(stagingGroup.getGroupId(), friendlyURL);

      LayoutSet stagingPublicLayoutSet = stagingGroup.getPublicLayoutSet();

      publicVirtualHost =
          ParamUtil.getString(
              actionRequest,
              "stagingPublicVirtualHost",
              stagingPublicLayoutSet.getVirtualHostname());

      LayoutSetServiceUtil.updateVirtualHost(stagingGroup.getGroupId(), false, publicVirtualHost);

      LayoutSet stagingPrivateLayoutSet = stagingGroup.getPrivateLayoutSet();

      privateVirtualHost =
          ParamUtil.getString(
              actionRequest,
              "stagingPrivateVirtualHost",
              stagingPrivateLayoutSet.getVirtualHostname());

      LayoutSetServiceUtil.updateVirtualHost(stagingGroup.getGroupId(), true, privateVirtualHost);
    }

    liveGroup =
        GroupServiceUtil.updateGroup(liveGroup.getGroupId(), typeSettingsProperties.toString());

    // Layout set prototypes

    if (!liveGroup.isStaged()) {
      long privateLayoutSetPrototypeId =
          ParamUtil.getLong(actionRequest, "privateLayoutSetPrototypeId");
      long publicLayoutSetPrototypeId =
          ParamUtil.getLong(actionRequest, "publicLayoutSetPrototypeId");

      boolean privateLayoutSetPrototypeLinkEnabled =
          ParamUtil.getBoolean(
              actionRequest,
              "privateLayoutSetPrototypeLinkEnabled",
              privateLayoutSet.isLayoutSetPrototypeLinkEnabled());
      boolean publicLayoutSetPrototypeLinkEnabled =
          ParamUtil.getBoolean(
              actionRequest,
              "publicLayoutSetPrototypeLinkEnabled",
              publicLayoutSet.isLayoutSetPrototypeLinkEnabled());

      if ((privateLayoutSetPrototypeId == 0)
          && (publicLayoutSetPrototypeId == 0)
          && !privateLayoutSetPrototypeLinkEnabled
          && !publicLayoutSetPrototypeLinkEnabled) {

        long layoutSetPrototypeId = ParamUtil.getLong(actionRequest, "layoutSetPrototypeId");
        int layoutSetVisibility = ParamUtil.getInteger(actionRequest, "layoutSetVisibility");
        boolean layoutSetPrototypeLinkEnabled =
            ParamUtil.getBoolean(
                actionRequest, "layoutSetPrototypeLinkEnabled", (layoutSetPrototypeId > 0));

        if (layoutSetVisibility == _LAYOUT_SET_VISIBILITY_PRIVATE) {
          privateLayoutSetPrototypeId = layoutSetPrototypeId;

          privateLayoutSetPrototypeLinkEnabled = layoutSetPrototypeLinkEnabled;
        } else {
          publicLayoutSetPrototypeId = layoutSetPrototypeId;

          publicLayoutSetPrototypeLinkEnabled = layoutSetPrototypeLinkEnabled;
        }
      }

      SitesUtil.updateLayoutSetPrototypesLinks(
          liveGroup,
          publicLayoutSetPrototypeId,
          privateLayoutSetPrototypeId,
          publicLayoutSetPrototypeLinkEnabled,
          privateLayoutSetPrototypeLinkEnabled);
    }

    // Staging

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

    long refererPlid = GetterUtil.getLong(HttpUtil.getParameter(redirect, "refererPlid", false));

    if (!privateLayoutSet.isLayoutSetPrototypeLinkActive()
        && !publicLayoutSet.isLayoutSetPrototypeLinkActive()) {

      if ((refererPlid > 0)
          && liveGroup.hasStagingGroup()
          && (themeDisplay.getScopeGroupId() != liveGroup.getGroupId())) {

        Layout firstLayout =
            LayoutLocalServiceUtil.fetchFirstLayout(
                liveGroup.getGroupId(), false, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);

        if (firstLayout == null) {
          firstLayout =
              LayoutLocalServiceUtil.fetchFirstLayout(
                  liveGroup.getGroupId(), true, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
        }

        if (firstLayout != null) {
          refererPlid = firstLayout.getPlid();
        } else {
          refererPlid = 0;
        }
      }

      StagingUtil.updateStaging(actionRequest, liveGroup);
    }

    return new Object[] {liveGroup, oldFriendlyURL, oldStagingFriendlyURL, refererPlid};
  }
  protected void doImportLayouts(
      long userId,
      long groupId,
      boolean privateLayout,
      Map<String, String[]> parameterMap,
      File file)
      throws Exception {

    boolean deleteMissingLayouts =
        MapUtil.getBoolean(
            parameterMap,
            PortletDataHandlerKeys.DELETE_MISSING_LAYOUTS,
            Boolean.TRUE.booleanValue());
    boolean deletePortletData =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.DELETE_PORTLET_DATA);
    boolean importCategories = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.CATEGORIES);
    boolean importPermissions =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PERMISSIONS);
    boolean importPublicLayoutPermissions =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PUBLIC_LAYOUT_PERMISSIONS);
    boolean importUserPermissions =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.USER_PERMISSIONS);
    boolean importPortletData =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PORTLET_DATA);
    boolean importPortletSetup =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PORTLET_SETUP);
    boolean importPortletArchivedSetups =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PORTLET_ARCHIVED_SETUPS);
    boolean importPortletUserPreferences =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PORTLET_USER_PREFERENCES);
    boolean importTheme = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.THEME);
    boolean importThemeSettings =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.THEME_REFERENCE);
    boolean importLogo = MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.LOGO);
    boolean importLayoutSetSettings =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.LAYOUT_SET_SETTINGS);

    boolean layoutSetPrototypeLinkEnabled =
        MapUtil.getBoolean(
            parameterMap, PortletDataHandlerKeys.LAYOUT_SET_PROTOTYPE_LINK_ENABLED, true);

    Group group = GroupLocalServiceUtil.getGroup(groupId);

    if (group.isLayoutSetPrototype()) {
      layoutSetPrototypeLinkEnabled = false;
    }

    boolean publishToRemote =
        MapUtil.getBoolean(parameterMap, PortletDataHandlerKeys.PUBLISH_TO_REMOTE);
    String layoutsImportMode =
        MapUtil.getString(
            parameterMap,
            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE,
            PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_UUID);
    String portletsMergeMode =
        MapUtil.getString(
            parameterMap,
            PortletDataHandlerKeys.PORTLETS_MERGE_MODE,
            PortletDataHandlerKeys.PORTLETS_MERGE_MODE_REPLACE);
    String userIdStrategy =
        MapUtil.getString(parameterMap, PortletDataHandlerKeys.USER_ID_STRATEGY);

    if (_log.isDebugEnabled()) {
      _log.debug("Delete portlet data " + deletePortletData);
      _log.debug("Import categories " + importCategories);
      _log.debug("Import permissions " + importPermissions);
      _log.debug("Import user permissions " + importUserPermissions);
      _log.debug("Import portlet data " + importPortletData);
      _log.debug("Import portlet setup " + importPortletSetup);
      _log.debug("Import portlet archived setups " + importPortletArchivedSetups);
      _log.debug("Import portlet user preferences " + importPortletUserPreferences);
      _log.debug("Import theme " + importTheme);
    }

    StopWatch stopWatch = null;

    if (_log.isInfoEnabled()) {
      stopWatch = new StopWatch();

      stopWatch.start();
    }

    LayoutCache layoutCache = new LayoutCache();

    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(groupId, privateLayout);

    long companyId = layoutSet.getCompanyId();

    User user = UserUtil.findByPrimaryKey(userId);

    UserIdStrategy strategy = _portletImporter.getUserIdStrategy(user, userIdStrategy);

    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(file);

    PortletDataContext portletDataContext =
        new PortletDataContextImpl(
            companyId, groupId, parameterMap, new HashSet<String>(), strategy, zipReader);

    portletDataContext.setPortetDataContextListener(
        new PortletDataContextListenerImpl(portletDataContext));

    portletDataContext.setPrivateLayout(privateLayout);

    // Zip

    Element rootElement = null;
    InputStream themeZip = null;

    // Manifest

    String xml = portletDataContext.getZipEntryAsString("/manifest.xml");

    if (xml == null) {
      throw new LARFileException("manifest.xml not found in the LAR");
    }

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

      rootElement = document.getRootElement();
    } catch (Exception e) {
      throw new LARFileException(e);
    }

    // Build compatibility

    Element headerElement = rootElement.element("header");

    int buildNumber = ReleaseInfo.getBuildNumber();

    int importBuildNumber = GetterUtil.getInteger(headerElement.attributeValue("build-number"));

    if (buildNumber != importBuildNumber) {
      throw new LayoutImportException(
          "LAR build number "
              + importBuildNumber
              + " does not match "
              + "portal build number "
              + buildNumber);
    }

    // Type compatibility

    String larType = headerElement.attributeValue("type");

    if (!larType.equals("layout-set") && !larType.equals("layout-set-prototype")) {

      throw new LARTypeException("Invalid type of LAR file (" + larType + ")");
    }

    // Group id

    long sourceGroupId = GetterUtil.getLong(headerElement.attributeValue("group-id"));

    portletDataContext.setSourceGroupId(sourceGroupId);

    // Layout set prototype

    if (group.isLayoutSetPrototype() && larType.equals("layout-set-prototype")) {

      LayoutSetPrototype layoutSetPrototype =
          LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototype(group.getClassPK());

      String layoutSetPrototypeUuid =
          GetterUtil.getString(headerElement.attributeValue("type-uuid"));

      LayoutSetPrototype existingLayoutSetPrototype = null;

      if (Validator.isNotNull(layoutSetPrototypeUuid)) {
        try {
          existingLayoutSetPrototype =
              LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototypeByUuid(
                  layoutSetPrototypeUuid);
        } catch (NoSuchLayoutSetPrototypeException nslspe) {
        }
      }

      if (existingLayoutSetPrototype == null) {
        layoutSetPrototype.setUuid(layoutSetPrototypeUuid);

        LayoutSetPrototypeLocalServiceUtil.updateLayoutSetPrototype(layoutSetPrototype);
      }
    }

    Element layoutsElement = rootElement.element("layouts");

    String layoutSetPrototypeUuid = layoutsElement.attributeValue("layout-set-prototype-uuid");

    ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();

    if (Validator.isNotNull(layoutSetPrototypeUuid)) {
      if (layoutSetPrototypeLinkEnabled) {
        if (publishToRemote) {
          importLayoutSetPrototype(
              portletDataContext, user, layoutSetPrototypeUuid, serviceContext);
        }
      }

      layoutSet.setLayoutSetPrototypeUuid(layoutSetPrototypeUuid);
      layoutSet.setLayoutSetPrototypeLinkEnabled(layoutSetPrototypeLinkEnabled);

      LayoutSetLocalServiceUtil.updateLayoutSet(layoutSet);
    }

    // Look and feel

    if (importTheme) {
      themeZip = portletDataContext.getZipEntryAsInputStream("theme.zip");
    }

    // Look and feel

    String themeId = layoutSet.getThemeId();
    String colorSchemeId = layoutSet.getColorSchemeId();

    if (importThemeSettings) {
      Attribute themeIdAttribute = headerElement.attribute("theme-id");

      if (themeIdAttribute != null) {
        themeId = themeIdAttribute.getValue();
      }

      Attribute colorSchemeIdAttribute = headerElement.attribute("color-scheme-id");

      if (colorSchemeIdAttribute != null) {
        colorSchemeId = colorSchemeIdAttribute.getValue();
      }
    }

    if (importLogo) {
      String logoPath = headerElement.attributeValue("logo-path");

      byte[] iconBytes = portletDataContext.getZipEntryAsByteArray(logoPath);

      if ((iconBytes != null) && (iconBytes.length > 0)) {
        File logo = FileUtil.createTempFile(iconBytes);

        LayoutSetLocalServiceUtil.updateLogo(groupId, privateLayout, true, logo);
      } else {
        LayoutSetLocalServiceUtil.updateLogo(groupId, privateLayout, false, (File) null);
      }
    }

    if (importLayoutSetSettings) {
      String settings = GetterUtil.getString(headerElement.elementText("settings"));

      LayoutSetLocalServiceUtil.updateSettings(groupId, privateLayout, settings);
    }

    String css = GetterUtil.getString(headerElement.elementText("css"));

    if (themeZip != null) {
      String importThemeId = importTheme(layoutSet, themeZip);

      if (importThemeId != null) {
        themeId = importThemeId;
        colorSchemeId = ColorSchemeImpl.getDefaultRegularColorSchemeId();
      }

      if (_log.isDebugEnabled()) {
        _log.debug("Importing theme takes " + stopWatch.getTime() + " ms");
      }
    }

    boolean wapTheme = false;

    LayoutSetLocalServiceUtil.updateLookAndFeel(
        groupId, privateLayout, themeId, colorSchemeId, css, wapTheme);

    // Read asset categories, asset tags, comments, locks, permissions, and
    // ratings entries to make them available to the data handlers through
    // the context

    if (importPermissions) {
      _permissionImporter.readPortletDataPermissions(portletDataContext);
    }

    if (importCategories) {
      _portletImporter.readAssetCategories(portletDataContext);
    }

    _portletImporter.readAssetTags(portletDataContext);
    _portletImporter.readComments(portletDataContext);
    _portletImporter.readExpandoTables(portletDataContext);
    _portletImporter.readLocks(portletDataContext);
    _portletImporter.readRatingsEntries(portletDataContext);

    // Layouts

    List<Layout> previousLayouts = LayoutUtil.findByG_P(groupId, privateLayout);

    // Remove layouts that were deleted from the layout set prototype

    if (Validator.isNotNull(layoutSetPrototypeUuid) && layoutSetPrototypeLinkEnabled) {

      LayoutSetPrototype layoutSetPrototype =
          LayoutSetPrototypeLocalServiceUtil.getLayoutSetPrototypeByUuid(layoutSetPrototypeUuid);

      Group layoutSetPrototypeGroup = layoutSetPrototype.getGroup();

      for (Layout layout : previousLayouts) {
        String sourcePrototypeLayoutUuid = layout.getSourcePrototypeLayoutUuid();

        if (Validator.isNull(layout.getSourcePrototypeLayoutUuid())) {
          continue;
        }

        Layout sourcePrototypeLayout =
            LayoutUtil.fetchByUUID_G(
                sourcePrototypeLayoutUuid, layoutSetPrototypeGroup.getGroupId());

        if (sourcePrototypeLayout == null) {
          LayoutLocalServiceUtil.deleteLayout(layout, false, serviceContext);
        }
      }
    }

    List<Layout> newLayouts = new ArrayList<Layout>();

    Set<Long> newLayoutIds = new HashSet<Long>();

    Map<Long, Layout> newLayoutsMap =
        (Map<Long, Layout>) portletDataContext.getNewPrimaryKeysMap(Layout.class);

    List<Element> layoutElements = layoutsElement.elements("layout");

    if (_log.isDebugEnabled()) {
      if (layoutElements.size() > 0) {
        _log.debug("Importing layouts");
      }
    }

    for (Element layoutElement : layoutElements) {
      importLayout(
          portletDataContext,
          user,
          layoutCache,
          previousLayouts,
          newLayouts,
          newLayoutsMap,
          newLayoutIds,
          portletsMergeMode,
          themeId,
          colorSchemeId,
          layoutsImportMode,
          privateLayout,
          importPermissions,
          importPublicLayoutPermissions,
          importUserPermissions,
          importThemeSettings,
          rootElement,
          layoutElement);
    }

    Element portletsElement = rootElement.element("portlets");

    List<Element> portletElements = portletsElement.elements("portlet");

    // Delete portlet data

    if (deletePortletData) {
      if (_log.isDebugEnabled()) {
        if (portletElements.size() > 0) {
          _log.debug("Deleting portlet data");
        }
      }

      for (Element portletElement : portletElements) {
        String portletId = portletElement.attributeValue("portlet-id");
        long layoutId = GetterUtil.getLong(portletElement.attributeValue("layout-id"));
        long plid = newLayoutsMap.get(layoutId).getPlid();

        portletDataContext.setPlid(plid);

        _portletImporter.deletePortletData(portletDataContext, portletId, plid);
      }
    }

    // Import portlets

    if (_log.isDebugEnabled()) {
      if (portletElements.size() > 0) {
        _log.debug("Importing portlets");
      }
    }

    for (Element portletElement : portletElements) {
      String portletPath = portletElement.attributeValue("path");
      String portletId = portletElement.attributeValue("portlet-id");
      long layoutId = GetterUtil.getLong(portletElement.attributeValue("layout-id"));
      long plid = newLayoutsMap.get(layoutId).getPlid();
      long oldPlid = GetterUtil.getLong(portletElement.attributeValue("old-plid"));

      Portlet portlet =
          PortletLocalServiceUtil.getPortletById(portletDataContext.getCompanyId(), portletId);

      if (!portlet.isActive() || portlet.isUndeployedPortlet()) {
        continue;
      }

      Layout layout = null;

      try {
        layout = LayoutUtil.findByPrimaryKey(plid);
      } catch (NoSuchLayoutException nsle) {
        continue;
      }

      portletDataContext.setPlid(plid);
      portletDataContext.setOldPlid(oldPlid);

      Document portletDocument =
          SAXReaderUtil.read(portletDataContext.getZipEntryAsString(portletPath));

      portletElement = portletDocument.getRootElement();

      // The order of the import is important. You must always import
      // the portlet preferences first, then the portlet data, then
      // the portlet permissions. The import of the portlet data
      // assumes that portlet preferences already exist.

      _portletImporter.setPortletScope(portletDataContext, portletElement);

      try {

        // Portlet preferences

        _portletImporter.importPortletPreferences(
            portletDataContext,
            layoutSet.getCompanyId(),
            layout.getGroupId(),
            layout,
            null,
            portletElement,
            importPortletSetup,
            importPortletArchivedSetups,
            importPortletUserPreferences,
            false);

        // Portlet data

        Element portletDataElement = portletElement.element("portlet-data");

        if (importPortletData && (portletDataElement != null)) {
          _portletImporter.importPortletData(
              portletDataContext, portletId, plid, portletDataElement);
        }
      } finally {
        _portletImporter.resetPortletScope(portletDataContext, layout.getGroupId());
      }

      // Portlet permissions

      if (importPermissions) {
        _permissionImporter.importPortletPermissions(
            layoutCache,
            companyId,
            groupId,
            userId,
            layout,
            portletElement,
            portletId,
            importUserPermissions);
      }

      // Archived setups

      _portletImporter.importPortletPreferences(
          portletDataContext,
          layoutSet.getCompanyId(),
          groupId,
          null,
          null,
          portletElement,
          importPortletSetup,
          importPortletArchivedSetups,
          importPortletUserPreferences,
          false);
    }

    if (importPermissions) {
      if ((userId > 0)
          && ((PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5)
              || (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6))) {

        Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(User.class);

        indexer.reindex(userId);
      }
    }

    // Asset links

    _portletImporter.readAssetLinks(portletDataContext);

    // Delete missing layouts

    if (deleteMissingLayouts) {
      deleteMissingLayouts(groupId, privateLayout, newLayoutIds, previousLayouts, serviceContext);
    }

    // Page count

    LayoutSetLocalServiceUtil.updatePageCount(groupId, privateLayout);

    if (_log.isInfoEnabled()) {
      _log.info("Importing layouts takes " + stopWatch.getTime() + " ms");
    }

    // Site

    GroupLocalServiceUtil.updateSite(groupId, true);

    // Web content layout type

    for (Layout layout : newLayouts) {
      UnicodeProperties typeSettingsProperties = layout.getTypeSettingsProperties();

      String articleId = typeSettingsProperties.getProperty("article-id");

      if (Validator.isNotNull(articleId)) {
        Map<String, String> articleIds =
            (Map<String, String>)
                portletDataContext.getNewPrimaryKeysMap(JournalArticle.class + ".articleId");

        typeSettingsProperties.setProperty(
            "article-id", MapUtil.getString(articleIds, articleId, articleId));

        LayoutUtil.update(layout, false);
      }
    }

    zipReader.close();
  }
  protected void importLayout(
      PortletDataContext portletDataContext,
      User user,
      LayoutCache layoutCache,
      List<Layout> previousLayouts,
      List<Layout> newLayouts,
      Map<Long, Layout> newLayoutsMap,
      Set<Long> newLayoutIds,
      String portletsMergeMode,
      String themeId,
      String colorSchemeId,
      String layoutsImportMode,
      boolean privateLayout,
      boolean importPermissions,
      boolean importPublicLayoutPermissions,
      boolean importUserPermissions,
      boolean importThemeSettings,
      Element rootElement,
      Element layoutElement)
      throws Exception {

    long groupId = portletDataContext.getGroupId();

    String layoutUuid = GetterUtil.getString(layoutElement.attributeValue("layout-uuid"));

    long layoutId = GetterUtil.getInteger(layoutElement.attributeValue("layout-id"));

    long oldLayoutId = layoutId;

    boolean deleteLayout = GetterUtil.getBoolean(layoutElement.attributeValue("delete"));

    if (deleteLayout) {
      Layout layout = LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(layoutUuid, groupId);

      if (layout != null) {
        newLayoutsMap.put(oldLayoutId, layout);

        ServiceContext serviceContext = ServiceContextThreadLocal.getServiceContext();

        LayoutLocalServiceUtil.deleteLayout(layout, false, serviceContext);
      }

      return;
    }

    String path = layoutElement.attributeValue("path");

    if (!portletDataContext.isPathNotProcessed(path)) {
      return;
    }

    Layout layout = (Layout) portletDataContext.getZipEntryAsObject(path);

    Layout existingLayout = null;
    Layout importedLayout = null;

    String friendlyURL = layout.getFriendlyURL();

    if (layoutsImportMode.equals(PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_ADD_AS_NEW)) {

      layoutId = LayoutLocalServiceUtil.getNextLayoutId(groupId, privateLayout);
      friendlyURL = StringPool.SLASH + layoutId;
    } else if (layoutsImportMode.equals(
        PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_NAME)) {

      Locale locale = LocaleUtil.getDefault();

      String localizedName = layout.getName(locale);

      for (Layout curLayout : previousLayouts) {
        if (localizedName.equals(curLayout.getName(locale))
            || friendlyURL.equals(curLayout.getFriendlyURL())) {

          existingLayout = curLayout;

          break;
        }
      }

      if (existingLayout == null) {
        layoutId = LayoutLocalServiceUtil.getNextLayoutId(groupId, privateLayout);
      }
    } else if (layoutsImportMode.equals(
        PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE)) {

      existingLayout = LayoutUtil.fetchByG_P_SPLU(groupId, privateLayout, layout.getUuid());

      if (SitesUtil.isLayoutModifiedSinceLastMerge(existingLayout)) {
        newLayoutsMap.put(oldLayoutId, existingLayout);

        return;
      }
    } else {

      // The default behaviour of import mode is
      // PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_MERGE_BY_LAYOUT_UUID

      existingLayout = LayoutUtil.fetchByUUID_G(layout.getUuid(), groupId);

      if (existingLayout == null) {
        existingLayout = LayoutUtil.fetchByG_P_F(groupId, privateLayout, friendlyURL);
      }

      if (existingLayout == null) {
        layoutId = LayoutLocalServiceUtil.getNextLayoutId(groupId, privateLayout);
      }
    }

    if (_log.isDebugEnabled()) {
      if (existingLayout == null) {
        _log.debug(
            "Layout with {groupId="
                + groupId
                + ",privateLayout="
                + privateLayout
                + ",layoutId="
                + layoutId
                + "} does not exist");
      } else {
        _log.debug(
            "Layout with {groupId="
                + groupId
                + ",privateLayout="
                + privateLayout
                + ",layoutId="
                + layoutId
                + "} exists");
      }
    }

    if (existingLayout == null) {
      long plid = CounterLocalServiceUtil.increment();

      importedLayout = LayoutUtil.create(plid);

      if (layoutsImportMode.equals(
          PortletDataHandlerKeys.LAYOUTS_IMPORT_MODE_CREATED_FROM_PROTOTYPE)) {

        importedLayout.setSourcePrototypeLayoutUuid(layout.getUuid());

        layoutId = LayoutLocalServiceUtil.getNextLayoutId(groupId, privateLayout);
      } else {
        importedLayout.setUuid(layout.getUuid());
        importedLayout.setCreateDate(layout.getCreateDate());
        importedLayout.setModifiedDate(layout.getModifiedDate());
        importedLayout.setLayoutPrototypeUuid(layout.getLayoutPrototypeUuid());
        importedLayout.setLayoutPrototypeLinkEnabled(layout.isLayoutPrototypeLinkEnabled());
        importedLayout.setSourcePrototypeLayoutUuid(layout.getSourcePrototypeLayoutUuid());
      }

      importedLayout.setGroupId(groupId);
      importedLayout.setPrivateLayout(privateLayout);
      importedLayout.setLayoutId(layoutId);

      // Resources

      boolean addGroupPermissions = true;

      Group group = importedLayout.getGroup();

      if (privateLayout && group.isUser()) {
        addGroupPermissions = false;
      }

      boolean addGuestPermissions = false;

      if (!privateLayout || layout.isTypeControlPanel()) {
        addGuestPermissions = true;
      }

      ResourceLocalServiceUtil.addResources(
          user.getCompanyId(),
          groupId,
          user.getUserId(),
          Layout.class.getName(),
          importedLayout.getPlid(),
          false,
          addGroupPermissions,
          addGuestPermissions);

      LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(groupId, privateLayout);

      importedLayout.setLayoutSet(layoutSet);
    } else {
      importedLayout = existingLayout;
    }

    newLayoutsMap.put(oldLayoutId, importedLayout);

    long parentLayoutId = layout.getParentLayoutId();

    Node parentLayoutNode =
        rootElement.selectSingleNode("./layouts/layout[@layout-id='" + parentLayoutId + "']");

    String parentLayoutUuid =
        GetterUtil.getString(layoutElement.attributeValue("parent-layout-uuid"));

    if ((parentLayoutId != LayoutConstants.DEFAULT_PARENT_LAYOUT_ID)
        && (parentLayoutNode != null)) {

      importLayout(
          portletDataContext,
          user,
          layoutCache,
          previousLayouts,
          newLayouts,
          newLayoutsMap,
          newLayoutIds,
          portletsMergeMode,
          themeId,
          colorSchemeId,
          layoutsImportMode,
          privateLayout,
          importPermissions,
          importPublicLayoutPermissions,
          importUserPermissions,
          importThemeSettings,
          rootElement,
          (Element) parentLayoutNode);

      Layout parentLayout = newLayoutsMap.get(parentLayoutId);

      parentLayoutId = parentLayout.getLayoutId();
    } else if (Validator.isNotNull(parentLayoutUuid)) {
      Layout parentLayout =
          LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(parentLayoutUuid, groupId);

      parentLayoutId = parentLayout.getLayoutId();
    }

    if (_log.isDebugEnabled()) {
      _log.debug(
          "Importing layout with layout id "
              + layoutId
              + " and parent layout id "
              + parentLayoutId);
    }

    importedLayout.setCompanyId(user.getCompanyId());
    importedLayout.setParentLayoutId(parentLayoutId);
    importedLayout.setName(layout.getName());
    importedLayout.setTitle(layout.getTitle());
    importedLayout.setDescription(layout.getDescription());
    importedLayout.setKeywords(layout.getKeywords());
    importedLayout.setRobots(layout.getRobots());
    importedLayout.setType(layout.getType());

    if (layout.isTypeArticle()) {
      importJournalArticle(portletDataContext, layout, layoutElement);

      importedLayout.setTypeSettings(layout.getTypeSettings());
    } else if (layout.isTypePortlet()
        && Validator.isNotNull(layout.getTypeSettings())
        && !portletsMergeMode.equals(PortletDataHandlerKeys.PORTLETS_MERGE_MODE_REPLACE)) {

      mergePortlets(importedLayout, layout.getTypeSettings(), portletsMergeMode);
    } else if (layout.isTypeLinkToLayout()) {
      UnicodeProperties typeSettingsProperties = layout.getTypeSettingsProperties();

      long linkToLayoutId =
          GetterUtil.getLong(
              typeSettingsProperties.getProperty("linkToLayoutId", StringPool.BLANK));

      if (linkToLayoutId > 0) {
        Node linkedLayoutNode =
            rootElement.selectSingleNode("./layouts/layout[@layout-id='" + linkToLayoutId + "']");

        if (linkedLayoutNode != null) {
          importLayout(
              portletDataContext,
              user,
              layoutCache,
              previousLayouts,
              newLayouts,
              newLayoutsMap,
              newLayoutIds,
              portletsMergeMode,
              themeId,
              colorSchemeId,
              layoutsImportMode,
              privateLayout,
              importPermissions,
              importPublicLayoutPermissions,
              importUserPermissions,
              importThemeSettings,
              rootElement,
              (Element) linkedLayoutNode);

          Layout linkedLayout = newLayoutsMap.get(linkToLayoutId);

          typeSettingsProperties.setProperty(
              "privateLayout", String.valueOf(linkedLayout.getPrivateLayout()));
          typeSettingsProperties.setProperty(
              "linkToLayoutId", String.valueOf(linkedLayout.getLayoutId()));
        } else {
          if (_log.isWarnEnabled()) {
            StringBundler sb = new StringBundler();

            sb.append("Unable to link layout with friendly URL ");
            sb.append(layout.getFriendlyURL());
            sb.append(" and layout id ");
            sb.append(layout.getLayoutId());
            sb.append(" to layout with layout id ");
            sb.append(linkToLayoutId);

            _log.warn(sb.toString());
          }
        }
      }

      importedLayout.setTypeSettings(layout.getTypeSettings());
    } else {
      importedLayout.setTypeSettings(layout.getTypeSettings());
    }

    importedLayout.setHidden(layout.isHidden());
    importedLayout.setFriendlyURL(friendlyURL);

    if (importThemeSettings) {
      importedLayout.setThemeId(layout.getThemeId());
      importedLayout.setColorSchemeId(layout.getColorSchemeId());
    } else {
      importedLayout.setThemeId(StringPool.BLANK);
      importedLayout.setColorSchemeId(StringPool.BLANK);
    }

    importedLayout.setWapThemeId(layout.getWapThemeId());
    importedLayout.setWapColorSchemeId(layout.getWapColorSchemeId());
    importedLayout.setCss(layout.getCss());
    importedLayout.setPriority(layout.getPriority());
    importedLayout.setLayoutPrototypeUuid(layout.getLayoutPrototypeUuid());
    importedLayout.setLayoutPrototypeLinkEnabled(layout.isLayoutPrototypeLinkEnabled());

    StagingUtil.updateLastImportSettings(layoutElement, importedLayout, portletDataContext);

    fixTypeSettings(importedLayout);

    importedLayout.setIconImage(false);

    if (layout.isIconImage()) {
      String iconImagePath = layoutElement.elementText("icon-image-path");

      byte[] iconBytes = portletDataContext.getZipEntryAsByteArray(iconImagePath);

      if ((iconBytes != null) && (iconBytes.length > 0)) {
        importedLayout.setIconImage(true);

        if (importedLayout.getIconImageId() == 0) {
          long iconImageId = CounterLocalServiceUtil.increment();

          importedLayout.setIconImageId(iconImageId);
        }

        ImageLocalServiceUtil.updateImage(importedLayout.getIconImageId(), iconBytes);
      }
    } else {
      ImageLocalServiceUtil.deleteImage(importedLayout.getIconImageId());
    }

    ServiceContext serviceContext =
        portletDataContext.createServiceContext(layoutElement, importedLayout, null);

    importedLayout.setExpandoBridgeAttributes(serviceContext);

    LayoutUtil.update(importedLayout, false);

    portletDataContext.setPlid(importedLayout.getPlid());
    portletDataContext.setOldPlid(layout.getPlid());

    newLayoutIds.add(importedLayout.getLayoutId());

    newLayouts.add(importedLayout);

    // Layout permissions

    if (importPermissions) {
      _permissionImporter.importLayoutPermissions(
          layoutCache,
          portletDataContext.getCompanyId(),
          groupId,
          user.getUserId(),
          importedLayout,
          layoutElement,
          rootElement,
          importUserPermissions);
    }

    if (importPublicLayoutPermissions) {
      String resourceName = Layout.class.getName();
      String resourcePrimKey = String.valueOf(importedLayout.getPlid());

      Role guestRole =
          RoleLocalServiceUtil.getRole(importedLayout.getCompanyId(), RoleConstants.GUEST);

      if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 5) {
        Resource resource =
            layoutCache.getResource(
                importedLayout.getCompanyId(),
                groupId,
                resourceName,
                ResourceConstants.SCOPE_INDIVIDUAL,
                resourcePrimKey,
                false);

        PermissionLocalServiceUtil.setRolePermissions(
            guestRole.getRoleId(), new String[] {ActionKeys.VIEW}, resource.getResourceId());
      } else if (PropsValues.PERMISSIONS_USER_CHECK_ALGORITHM == 6) {
        ResourcePermissionLocalServiceUtil.setResourcePermissions(
            importedLayout.getCompanyId(),
            resourceName,
            ResourceConstants.SCOPE_INDIVIDUAL,
            resourcePrimKey,
            guestRole.getRoleId(),
            new String[] {ActionKeys.VIEW});
      } else {
        Resource resource =
            layoutCache.getResource(
                importedLayout.getCompanyId(),
                groupId,
                resourceName,
                ResourceConstants.SCOPE_INDIVIDUAL,
                resourcePrimKey,
                false);

        PermissionLocalServiceUtil.setGroupPermissions(
            groupId, new String[] {ActionKeys.VIEW}, resource.getResourceId());
      }
    }

    _portletImporter.importPortletData(
        portletDataContext, PortletKeys.LAYOUT_CONFIGURATION, null, layoutElement);
  }