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 direct child of another given one.
026 */
027public class ChildPageQuery implements Query
028{
029    /** The id. */
030    protected String _parentPageId;
031    
032    /**
033     * Creates a ChildPageQuery
034     * @param parentPageId The parent page id
035     */
036    public ChildPageQuery(String parentPageId)
037    {
038        _parentPageId = parentPageId;
039    }
040    
041    @Override
042    public String build() throws QuerySyntaxException
043    {
044        StringBuilder sb = new StringBuilder(SolrWebFieldNames.PAGE_PARENT_ID)
045                .append(":\"")
046                .append(_parentPageId)
047                .append("\"");
048        return sb.toString();
049    }
050
051    @Override
052    public int hashCode()
053    {
054        return Objects.hash(_parentPageId);
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        ChildPageQuery other = (ChildPageQuery) obj;
073        return Objects.equals(_parentPageId, other._parentPageId);
074    }
075}
076