protected String getOldScopeName(ActionRequest actionRequest, Portlet portlet) throws Exception {

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

    Layout layout = themeDisplay.getLayout();

    PortletPreferences portletPreferences = actionRequest.getPreferences();

    String scopeType = GetterUtil.getString(portletPreferences.getValue("lfrScopeType", null));

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

    String scopeName = null;

    if (scopeType.equals("company")) {
      scopeName = themeDisplay.translate("global");
    } else if (scopeType.equals("layout")) {
      String scopeLayoutUuid =
          GetterUtil.getString(portletPreferences.getValue("lfrScopeLayoutUuid", null));

      Layout scopeLayout =
          _layoutLocalService.fetchLayoutByUuidAndGroupId(
              scopeLayoutUuid, layout.getGroupId(), layout.isPrivateLayout());

      if (scopeLayout != null) {
        scopeName = scopeLayout.getName(themeDisplay.getLocale());
      }
    } else {
      throw new IllegalArgumentException("Scope type " + scopeType + " is invalid");
    }

    return scopeName;
  }
  protected PortletDataContext getPortletDataContext(
      ExportImportConfiguration exportImportConfiguration, File file) throws PortalException {

    Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

    Map<String, String[]> parameterMap = (Map<String, String[]>) settingsMap.get("parameterMap");
    String portletId = MapUtil.getString(settingsMap, "portletId");
    long targetPlid = MapUtil.getLong(settingsMap, "targetPlid");
    long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
    long userId = MapUtil.getLong(settingsMap, "userId");

    Layout layout = _layoutLocalService.getLayout(targetPlid);

    String userIdStrategyString =
        MapUtil.getString(parameterMap, PortletDataHandlerKeys.USER_ID_STRATEGY);

    UserIdStrategy userIdStrategy =
        ExportImportHelperUtil.getUserIdStrategy(userId, userIdStrategyString);

    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(file);

    PortletDataContext portletDataContext =
        PortletDataContextFactoryUtil.createImportPortletDataContext(
            layout.getCompanyId(), targetGroupId, parameterMap, userIdStrategy, zipReader);

    portletDataContext.setOldPlid(targetPlid);
    portletDataContext.setPlid(targetPlid);
    portletDataContext.setPortletId(portletId);
    portletDataContext.setPrivateLayout(layout.isPrivateLayout());
    portletDataContext.setType("portlet");

    return portletDataContext;
  }
    protected String toJSON(Layout layout) {
      JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

      jsonObject.put("groupId", layout.getGroupId());
      jsonObject.put("layoutId", layout.getLayoutId());
      jsonObject.put("privateLayout", layout.isPrivateLayout());

      return jsonObject.toString();
    }
  protected Tuple getNewScope(ActionRequest actionRequest) throws Exception {
    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    Layout layout = themeDisplay.getLayout();

    String[] scopes = StringUtil.split(ParamUtil.getString(actionRequest, "scope"));

    String scopeType = scopes[0];

    long scopeGroupId = 0;
    String scopeName = null;

    if (Validator.isNull(scopeType)) {
      scopeGroupId = layout.getGroupId();
    } else if (scopeType.equals("company")) {
      scopeGroupId = themeDisplay.getCompanyGroupId();
      scopeName = themeDisplay.translate("global");
    } else if (scopeType.equals("layout")) {
      String scopeLayoutUuid = scopes[1];

      Layout scopeLayout =
          _layoutLocalService.getLayoutByUuidAndGroupId(
              scopeLayoutUuid, layout.getGroupId(), layout.isPrivateLayout());

      if (!scopeLayout.hasScopeGroup()) {
        Map<Locale, String> nameMap = new HashMap<>();

        String name = String.valueOf(scopeLayout.getPlid());

        nameMap.put(LocaleUtil.getDefault(), name);

        _groupLocalService.addGroup(
            themeDisplay.getUserId(),
            GroupConstants.DEFAULT_PARENT_GROUP_ID,
            Layout.class.getName(),
            scopeLayout.getPlid(),
            GroupConstants.DEFAULT_LIVE_GROUP_ID,
            nameMap,
            null,
            0,
            true,
            GroupConstants.DEFAULT_MEMBERSHIP_RESTRICTION,
            null,
            false,
            true,
            null);
      }

      scopeGroupId = scopeLayout.getGroupId();
      scopeName = scopeLayout.getName(themeDisplay.getLocale());
    } else {
      throw new IllegalArgumentException("Scope type " + scopeType + " is invalid");
    }

    return new Tuple(scopeGroupId, scopeName);
  }
  protected List<NavItem> getNavItems(List<NavItem> branchNavItems) throws Exception {

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

    List<NavItem> navItems = new ArrayList<>();

    NavItem rootNavItem = null;

    if (_rootLayoutType.equals("relative")) {
      if ((_rootLayoutLevel >= 0) && (_rootLayoutLevel < branchNavItems.size())) {

        rootNavItem = branchNavItems.get(_rootLayoutLevel);
      }
    } else if (_rootLayoutType.equals("absolute")) {
      int ancestorIndex = branchNavItems.size() - _rootLayoutLevel;

      if ((ancestorIndex >= 0) && (ancestorIndex < branchNavItems.size())) {

        rootNavItem = branchNavItems.get(ancestorIndex);
      } else if (ancestorIndex == branchNavItems.size()) {
        navItems = NavItem.fromLayouts(request, themeDisplay.getLayouts(), null);
      }
    } else if (_rootLayoutType.equals("select")) {
      Layout layout = themeDisplay.getLayout();

      if (Validator.isNotNull(_rootLayoutUuid)) {
        Layout rootLayout =
            LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
                _rootLayoutUuid, layout.getGroupId(), layout.isPrivateLayout());

        rootNavItem = new NavItem(request, rootLayout, null);
      } else {
        navItems = NavItem.fromLayouts(request, themeDisplay.getLayouts(), null);
      }
    }

    if (rootNavItem != null) {
      navItems = rootNavItem.getChildren();
    }

    return navItems;
  }
  private List<Group> _filterLayoutGroups(List<Group> groups, Boolean privateLayout)
      throws Exception {

    List<Group> filteredGroups = new ArrayList();

    if (privateLayout == null) {
      return groups;
    }

    for (Group group : groups) {
      if (!group.isLayout()) {
        continue;
      }

      Layout layout = LayoutLocalServiceUtil.getLayout(group.getClassPK());

      if (layout.isPrivateLayout() == privateLayout) {
        filteredGroups.add(group);
      }
    }

    return filteredGroups;
  }