@Override
  public TreeView getTreeView() throws PortalException {
    _nodeId = 1;

    _list = new ArrayList<>();

    TreeNodeView rootNodeView = null;

    if (_rootNodeName != null) {
      rootNodeView = new TreeNodeView(_nodeId);

      rootNodeView.setLeaf(false);
      rootNodeView.setName(_rootNodeName);

      _list.add(rootNodeView);
    }

    PortletCategory portletCategory =
        (PortletCategory) WebAppPool.get(_user.getCompanyId(), WebKeys.PORTLET_CATEGORY);

    List<PortletCategory> portletCategories =
        ListUtil.fromCollection(portletCategory.getCategories());

    iteratePortletCategories(rootNodeView, portletCategories, _nodeId, 0);

    return new TreeView(_list, _depth);
  }
  @Override
  public Map<String, VariableDependencies> getVariableDependenciesMap()
      throws DDMExpressionException {

    Map<String, VariableDependencies> variableDependenciesMap = new HashMap<>();

    List<Variable> variables = ListUtil.fromCollection(_variables.values());

    for (Variable variable : variables) {
      populateVariableDependenciesMap(variable, variableDependenciesMap);
    }

    return variableDependenciesMap;
  }
  public List<Theme> getWARThemes() {
    List<Theme> themes = ListUtil.fromCollection(_themes.values());

    Iterator<Theme> itr = themes.iterator();

    while (itr.hasNext()) {
      Theme theme = itr.next();

      if (!theme.isWARFile()) {
        itr.remove();
      }
    }

    return themes;
  }
  @Test
  public void testMultipleInvalidFriendlyURLMapperURL() throws Exception {
    Map<Locale, String> friendlyURLMap = new HashMap<>();

    friendlyURLMap.put(LocaleUtil.SPAIN, "/tags/dos");
    friendlyURLMap.put(LocaleUtil.US, "/tags/two");

    try {
      addLayout(_group.getGroupId(), false, friendlyURLMap);
    } catch (LayoutFriendlyURLsException lfurle) {
      Map<Locale, Exception> localizedExceptionsMap = lfurle.getLocalizedExceptionsMap();

      List<Exception> layoutFriendlyURLExceptions =
          ListUtil.fromCollection(localizedExceptionsMap.values());

      Assert.assertEquals(2, layoutFriendlyURLExceptions.size());

      for (Exception e : layoutFriendlyURLExceptions) {
        String keywordsConflict = ((LayoutFriendlyURLException) e).getKeywordConflict();

        Assert.assertEquals(keywordsConflict, "tags");
      }
    }
  }
  public List<Theme> getThemes(long companyId) {
    List<Theme> themes = ListUtil.fromCollection(_getThemes(companyId).values());

    return ListUtil.sort(themes);
  }
  @Test
  public void testInvalidFriendlyURLMapperURLInDefaultLocale() throws Exception {

    Map<Locale, String> friendlyURLMap = new HashMap<>();

    friendlyURLMap.put(LocaleUtil.US, "/tags");

    try {
      addLayout(_group.getGroupId(), false, friendlyURLMap);

      Assert.fail();
    } catch (LayoutFriendlyURLsException lfurle) {
      Map<Locale, Exception> localizedExceptionsMap = lfurle.getLocalizedExceptionsMap();

      List<Exception> layoutFriendlyURLExceptions =
          ListUtil.fromCollection(localizedExceptionsMap.values());

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

      LayoutFriendlyURLException layoutFriendlyURLException =
          (LayoutFriendlyURLException) layoutFriendlyURLExceptions.get(0);

      Assert.assertEquals(layoutFriendlyURLException.getKeywordConflict(), "tags");
    }

    friendlyURLMap = new HashMap<>();

    friendlyURLMap.put(LocaleUtil.US, "/home/tags");

    try {
      addLayout(_group.getGroupId(), false, friendlyURLMap);

      Assert.fail();
    } catch (LayoutFriendlyURLsException lfurle) {
      Map<Locale, Exception> localizedExceptionsMap = lfurle.getLocalizedExceptionsMap();

      List<Exception> layoutFriendlyURLExceptions =
          ListUtil.fromCollection(localizedExceptionsMap.values());

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

      LayoutFriendlyURLException layoutFriendlyURLException =
          (LayoutFriendlyURLException) layoutFriendlyURLExceptions.get(0);

      Assert.assertEquals(layoutFriendlyURLException.getKeywordConflict(), "tags");
    }

    friendlyURLMap = new HashMap<>();

    friendlyURLMap.put(LocaleUtil.US, "/tags/home");

    try {
      addLayout(_group.getGroupId(), false, friendlyURLMap);

      Assert.fail();
    } catch (LayoutFriendlyURLsException lfurle) {
      Map<Locale, Exception> localizedExceptionsMap = lfurle.getLocalizedExceptionsMap();

      List<Exception> layoutFriendlyURLExceptions =
          ListUtil.fromCollection(localizedExceptionsMap.values());

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

      LayoutFriendlyURLException layoutFriendlyURLException =
          (LayoutFriendlyURLException) layoutFriendlyURLExceptions.get(0);

      Assert.assertEquals(layoutFriendlyURLException.getKeywordConflict(), "tags");
    }

    friendlyURLMap = new HashMap<>();

    friendlyURLMap.put(LocaleUtil.US, "/blogs/-/home");

    try {
      addLayout(_group.getGroupId(), false, friendlyURLMap);

      Assert.fail();
    } catch (LayoutFriendlyURLsException lfurle) {
      Map<Locale, Exception> localizedExceptionsMap = lfurle.getLocalizedExceptionsMap();

      List<Exception> layoutFriendlyURLExceptions =
          ListUtil.fromCollection(localizedExceptionsMap.values());

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

      LayoutFriendlyURLException layoutFriendlyURLException =
          (LayoutFriendlyURLException) layoutFriendlyURLExceptions.get(0);

      Assert.assertEquals(layoutFriendlyURLException.getKeywordConflict(), "/-/");
    }
  }
  protected void iteratePortletCategories(
      TreeNodeView parentNodeView,
      List<PortletCategory> portletCategories,
      long parentId,
      int depth)
      throws PortalException {

    portletCategories =
        ListUtil.sort(portletCategories, new PortletCategoryComparator(getLocale()));

    for (int i = 0; i < portletCategories.size(); i++) {
      PortletCategory portletCategory = portletCategories.get(i);

      if (portletCategory.isHidden()) {
        continue;
      }

      if (i == 0) {
        depth++;

        if (depth > _depth) {
          _depth = depth;
        }
      }

      TreeNodeView nodeView = new TreeNodeView(++_nodeId);

      nodeView.setDepth(depth);
      nodeView.setLeaf(false);

      if ((i + 1) == portletCategories.size()) {
        nodeView.setLs("1");
      } else {
        nodeView.setLs("0");
      }

      nodeView.setName(LanguageUtil.get(getLocale(), portletCategory.getName()));
      nodeView.setObjId(portletCategory.getPath());
      nodeView.setParentId(parentId);

      if (_hierarchicalTree) {
        if (parentNodeView != null) {
          parentNodeView.addChild(nodeView);
        }
      } else {
        _list.add(nodeView);
      }

      int nodeId = _nodeId;

      List<PortletCategory> subCategories =
          ListUtil.fromCollection(portletCategory.getCategories());

      iteratePortletCategories(nodeView, subCategories, nodeId, depth);

      if (_iteratePortlets) {
        iteratePortlets(
            nodeView, portletCategory, portletCategory.getPortletIds(), nodeId, depth + 1);
      }
    }
  }