@Test public void testCategoryConstraintSearchQuery() throws RepositoryException { Mockito.when(servletRequest.getHeader("Referer")).thenReturn("editor.html/test/page.html"); PageManager pageManager = Mockito.mock(PageManager.class); Mockito.when(resourceResolver.adaptTo(PageManager.class)).thenReturn(pageManager); Page page = Mockito.mock(Page.class); Mockito.when(pageManager.getContainingPage("/test/page")).thenReturn(page); Resource contentResource = Mockito.mock(Resource.class); Mockito.when(page.getContentResource()).thenReturn(contentResource); ModifiableValueMapDecorator valueMap = new ModifiableValueMapDecorator(new HashMap<>()); Mockito.when(contentResource.getValueMap()).thenReturn(valueMap); valueMap.put(PN_CATALOG_PATH, "/catalog/path"); Resource resource = Mockito.mock(Resource.class); Mockito.when(resourceResolver.getResource("/catalog/path")).thenReturn(resource); ModifiableValueMapDecorator properties = new ModifiableValueMapDecorator(new HashMap<>()); Mockito.when(resource.getValueMap()).thenReturn(properties); properties.put("cq:commerceType", "category"); properties.put("cifId", "1"); ViewQuery viewQuery = viewHandler.createQuery(servletRequest, null, "query=trail"); PredicateGroup predicateGroup = ((GraphqlProductViewHandler.GQLViewQuery) viewQuery).predicateGroup; Assert.assertNotNull(predicateGroup.getByName("3_" + CATEGORY_ID_PARAMETER)); Assert.assertEquals(predicateGroup.getByName("3_" + CATEGORY_ID_PARAMETER).get(CATEGORY_ID_PARAMETER), "1"); Assert.assertNotNull(predicateGroup.getByName("4_" + CATEGORY_PATH_PARAMETER)); Assert.assertEquals(predicateGroup.getByName("4_" + CATEGORY_PATH_PARAMETER).get(CATEGORY_PATH_PARAMETER), "/catalog/path"); }
@Override protected ViewQuery createQuery(SlingHttpServletRequest request, Session session, String queryString) throws RepositoryException { queryString = preserveWildcards(queryString); PredicateGroup gqlPredicateGroup = PredicateConverter.createPredicatesFromGQL(queryString); ResourceResolver resolver = request.getResourceResolver(); tagManager = resolver.adaptTo(TagManager.class); Set<String> predicateSet = customizePredicateGroup(gqlPredicateGroup); RequestPathInfo pathInfo = request.getRequestPathInfo(); CommerceBasePathsService cbps = resolver.adaptTo(CommerceBasePathsService.class); String defaultStartPath = cbps.getProductsBasePath(); String startPath = (pathInfo.getSuffix() != null && pathInfo.getSuffix().startsWith(defaultStartPath)) ? pathInfo.getSuffix() : defaultStartPath; if (!predicateSet.contains(PathPredicateEvaluator.PATH)) { gqlPredicateGroup.add(new Predicate(PathPredicateEvaluator.PATH).set(PathPredicateEvaluator.PATH, startPath)); } String refererHeader = request.getHeader(REFERER_HEADER); if (StringUtils.isNotBlank(refererHeader) && refererHeader.contains(PAGE_EDITOR_PATH + "/")) { int p = refererHeader.lastIndexOf(PAGE_EDITOR_PATH); String pagePath = refererHeader.substring(p + PAGE_EDITOR_PATH.length()); if (pagePath.endsWith(".html")) { pagePath = pagePath.substring(0, pagePath.length() - ".html".length()); } CatalogSearchSupport catalogSearch = new CatalogSearchSupport(resolver); String catalogPath = catalogSearch.findCatalogPath(pagePath); String rootCategoryId = catalogSearch.findCategoryId(catalogPath); if (rootCategoryId != null) { gqlPredicateGroup.add(new Predicate(CATEGORY_ID_PARAMETER).set(CATEGORY_ID_PARAMETER, rootCategoryId)); gqlPredicateGroup.add(new Predicate(CATEGORY_PATH_PARAMETER).set(CATEGORY_PATH_PARAMETER, catalogPath)); } } else { LOGGER.warn("The path of the edited page cannot be determined"); } gqlPredicateGroup.add(new Predicate(TypePredicateEvaluator.TYPE).set(TypePredicateEvaluator.TYPE, NT_UNSTRUCTURED)); if (gqlPredicateGroup.get(Predicate.PARAM_LIMIT) == null) { String limit = request.getParameter(LIMIT); if ((limit != null) && (!limit.equals(""))) { int offset = Integer.parseInt(StringUtils.substringBefore(limit, "..")); int total = Integer.parseInt(StringUtils.substringAfter(limit, "..")); gqlPredicateGroup.set(Predicate.PARAM_LIMIT, Long.toString(total - offset)); gqlPredicateGroup.set(Predicate.PARAM_OFFSET, Long.toString(offset)); } else { gqlPredicateGroup.set(Predicate.PARAM_LIMIT, Long.toString(DEFAULT_LIMIT)); } } addProductConstraint(gqlPredicateGroup); if (!predicateSet.contains(Predicate.ORDER_BY)) { gqlPredicateGroup.add(new Predicate(Predicate.ORDER_BY) .set(Predicate.ORDER_BY, "@" + JCR_LASTMODIFIED) .set(Predicate.PARAM_SORT, Predicate.SORT_DESCENDING)); } return new GQLViewQuery(resolver, omniSearchHandler, xssAPI, gqlPredicateGroup); }
GraphqlProductViewHandler extends ViewHandler { @Override protected ViewQuery createQuery(SlingHttpServletRequest request, Session session, String queryString) throws RepositoryException { queryString = preserveWildcards(queryString); PredicateGroup gqlPredicateGroup = PredicateConverter.createPredicatesFromGQL(queryString); ResourceResolver resolver = request.getResourceResolver(); tagManager = resolver.adaptTo(TagManager.class); Set<String> predicateSet = customizePredicateGroup(gqlPredicateGroup); RequestPathInfo pathInfo = request.getRequestPathInfo(); CommerceBasePathsService cbps = resolver.adaptTo(CommerceBasePathsService.class); String defaultStartPath = cbps.getProductsBasePath(); String startPath = (pathInfo.getSuffix() != null && pathInfo.getSuffix().startsWith(defaultStartPath)) ? pathInfo.getSuffix() : defaultStartPath; if (!predicateSet.contains(PathPredicateEvaluator.PATH)) { gqlPredicateGroup.add(new Predicate(PathPredicateEvaluator.PATH).set(PathPredicateEvaluator.PATH, startPath)); } String refererHeader = request.getHeader(REFERER_HEADER); if (StringUtils.isNotBlank(refererHeader) && refererHeader.contains(PAGE_EDITOR_PATH + "/")) { int p = refererHeader.lastIndexOf(PAGE_EDITOR_PATH); String pagePath = refererHeader.substring(p + PAGE_EDITOR_PATH.length()); if (pagePath.endsWith(".html")) { pagePath = pagePath.substring(0, pagePath.length() - ".html".length()); } CatalogSearchSupport catalogSearch = new CatalogSearchSupport(resolver); String catalogPath = catalogSearch.findCatalogPath(pagePath); String rootCategoryId = catalogSearch.findCategoryId(catalogPath); if (rootCategoryId != null) { gqlPredicateGroup.add(new Predicate(CATEGORY_ID_PARAMETER).set(CATEGORY_ID_PARAMETER, rootCategoryId)); gqlPredicateGroup.add(new Predicate(CATEGORY_PATH_PARAMETER).set(CATEGORY_PATH_PARAMETER, catalogPath)); } } else { LOGGER.warn("The path of the edited page cannot be determined"); } gqlPredicateGroup.add(new Predicate(TypePredicateEvaluator.TYPE).set(TypePredicateEvaluator.TYPE, NT_UNSTRUCTURED)); if (gqlPredicateGroup.get(Predicate.PARAM_LIMIT) == null) { String limit = request.getParameter(LIMIT); if ((limit != null) && (!limit.equals(""))) { int offset = Integer.parseInt(StringUtils.substringBefore(limit, "..")); int total = Integer.parseInt(StringUtils.substringAfter(limit, "..")); gqlPredicateGroup.set(Predicate.PARAM_LIMIT, Long.toString(total - offset)); gqlPredicateGroup.set(Predicate.PARAM_OFFSET, Long.toString(offset)); } else { gqlPredicateGroup.set(Predicate.PARAM_LIMIT, Long.toString(DEFAULT_LIMIT)); } } addProductConstraint(gqlPredicateGroup); if (!predicateSet.contains(Predicate.ORDER_BY)) { gqlPredicateGroup.add(new Predicate(Predicate.ORDER_BY) .set(Predicate.ORDER_BY, "@" + JCR_LASTMODIFIED) .set(Predicate.PARAM_SORT, Predicate.SORT_DESCENDING)); } return new GQLViewQuery(resolver, omniSearchHandler, xssAPI, gqlPredicateGroup); } }
GraphqlProductViewHandler extends ViewHandler { @Override protected ViewQuery createQuery(SlingHttpServletRequest request, Session session, String queryString) throws RepositoryException { queryString = preserveWildcards(queryString); PredicateGroup gqlPredicateGroup = PredicateConverter.createPredicatesFromGQL(queryString); ResourceResolver resolver = request.getResourceResolver(); tagManager = resolver.adaptTo(TagManager.class); Set<String> predicateSet = customizePredicateGroup(gqlPredicateGroup); RequestPathInfo pathInfo = request.getRequestPathInfo(); CommerceBasePathsService cbps = resolver.adaptTo(CommerceBasePathsService.class); String defaultStartPath = cbps.getProductsBasePath(); String startPath = (pathInfo.getSuffix() != null && pathInfo.getSuffix().startsWith(defaultStartPath)) ? pathInfo.getSuffix() : defaultStartPath; if (!predicateSet.contains(PathPredicateEvaluator.PATH)) { gqlPredicateGroup.add(new Predicate(PathPredicateEvaluator.PATH).set(PathPredicateEvaluator.PATH, startPath)); } String refererHeader = request.getHeader(REFERER_HEADER); if (StringUtils.isNotBlank(refererHeader) && refererHeader.contains(PAGE_EDITOR_PATH + "/")) { int p = refererHeader.lastIndexOf(PAGE_EDITOR_PATH); String pagePath = refererHeader.substring(p + PAGE_EDITOR_PATH.length()); if (pagePath.endsWith(".html")) { pagePath = pagePath.substring(0, pagePath.length() - ".html".length()); } CatalogSearchSupport catalogSearch = new CatalogSearchSupport(resolver); String catalogPath = catalogSearch.findCatalogPath(pagePath); String rootCategoryId = catalogSearch.findCategoryId(catalogPath); if (rootCategoryId != null) { gqlPredicateGroup.add(new Predicate(CATEGORY_ID_PARAMETER).set(CATEGORY_ID_PARAMETER, rootCategoryId)); gqlPredicateGroup.add(new Predicate(CATEGORY_PATH_PARAMETER).set(CATEGORY_PATH_PARAMETER, catalogPath)); } } else { LOGGER.warn("The path of the edited page cannot be determined"); } gqlPredicateGroup.add(new Predicate(TypePredicateEvaluator.TYPE).set(TypePredicateEvaluator.TYPE, NT_UNSTRUCTURED)); if (gqlPredicateGroup.get(Predicate.PARAM_LIMIT) == null) { String limit = request.getParameter(LIMIT); if ((limit != null) && (!limit.equals(""))) { int offset = Integer.parseInt(StringUtils.substringBefore(limit, "..")); int total = Integer.parseInt(StringUtils.substringAfter(limit, "..")); gqlPredicateGroup.set(Predicate.PARAM_LIMIT, Long.toString(total - offset)); gqlPredicateGroup.set(Predicate.PARAM_OFFSET, Long.toString(offset)); } else { gqlPredicateGroup.set(Predicate.PARAM_LIMIT, Long.toString(DEFAULT_LIMIT)); } } addProductConstraint(gqlPredicateGroup); if (!predicateSet.contains(Predicate.ORDER_BY)) { gqlPredicateGroup.add(new Predicate(Predicate.ORDER_BY) .set(Predicate.ORDER_BY, "@" + JCR_LASTMODIFIED) .set(Predicate.PARAM_SORT, Predicate.SORT_DESCENDING)); } return new GQLViewQuery(resolver, omniSearchHandler, xssAPI, gqlPredicateGroup); }  }
GraphqlProductViewHandler extends ViewHandler { @Override protected ViewQuery createQuery(SlingHttpServletRequest request, Session session, String queryString) throws RepositoryException { queryString = preserveWildcards(queryString); PredicateGroup gqlPredicateGroup = PredicateConverter.createPredicatesFromGQL(queryString); ResourceResolver resolver = request.getResourceResolver(); tagManager = resolver.adaptTo(TagManager.class); Set<String> predicateSet = customizePredicateGroup(gqlPredicateGroup); RequestPathInfo pathInfo = request.getRequestPathInfo(); CommerceBasePathsService cbps = resolver.adaptTo(CommerceBasePathsService.class); String defaultStartPath = cbps.getProductsBasePath(); String startPath = (pathInfo.getSuffix() != null && pathInfo.getSuffix().startsWith(defaultStartPath)) ? pathInfo.getSuffix() : defaultStartPath; if (!predicateSet.contains(PathPredicateEvaluator.PATH)) { gqlPredicateGroup.add(new Predicate(PathPredicateEvaluator.PATH).set(PathPredicateEvaluator.PATH, startPath)); } String refererHeader = request.getHeader(REFERER_HEADER); if (StringUtils.isNotBlank(refererHeader) && refererHeader.contains(PAGE_EDITOR_PATH + "/")) { int p = refererHeader.lastIndexOf(PAGE_EDITOR_PATH); String pagePath = refererHeader.substring(p + PAGE_EDITOR_PATH.length()); if (pagePath.endsWith(".html")) { pagePath = pagePath.substring(0, pagePath.length() - ".html".length()); } CatalogSearchSupport catalogSearch = new CatalogSearchSupport(resolver); String catalogPath = catalogSearch.findCatalogPath(pagePath); String rootCategoryId = catalogSearch.findCategoryId(catalogPath); if (rootCategoryId != null) { gqlPredicateGroup.add(new Predicate(CATEGORY_ID_PARAMETER).set(CATEGORY_ID_PARAMETER, rootCategoryId)); gqlPredicateGroup.add(new Predicate(CATEGORY_PATH_PARAMETER).set(CATEGORY_PATH_PARAMETER, catalogPath)); } } else { LOGGER.warn("The path of the edited page cannot be determined"); } gqlPredicateGroup.add(new Predicate(TypePredicateEvaluator.TYPE).set(TypePredicateEvaluator.TYPE, NT_UNSTRUCTURED)); if (gqlPredicateGroup.get(Predicate.PARAM_LIMIT) == null) { String limit = request.getParameter(LIMIT); if ((limit != null) && (!limit.equals(""))) { int offset = Integer.parseInt(StringUtils.substringBefore(limit, "..")); int total = Integer.parseInt(StringUtils.substringAfter(limit, "..")); gqlPredicateGroup.set(Predicate.PARAM_LIMIT, Long.toString(total - offset)); gqlPredicateGroup.set(Predicate.PARAM_OFFSET, Long.toString(offset)); } else { gqlPredicateGroup.set(Predicate.PARAM_LIMIT, Long.toString(DEFAULT_LIMIT)); } } addProductConstraint(gqlPredicateGroup); if (!predicateSet.contains(Predicate.ORDER_BY)) { gqlPredicateGroup.add(new Predicate(Predicate.ORDER_BY) .set(Predicate.ORDER_BY, "@" + JCR_LASTMODIFIED) .set(Predicate.PARAM_SORT, Predicate.SORT_DESCENDING)); } return new GQLViewQuery(resolver, omniSearchHandler, xssAPI, gqlPredicateGroup); }   }
GraphqlProductViewHandler extends ViewHandler { @Override protected ViewQuery createQuery(SlingHttpServletRequest request, Session session, String queryString) throws RepositoryException { queryString = preserveWildcards(queryString); PredicateGroup gqlPredicateGroup = PredicateConverter.createPredicatesFromGQL(queryString); ResourceResolver resolver = request.getResourceResolver(); tagManager = resolver.adaptTo(TagManager.class); Set<String> predicateSet = customizePredicateGroup(gqlPredicateGroup); RequestPathInfo pathInfo = request.getRequestPathInfo(); CommerceBasePathsService cbps = resolver.adaptTo(CommerceBasePathsService.class); String defaultStartPath = cbps.getProductsBasePath(); String startPath = (pathInfo.getSuffix() != null && pathInfo.getSuffix().startsWith(defaultStartPath)) ? pathInfo.getSuffix() : defaultStartPath; if (!predicateSet.contains(PathPredicateEvaluator.PATH)) { gqlPredicateGroup.add(new Predicate(PathPredicateEvaluator.PATH).set(PathPredicateEvaluator.PATH, startPath)); } String refererHeader = request.getHeader(REFERER_HEADER); if (StringUtils.isNotBlank(refererHeader) && refererHeader.contains(PAGE_EDITOR_PATH + "/")) { int p = refererHeader.lastIndexOf(PAGE_EDITOR_PATH); String pagePath = refererHeader.substring(p + PAGE_EDITOR_PATH.length()); if (pagePath.endsWith(".html")) { pagePath = pagePath.substring(0, pagePath.length() - ".html".length()); } CatalogSearchSupport catalogSearch = new CatalogSearchSupport(resolver); String catalogPath = catalogSearch.findCatalogPath(pagePath); String rootCategoryId = catalogSearch.findCategoryId(catalogPath); if (rootCategoryId != null) { gqlPredicateGroup.add(new Predicate(CATEGORY_ID_PARAMETER).set(CATEGORY_ID_PARAMETER, rootCategoryId)); gqlPredicateGroup.add(new Predicate(CATEGORY_PATH_PARAMETER).set(CATEGORY_PATH_PARAMETER, catalogPath)); } } else { LOGGER.warn("The path of the edited page cannot be determined"); } gqlPredicateGroup.add(new Predicate(TypePredicateEvaluator.TYPE).set(TypePredicateEvaluator.TYPE, NT_UNSTRUCTURED)); if (gqlPredicateGroup.get(Predicate.PARAM_LIMIT) == null) { String limit = request.getParameter(LIMIT); if ((limit != null) && (!limit.equals(""))) { int offset = Integer.parseInt(StringUtils.substringBefore(limit, "..")); int total = Integer.parseInt(StringUtils.substringAfter(limit, "..")); gqlPredicateGroup.set(Predicate.PARAM_LIMIT, Long.toString(total - offset)); gqlPredicateGroup.set(Predicate.PARAM_OFFSET, Long.toString(offset)); } else { gqlPredicateGroup.set(Predicate.PARAM_LIMIT, Long.toString(DEFAULT_LIMIT)); } } addProductConstraint(gqlPredicateGroup); if (!predicateSet.contains(Predicate.ORDER_BY)) { gqlPredicateGroup.add(new Predicate(Predicate.ORDER_BY) .set(Predicate.ORDER_BY, "@" + JCR_LASTMODIFIED) .set(Predicate.PARAM_SORT, Predicate.SORT_DESCENDING)); } return new GQLViewQuery(resolver, omniSearchHandler, xssAPI, gqlPredicateGroup); }    }