Index: test/src/test/java/org/hippoecm/frontend/util/DocumentListFilterTest.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- test/src/test/java/org/hippoecm/frontend/util/DocumentListFilterTest.java (date 1561452315000) +++ test/src/test/java/org/hippoecm/frontend/util/DocumentListFilterTest.java (date 1561452315000) @@ -0,0 +1,93 @@ +package org.hippoecm.frontend.util; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import javax.jcr.Node; +import javax.jcr.NodeIterator; +import javax.jcr.RepositoryException; + +import org.hippoecm.frontend.PluginTest; +import org.hippoecm.frontend.model.JcrNodeModel; +import org.hippoecm.frontend.plugin.config.IPluginConfig; +import org.hippoecm.frontend.plugin.config.impl.JcrPluginConfig; +import org.hippoecm.frontend.plugins.standards.DocumentListFilter; +import org.junit.Test; +import org.onehippo.repository.testutils.RepositoryTestCase; +import org.onehippo.repository.util.NodeTypeUtils; + +import static org.junit.Assert.*; + +public class DocumentListFilterTest extends PluginTest { + + String[] content = new String[]{ + "/test", + "nt:unstructured", + "/test/content", + "hippostd:folder", + "/test/content/toBeShowed", + "hippo:handle", + "jcr:mixinTypes", "mix:referenceable", + "/test/content/toBeShowed/toBeShowed", + "test:faqitem", + "jcr:mixinTypes", "mix:referenceable", + "/test/content/toBeHidden", + "hippo:handle", + "jcr:mixinTypes", "mix:referenceable", + "/test/content/toBeHidden/toBeHidden", + "hippo:document", + "jcr:mixinTypes", "mix:referenceable", + "/plugin", + "nt:unstructured", + "/plugin/filters", + "frontend:pluginconfig", + "/plugin/filters/showFaqItemType", + "frontend:pluginconfig", + "child", "hippo:handle", + "display", "true", + "subchild", "test:faqitem", + "/plugin/filters/hideDocuments", + "frontend:pluginconfig", + "child", "hippo:handle", + "display", "false", + "subchild", "hippo:document", + + + }; + + + private static List + getListFromIterator(Iterator iterator) { + + // Create an empty list + List list = new ArrayList<>(); + + // Add each element of iterator to the List + iterator.forEachRemaining(list::add); + + // Return the List + return list; + } + + // @Ignore + @Test + public void filter() throws RepositoryException { + session.getWorkspace().getNamespaceRegistry().registerNamespace("test", "http://www.test.com/test/nt/1.0"); + NodeTypeUtils.initializeNodeTypes(session, getClass().getResourceAsStream("/test.cnd"), "test"); + build(content, session); + session.save(); + + IPluginConfig config = new JcrPluginConfig(new JcrNodeModel(session.getNode("/plugin"))); + DocumentListFilter documentListFilter = new DocumentListFilter(config); + + Node folder = session.getNode("/test/content"); + + NodeIterator it = documentListFilter.filter(folder, folder.getNodes()); + + List list = getListFromIterator(it); + assertTrue(list.size() == 1); + assertTrue(list.get(0).getName().equals("toBeShowed")); + removeNode("/plugin"); + } +} \ No newline at end of file Index: api/src/main/java/org/hippoecm/frontend/plugins/standards/DocumentListFilter.java IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- api/src/main/java/org/hippoecm/frontend/plugins/standards/DocumentListFilter.java (date 1556288290000) +++ api/src/main/java/org/hippoecm/frontend/plugins/standards/DocumentListFilter.java (date 1561451536000) @@ -1,12 +1,12 @@ /* * Copyright 2008-2013 Hippo B.V. (http://www.onehippo.com) - * + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -16,10 +16,8 @@ package org.hippoecm.frontend.plugins.standards; import java.util.ArrayList; -import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; -import java.util.Vector; import javax.jcr.Node; import javax.jcr.NodeIterator; @@ -36,66 +34,22 @@ private static final Logger log = LoggerFactory.getLogger(DocumentListFilter.class); private String currentState = ""; - - private static class FilterDefinition implements IClusterable { - private static final long serialVersionUID = 1L; - - String state; - String path; - String parent; - String child; - String targetState; - boolean targetDisplay; - String targetName; - - FilterDefinition(String state, String path, String parent, String child, String targetState, boolean targetDisplay, String targetName) { - this.state = state; - this.path = path; - this.parent = parent; - this.child = child; - this.targetState = targetState; - this.targetDisplay = targetDisplay; - this.targetName = targetName; - } - - boolean match(String currentState, Node node) throws RepositoryException { - if (!currentState.isEmpty() && !state.isEmpty() && !currentState.equals(state)) { - return false; - } - if (!path.isEmpty()) { - if (path.startsWith("/")) { - if (!path.equals(node.getPath())) { - return false; - } - } else if (!path.equals(node.getName())) { - return false; - } - } - if (!parent.isEmpty() && (node.getDepth() == 0 || !node.getParent().isNodeType(parent))) { - return false; - } - if (!child.isEmpty() && !node.isNodeType(child)) { - return false; - } - return true; - } - } - private List filters; public DocumentListFilter(IPluginConfig config) { - filters = new ArrayList(); + filters = new ArrayList(); IPluginConfig filterConfig = config.getPluginConfig("filters"); if (filterConfig != null) { for (IPluginConfig filter : filterConfig.getPluginConfigSet()) { filters.add(new FilterDefinition(filter.getString("state", ""), - filter.getString("path", ""), - filter.getString("parent", ""), - filter.getString("child", ""), - filter.getString("target", ""), - filter.getBoolean("display"), - filter.getString("name", ""))); + filter.getString("path", ""), + filter.getString("parent", ""), + filter.getString("child", ""), + filter.getString("subchild", ""), + filter.getString("target", ""), + filter.getBoolean("display"), + filter.getString("name", ""))); } } @@ -103,8 +57,8 @@ // of looping over {@link FilterDefinition}(s) in case the debug level is not enabled if (log.isDebugEnabled()) { log.debug("Filter definitions are:"); - for(FilterDefinition def : filters) { - log.debug(" ({}, {}, {}, {}, {}, {}, {})", new Object[] {def.state, def.path, def.parent, def.child, + for (FilterDefinition def : filters) { + log.debug(" ({}, {}, {}, {}, {}, {}, {})", new Object[]{def.state, def.path, def.parent, def.child, def.targetState, def.targetDisplay, def.targetName}); } @@ -116,6 +70,7 @@ currentState = state; } + public NodeIterator filter(Node current, final NodeIterator iter) { return new NodeIterator() { private int index = 0; @@ -200,4 +155,69 @@ } return displayName; } + + private static class FilterDefinition implements IClusterable { + private static final long serialVersionUID = 1L; + + String state; + String path; + String parent; + String child; + String subChild; + String targetState; + boolean targetDisplay; + String targetName; + + FilterDefinition(String state, String path, String parent, String child, String subChild, String targetState, boolean targetDisplay, String targetName) { + this.state = state; + this.path = path; + this.parent = parent; + this.child = child; + this.subChild = subChild; + this.targetState = targetState; + this.targetDisplay = targetDisplay; + this.targetName = targetName; + } + + boolean match(String currentState, Node node) throws RepositoryException { + if (!currentState.isEmpty() && !state.isEmpty() && !currentState.equals(state)) { + return false; + } + if (!path.isEmpty()) { + if (path.startsWith("/")) { + if (!path.equals(node.getPath())) { + return false; + } + } else if (!path.equals(node.getName())) { + return false; + } + } + if (!parent.isEmpty() && (node.getDepth() == 0 || !node.getParent().isNodeType(parent))) { + return false; + } + if (!child.isEmpty() && !node.isNodeType(child) && subChild.isEmpty()) { + return false; + } + if (!child.isEmpty() && node.isNodeType(child) && !subChild.isEmpty() && !hadSubChildrenOfType(node, subChild)) { + return false; + } + return true; + } + + public boolean hadSubChildrenOfType(Node node, String subchildType) throws RepositoryException { + boolean hasSubChildOfType = false; + if (node != null) { + NodeIterator it = node.getNodes(); + while (it.hasNext()) { + Node subChild = it.nextNode(); + if (subChild != null && subChild.isNodeType(subchildType)) { + hasSubChildOfType = true; + break; + } + + } + } + return hasSubChildOfType; + } + } } Index: test/src/test/resources/test.cnd IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== --- test/src/test/resources/test.cnd (date 1560261235000) +++ test/src/test/resources/test.cnd (date 1560261235000) @@ -0,0 +1,9 @@ +<'test'='http://www.test.com/test/nt/1.0'> +<'hippo'='http://www.onehippo.org/jcr/hippo/nt/2.0.4'> + +[test:basedocument] > hippo:document + orderable + +[test:faqitem] > test:basedocument + orderable +