@Override
 protected StagedModel getStagedModel(String uuid, Group group) {
   try {
     return LayoutLocalServiceUtil.fetchLayoutByUuidAndGroupId(uuid, group.getGroupId(), false);
   } catch (Exception e) {
     return null;
   }
 }
 public Layout getScopeLayout() throws PortalException {
   if (_layout.hasScopeGroup()) {
     return _layout;
   } else if (_scopeGroup.isLayout()) {
     return LayoutLocalServiceUtil.getLayout(_scopeGroup.getClassPK());
   } else {
     return null;
   }
 }
  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 static void _addLayoutBreadcrumbEntries(
      List<BreadcrumbEntry> breadcrumbEntries, ThemeDisplay themeDisplay, Layout layout)
      throws Exception {

    if (layout.getParentLayoutId() != LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {

      Layout parentLayout = LayoutLocalServiceUtil.getParentLayout(layout);

      _addLayoutBreadcrumbEntries(breadcrumbEntries, themeDisplay, parentLayout);
    }

    BreadcrumbEntry breadcrumbEntry = new BreadcrumbEntry();

    breadcrumbEntry.setBaseModel(layout);

    LayoutType layoutType = layout.getLayoutType();

    if (!layoutType.isBrowsable()) {
      breadcrumbEntry.setBrowsable(false);
    }

    String layoutName = layout.getName(themeDisplay.getLocale());

    if (layout.isTypeControlPanel()) {
      if (layoutName.equals(LayoutConstants.NAME_CONTROL_PANEL_DEFAULT)) {
        layoutName = LanguageUtil.get(themeDisplay.getLocale(), "control-panel");
      }
    }

    breadcrumbEntry.setTitle(layoutName);

    String layoutURL = PortalUtil.getLayoutFullURL(layout, themeDisplay);

    if (themeDisplay.isAddSessionIdToURL()) {
      layoutURL = PortalUtil.getURLWithSessionId(layoutURL, themeDisplay.getSessionId());
    }

    breadcrumbEntry.setURL(layoutURL);

    breadcrumbEntries.add(breadcrumbEntry);
  }
  @Override
  protected void validateImport(
      Map<String, List<StagedModel>> dependentStagedModelsMap, Group group) throws Exception {

    List<StagedModel> dependentStagedModels =
        dependentStagedModelsMap.get(Layout.class.getSimpleName());

    Assert.assertEquals(1, dependentStagedModels.size());

    Layout parentLayout = (Layout) dependentStagedModels.get(0);

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        parentLayout.getUuid(), group.getGroupId(), false);

    List<LayoutFriendlyURL> parentLayoutFriendlyURLs =
        LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(parentLayout.getPlid());

    LayoutFriendlyURL parentLayoutFriendlyURL = parentLayoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        parentLayoutFriendlyURL.getUuid(), group.getGroupId());
  }
  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;
  }
  protected String getCheckedNodes() {
    JSONArray checkedNodesJSONArray = JSONFactoryUtil.createJSONArray();

    String checkedLayoutIds =
        GetterUtil.getString(
            _selectedLayoutIds,
            SessionTreeJSClicks.getOpenNodes(request, _treeId + "SelectedNode"));

    if (Validator.isNull(checkedLayoutIds)) {
      return checkedNodesJSONArray.toString();
    }

    for (long checkedLayoutId : StringUtil.split(checkedLayoutIds, 0L)) {
      Layout checkedLayout =
          LayoutLocalServiceUtil.fetchLayout(_groupId, _privateLayout, checkedLayoutId);

      if (checkedLayout != null) {
        checkedNodesJSONArray.put(String.valueOf(checkedLayout.getPlid()));
      }
    }

    return checkedNodesJSONArray.toString();
  }
  @Test
  public void testTypeLinkToLayout() throws Exception {
    initExport();

    Map<String, List<StagedModel>> dependentStagedModelsMap = new HashMap<>();

    Layout linkedLayout = LayoutTestUtil.addLayout(stagingGroup);

    List<LayoutFriendlyURL> linkedLayoutFriendlyURLs =
        LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(linkedLayout.getPlid());

    addDependentStagedModel(dependentStagedModelsMap, Layout.class, linkedLayout);

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, linkedLayout);

    Layout layout =
        LayoutTestUtil.addTypeLinkToLayoutLayout(
            stagingGroup.getGroupId(), linkedLayout.getLayoutId());

    List<LayoutFriendlyURL> layoutFriendlyURLs =
        LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLs(layout.getPlid());

    addDependentLayoutFriendlyURLs(dependentStagedModelsMap, layout);

    StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, layout);

    validateExport(portletDataContext, layout, dependentStagedModelsMap);

    initImport();

    ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
        ExportImportLifecycleConstants.EVENT_LAYOUT_IMPORT_STARTED,
        ExportImportLifecycleConstants.PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS,
        PortletDataContextFactoryUtil.clonePortletDataContext(portletDataContext));

    Layout exportedLayout = (Layout) readExportedStagedModel(layout);

    StagedModelDataHandlerUtil.importStagedModel(portletDataContext, exportedLayout);

    Layout exportedLinkedLayout = (Layout) readExportedStagedModel(linkedLayout);

    StagedModelDataHandlerUtil.importStagedModel(portletDataContext, exportedLinkedLayout);

    ExportImportLifecycleManagerUtil.fireExportImportLifecycleEvent(
        ExportImportLifecycleConstants.EVENT_LAYOUT_IMPORT_SUCCEEDED,
        ExportImportLifecycleConstants.PROCESS_FLAG_LAYOUT_STAGING_IN_PROCESS,
        PortletDataContextFactoryUtil.clonePortletDataContext(portletDataContext));

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        linkedLayout.getUuid(), liveGroup.getGroupId(), false);

    LayoutFriendlyURL linkedLayoutFriendlyURL = linkedLayoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        linkedLayoutFriendlyURL.getUuid(), liveGroup.getGroupId());

    LayoutLocalServiceUtil.getLayoutByUuidAndGroupId(
        layout.getUuid(), liveGroup.getGroupId(), false);

    LayoutFriendlyURL layoutFriendlyURL = layoutFriendlyURLs.get(0);

    LayoutFriendlyURLLocalServiceUtil.getLayoutFriendlyURLByUuidAndGroupId(
        layoutFriendlyURL.getUuid(), liveGroup.getGroupId());
  }