@Test public void testNtStatement() throws Exception { TestUtil.withInMemoryRepo(session -> { TestUtil.installCndFromURL(session, getClass().getResource("/sling_nodetypes.cnd")); final int sample = 5; List<String> orderedPaths = IntStream.range(0, sample).mapToObj(val -> "/test/ordered" + val).collect(Collectors.toList()); List<String> unorderedPaths = IntStream.range(0, sample).mapToObj(val -> "/test/unordered" + val).collect(Collectors.toList()); assertEquals("should generate n paths: ", sample, orderedPaths.size()); orderedPaths.forEach(Fun.tryOrVoid1(path -> JcrUtils.getOrCreateByPath(path, "nt:folder", "sling:OrderedFolder", session, true))); unorderedPaths.forEach(Fun.tryOrVoid1(path -> JcrUtils.getOrCreateByPath(path, "nt:folder", "sling:Folder", session, true))); final ChecklistExporter exporter = new ChecklistExporter.Builder().build(); final String statement = exporter.ntStatement(session, Arrays.asList("nt:folder", "+sling:Folder")); assertTrue("statement should contain 'FROM [sling:Folder]'", statement.contains("FROM [sling:Folder]")); assertTrue("statement should contain ' UNION '", statement.contains(" UNION ")); assertTrue("statement should contain 'FROM [nt:base]'", statement.contains("FROM [nt:base]")); assertTrue("statement should contain '[jcr:primaryType] = 'nt:folder'", statement.contains("[jcr:primaryType] = 'nt:folder'")); List<ForcedRoot> bySQL2 = exporter.query(session, statement); for (String path : unorderedPaths) { ForcedRoot root0 = bySQL2.stream().filter(root -> path.equals(root.getPath())).findFirst().orElse(null); assertNotNull(String.format("root for path %s should not be null", path), root0); assertEquals(String.format("root primaryType for path %s should be sling:Folder", path), "sling:Folder", root0.getPrimaryType()); } for (String path : orderedPaths) { ForcedRoot root0 = bySQL2.stream().filter(root -> path.equals(root.getPath())).findFirst().orElse(null); assertNotNull(String.format("root for path %s should not be null", path), root0); assertEquals(String.format("root primaryType for path %s should be sling:OrderedFolder", path), "sling:OrderedFolder", root0.getPrimaryType()); } for (String path : Collections.singletonList("/test")) { ForcedRoot root0 = bySQL2.stream().filter(root -> path.equals(root.getPath())).findFirst().orElse(null); assertNotNull(String.format("root for path %s should not be null", path), root0); assertEquals(String.format("root primaryType for path %s should be nt:folder", path), "nt:folder", root0.getPrimaryType()); } final String simpler = exporter.ntStatement(session, Collections.singletonList("sling:OrderedFolder")); List<ForcedRoot> orderedRoots = exporter.query(session, simpler); for (String path : orderedPaths) { ForcedRoot root0 = orderedRoots.stream().filter(root -> path.equals(root.getPath())).findFirst().orElse(null); assertNotNull(String.format("root for path %s should not be null", path), root0); assertEquals(String.format("root primaryType for path %s should be sling:OrderedFolder", path), "sling:OrderedFolder", root0.getPrimaryType()); } }); }
String ntStatement(final Session session, final List<String> nodeTypeNames) throws RepositoryException { final List<String> subqueries = new ArrayList<>(); final NodeTypeManager ntManager = session.getWorkspace().getNodeTypeManager(); nodeTypeNames.stream() .filter(COVARIANT_FILTER) .map(COVARIANT_FORMAT) .filter(Fun.testOrDefault1(ntManager::hasNodeType, false)) .map(name -> String.format("SELECT [jcr:path] FROM [%s] AS a", name)) .forEachOrdered(subqueries::add); nodeTypeNames.stream() .filter(COVARIANT_FILTER.negate()) .filter(Fun.testOrDefault1(ntManager::hasNodeType, false)) .map(name -> String.format("SELECT [jcr:path] FROM [nt:base] AS a WHERE [a].[jcr:primaryType] = '%s' UNION SELECT [jcr:path] FROM [nt:base] AS a WHERE [a].[jcr:mixinTypes] = '%s'", name, name)) .forEachOrdered(subqueries::add); return String.join(" UNION ", subqueries) + " OPTION(TRAVERSAL OK, INDEX NAME nodetype)"; }
ChecklistExporter { String ntStatement(final Session session, final List<String> nodeTypeNames) throws RepositoryException { final List<String> subqueries = new ArrayList<>(); final NodeTypeManager ntManager = session.getWorkspace().getNodeTypeManager(); nodeTypeNames.stream() .filter(COVARIANT_FILTER) .map(COVARIANT_FORMAT) .filter(Fun.testOrDefault1(ntManager::hasNodeType, false)) .map(name -> String.format("SELECT [jcr:path] FROM [%s] AS a", name)) .forEachOrdered(subqueries::add); nodeTypeNames.stream() .filter(COVARIANT_FILTER.negate()) .filter(Fun.testOrDefault1(ntManager::hasNodeType, false)) .map(name -> String.format("SELECT [jcr:path] FROM [nt:base] AS a WHERE [a].[jcr:primaryType] = '%s' UNION SELECT [jcr:path] FROM [nt:base] AS a WHERE [a].[jcr:mixinTypes] = '%s'", name, name)) .forEachOrdered(subqueries::add); return String.join(" UNION ", subqueries) + " OPTION(TRAVERSAL OK, INDEX NAME nodetype)"; } }
ChecklistExporter { String ntStatement(final Session session, final List<String> nodeTypeNames) throws RepositoryException { final List<String> subqueries = new ArrayList<>(); final NodeTypeManager ntManager = session.getWorkspace().getNodeTypeManager(); nodeTypeNames.stream() .filter(COVARIANT_FILTER) .map(COVARIANT_FORMAT) .filter(Fun.testOrDefault1(ntManager::hasNodeType, false)) .map(name -> String.format("SELECT [jcr:path] FROM [%s] AS a", name)) .forEachOrdered(subqueries::add); nodeTypeNames.stream() .filter(COVARIANT_FILTER.negate()) .filter(Fun.testOrDefault1(ntManager::hasNodeType, false)) .map(name -> String.format("SELECT [jcr:path] FROM [nt:base] AS a WHERE [a].[jcr:primaryType] = '%s' UNION SELECT [jcr:path] FROM [nt:base] AS a WHERE [a].[jcr:mixinTypes] = '%s'", name, name)) .forEachOrdered(subqueries::add); return String.join(" UNION ", subqueries) + " OPTION(TRAVERSAL OK, INDEX NAME nodetype)"; } private  ChecklistExporter(final List<Op> operations,
                              final List<String> exportTypeDefs,
                              final List<Rule> pathScopes,
                              final List<Rule> nodeTypeFilters,
                              final List<JcrNs> jcrNamespaces); }
