001/* 002 * Copyright 2016 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.web.search.query; 017 018import org.ametys.cms.content.indexing.solr.SolrFieldNames; 019import org.ametys.cms.search.query.Query; 020import org.ametys.cms.search.query.QuerySyntaxException; 021import org.ametys.web.indexing.solr.SolrWebFieldNames; 022 023/** 024 * Query testing the id of a page or its ancestor ids (enabling to search the subpages of a given one). 025 */ 026public class PageQuery implements Query 027{ 028 /** The id. */ 029 protected String _pageId; 030 /** True to search for a whole hierarchy. */ 031 protected boolean _withSubPages; 032 033 /** 034 * Create a PageQuery which searches on the given page (and optionally its sub-pages). 035 * @param pageId the page id. 036 * @param withSubPages true to search for sub-pages of the given page id too. 037 */ 038 public PageQuery(String pageId, boolean withSubPages) 039 { 040 _pageId = pageId; 041 _withSubPages = withSubPages; 042 } 043 044 @Override 045 public String build() throws QuerySyntaxException 046 { 047 StringBuilder query = new StringBuilder(); 048 049 if (_withSubPages) 050 { 051 query.append('('); 052 } 053 054 query.append(SolrFieldNames.ID) 055 .append(":\"") 056 .append(_pageId) 057 .append("\""); 058 059 if (_withSubPages) 060 { 061 query.append(" OR ").append(SolrWebFieldNames.ANCESTOR_IDS).append(":\"") 062 .append(_pageId).append("\")"); 063 } 064 065 return query.toString(); 066 } 067}