001/*
002 *  Copyright 2019 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 java.util.Objects;
019
020import org.ametys.cms.search.query.Query;
021import org.ametys.cms.search.query.QuerySyntaxException;
022import org.ametys.web.indexing.solr.SolrWebFieldNames;
023
024/**
025 * Query testing if the Page is a descendant (i.e., child, child of a child, etc.) of another given one.
026 */
027public class DescendantPageQuery implements Query
028{
029    /** The id. */
030    protected String _ancestorPageId;
031    
032    /**
033     * Creates a DescendantPageQuery
034     * @param ancestorPageId The ancestor page id
035     */
036    public DescendantPageQuery(String ancestorPageId)
037    {
038        _ancestorPageId = ancestorPageId;
039    }
040    
041    @Override
042    public String build() throws QuerySyntaxException
043    {
044        StringBuilder sb = new StringBuilder(SolrWebFieldNames.PAGE_ANCESTOR_IDS)
045                .append(":\"")
046                .append(_ancestorPageId)
047                .append("\"");
048        return sb.toString();
049    }
050
051    @Override
052    public int hashCode()
053    {
054        return Objects.hash(_ancestorPageId);
055    }
056
057    @Override
058    public boolean equals(Object obj)
059    {
060        if (this == obj)
061        {
062            return true;
063        }
064        if (obj == null)
065        {
066            return false;
067        }
068        if (getClass() != obj.getClass())
069        {
070            return false;
071        }
072        DescendantPageQuery other = (DescendantPageQuery) obj;
073        return Objects.equals(_ancestorPageId, other._ancestorPageId);
074    }
075}
076