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 operator. */
029    protected Operator _operator;
030    /** The id. */
031    protected String _pageId;
032    /** True to search for a whole hierarchy. */
033    protected boolean _withSubPages;
034
035    /**
036     * Create a PageQuery which searches on the given page (and optionally its sub-pages).
037     * @param withSubPages true to search for sub-pages too.
038     */
039    public PageQuery(boolean withSubPages)
040    {
041        _withSubPages = withSubPages;
042        _operator = Operator.EXISTS;
043    }
044    
045    /**
046     * Create a PageQuery which searches on the given page (and optionally its sub-pages).
047     * @param pageId the page id.
048     * @param withSubPages true to search for sub-pages of the given page id too.
049     */
050    public PageQuery(String pageId, boolean withSubPages)
051    {
052        _pageId = pageId;
053        _withSubPages = withSubPages;
054        _operator = Operator.EQ;
055    }
056    
057    @Override
058    public String build() throws QuerySyntaxException
059    {
060        StringBuilder query = new StringBuilder();
061
062        if (_operator == Operator.EXISTS)
063        {
064            if (_withSubPages)
065            {
066                query.append('(');
067            }
068            
069            query.append(SolrFieldNames.ID).append(":*");
070            
071            if (_withSubPages)
072            {
073                query.append(" OR ").append(SolrWebFieldNames.PAGE_ANCESTOR_IDS).append(":*)");
074            }
075        }
076        else
077        {
078            if (_withSubPages)
079            {
080                query.append('(');
081            }
082            
083            query.append(SolrFieldNames.ID)
084                 .append(":\"")
085                 .append(_pageId)
086                 .append("\"");
087            
088            if (_withSubPages)
089            {
090                query.append(" OR ").append(SolrWebFieldNames.PAGE_ANCESTOR_IDS).append(":\"")
091                .append(_pageId).append("\")");
092            }
093        }
094        
095        return query.toString();
096    }
097
098    @Override
099    public int hashCode()
100    {
101        final int prime = 31;
102        int result = 1;
103        result = prime * result + ((_pageId == null) ? 0 : _pageId.hashCode());
104        result = prime * result + (_withSubPages ? 1231 : 1237);
105        return result;
106    }
107
108    @Override
109    public boolean equals(Object obj)
110    {
111        if (this == obj)
112        {
113            return true;
114        }
115        if (obj == null)
116        {
117            return false;
118        }
119        if (getClass() != obj.getClass())
120        {
121            return false;
122        }
123        PageQuery other = (PageQuery) obj;
124        if (_pageId == null)
125        {
126            if (other._pageId != null)
127            {
128                return false;
129            }
130        }
131        else if (!_pageId.equals(other._pageId))
132        {
133            return false;
134        }
135        if (_withSubPages != other._withSubPages)
136        {
137            return false;
138        }
139        return true;
140    }
141}