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.PAGE_ANCESTOR_IDS).append(":\"")
062                 .append(_pageId).append("\")");
063        }
064        
065        return query.toString();
066    }
067
068    @Override
069    public int hashCode()
070    {
071        final int prime = 31;
072        int result = 1;
073        result = prime * result + ((_pageId == null) ? 0 : _pageId.hashCode());
074        result = prime * result + (_withSubPages ? 1231 : 1237);
075        return result;
076    }
077
078    @Override
079    public boolean equals(Object obj)
080    {
081        if (this == obj)
082        {
083            return true;
084        }
085        if (obj == null)
086        {
087            return false;
088        }
089        if (getClass() != obj.getClass())
090        {
091            return false;
092        }
093        PageQuery other = (PageQuery) obj;
094        if (_pageId == null)
095        {
096            if (other._pageId != null)
097            {
098                return false;
099            }
100        }
101        else if (!_pageId.equals(other._pageId))
102        {
103            return false;
104        }
105        if (_withSubPages != other._withSubPages)
106        {
107            return false;
108        }
109        return true;
110    }
111}