ChecklistExporter { String ntStatement(final Session session, final List<String> nodeTypeNames) throws RepositoryException { final List<String> subqueries = new ArrayList<>(); final NodeTypeManager ntManager = session.getWorkspace().getNodeTypeManager(); nodeTypeNames.stream() .filter(COVARIANT_FILTER) .map(COVARIANT_FORMAT) .filter(Fun.testOrDefault1(ntManager::hasNodeType, false)) .map(name -> String.format("SELECT [jcr:path] FROM [%s] AS a", name)) .forEachOrdered(subqueries::add); nodeTypeNames.stream() .filter(COVARIANT_FILTER.negate()) .filter(Fun.testOrDefault1(ntManager::hasNodeType, false)) .map(name -> String.format("SELECT [jcr:path] FROM [nt:base] AS a WHERE [a].[jcr:primaryType] = '%s' UNION SELECT [jcr:path] FROM [nt:base] AS a WHERE [a].[jcr:mixinTypes] = '%s'", name, name)) .forEachOrdered(subqueries::add); return String.join(" UNION ", subqueries) + " OPTION(TRAVERSAL OK, INDEX NAME nodetype)"; } private  ChecklistExporter(final List<Op> operations,
                              final List<String> exportTypeDefs,
                              final List<Rule> pathScopes,
                              final List<Rule> nodeTypeFilters,
                              final List<JcrNs> jcrNamespaces); void updateChecklist(final WriterOpener writerOpener,
                                final Session session,
                                final Checklist checklist,
                                final ForcedRootUpdatePolicy updatePolicy); List<ForcedRoot> findRoots(final Session session); }
ChecklistExporter { String ntStatement(final Session session, final List<String> nodeTypeNames) throws RepositoryException { final List<String> subqueries = new ArrayList<>(); final NodeTypeManager ntManager = session.getWorkspace().getNodeTypeManager(); nodeTypeNames.stream() .filter(COVARIANT_FILTER) .map(COVARIANT_FORMAT) .filter(Fun.testOrDefault1(ntManager::hasNodeType, false)) .map(name -> String.format("SELECT [jcr:path] FROM [%s] AS a", name)) .forEachOrdered(subqueries::add); nodeTypeNames.stream() .filter(COVARIANT_FILTER.negate()) .filter(Fun.testOrDefault1(ntManager::hasNodeType, false)) .map(name -> String.format("SELECT [jcr:path] FROM [nt:base] AS a WHERE [a].[jcr:primaryType] = '%s' UNION SELECT [jcr:path] FROM [nt:base] AS a WHERE [a].[jcr:mixinTypes] = '%s'", name, name)) .forEachOrdered(subqueries::add); return String.join(" UNION ", subqueries) + " OPTION(TRAVERSAL OK, INDEX NAME nodetype)"; } private  ChecklistExporter(final List<Op> operations,
                              final List<String> exportTypeDefs,
                              final List<Rule> pathScopes,
                              final List<Rule> nodeTypeFilters,
                              final List<JcrNs> jcrNamespaces); void updateChecklist(final WriterOpener writerOpener,
                                final Session session,
                                final Checklist checklist,
                                final ForcedRootUpdatePolicy updatePolicy); List<ForcedRoot> findRoots(final Session session); static final ForcedRootUpdatePolicy DEFAULT_UPDATE_POLICY; static final String COVARIANT_PREFIX; }