protected void copyPreferences(long userId, String sourcePortletId, String targetPortletId) {

    Layout layout = getLayout();

    try {
      PortletPreferencesIds portletPreferencesIds =
          PortletPreferencesFactoryUtil.getPortletPreferencesIds(
              layout.getGroupId(), 0, layout, sourcePortletId, false);

      javax.portlet.PortletPreferences sourcePortletPreferences =
          PortletPreferencesLocalServiceUtil.getStrictPreferences(portletPreferencesIds);

      portletPreferencesIds =
          PortletPreferencesFactoryUtil.getPortletPreferencesIds(
              layout.getGroupId(), userId, layout, targetPortletId, false);

      PortletPreferencesLocalServiceUtil.updatePreferences(
          portletPreferencesIds.getOwnerId(),
          portletPreferencesIds.getOwnerType(),
          portletPreferencesIds.getPlid(),
          portletPreferencesIds.getPortletId(),
          sourcePortletPreferences);
    } catch (Exception e) {
    }
  }
  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 PortletPreferences getPortletPreferences(
      ThemeDisplay themeDisplay, String portletId, String settingsScope) {

    Layout layout = themeDisplay.getLayout();

    if (!layout.isSupportsEmbeddedPortlets()) {
      return null;
    }

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

    if (!layoutTypePortlet.isPortletEmbedded(portletId)) {
      return null;
    }

    PortletPreferencesIds portletPreferencesIds =
        new PortletPreferencesIds(
            themeDisplay.getCompanyId(),
            layout.getGroupId(),
            PortletKeys.PREFS_OWNER_TYPE_LAYOUT,
            PortletKeys.PREFS_PLID_SHARED,
            portletId);

    return _portletPreferencesLocalService.getPreferences(portletPreferencesIds);
  }
  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 void inheritMobileRuleGroups(Layout layout, ServiceContext serviceContext)
      throws PortalException {

    List<MDRRuleGroupInstance> parentMDRRuleGroupInstances =
        mdrRuleGroupInstanceLocalService.getRuleGroupInstances(
            Layout.class.getName(), layout.getParentPlid());

    for (MDRRuleGroupInstance parentMDRRuleGroupInstance : parentMDRRuleGroupInstances) {

      MDRRuleGroupInstance mdrRuleGroupInstance =
          mdrRuleGroupInstanceService.addRuleGroupInstance(
              layout.getGroupId(),
              Layout.class.getName(),
              layout.getPlid(),
              parentMDRRuleGroupInstance.getRuleGroupId(),
              parentMDRRuleGroupInstance.getPriority(),
              serviceContext);

      List<MDRAction> parentMDRActions =
          mdrActionLocalService.getActions(parentMDRRuleGroupInstance.getRuleGroupInstanceId());

      for (MDRAction mdrAction : parentMDRActions) {
        mdrActionService.addAction(
            mdrRuleGroupInstance.getRuleGroupInstanceId(),
            mdrAction.getNameMap(),
            mdrAction.getDescriptionMap(),
            mdrAction.getType(),
            mdrAction.getTypeSettings(),
            serviceContext);
      }
    }
  }
  protected String[] getStaticPortletIds(String position) {
    Layout layout = getLayout();

    String selector1 = StringPool.BLANK;

    Group group = null;

    try {
      group = layout.getGroup();
    } catch (PortalException pe) {
      _log.error("Unable to get group " + layout.getGroupId());

      return new String[0];
    }

    if (group.isUser()) {
      selector1 = LayoutTypePortletConstants.STATIC_PORTLET_USER_SELECTOR;
    } else if (group.isOrganization()) {
      selector1 = LayoutTypePortletConstants.STATIC_PORTLET_ORGANIZATION_SELECTOR;
    } else if (group.isRegularSite()) {
      selector1 = LayoutTypePortletConstants.STATIC_PORTLET_REGULAR_SITE_SELECTOR;
    }

    String selector2 = layout.getFriendlyURL();

    String[] portletIds = PropsUtil.getArray(position, new Filter(selector1, selector2));

    for (int i = 0; i < portletIds.length; i++) {
      portletIds[i] = JS.getSafeName(portletIds[i]);
    }

    return portletIds;
  }
    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 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;
  }
  @Override
  public boolean isPortletEmbedded(String portletId) {
    Layout layout = getLayout();

    return layout.isPortletEmbedded(portletId, layout.getGroupId());
  }