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 java.util.ArrayList;
019import java.util.Arrays;
020import java.util.Collection;
021import java.util.List;
022
023import org.ametys.cms.search.query.NotQuery;
024import org.ametys.cms.search.query.Query;
025import org.ametys.cms.search.query.QuerySyntaxException;
026import org.ametys.web.indexing.solr.SolrWebFieldNames;
027
028/**
029 * Represents a {@link Query} testing the page property of a content or resource.
030 */
031public class PageIdQuery implements Query
032{
033    
034    private Operator _operator;
035    private List<String> _ids;
036    
037    /**
038     * Build a PageIdQuery.
039     * @param ids the page ids.
040     */
041    public PageIdQuery(String... ids)
042    {
043        this(Operator.EQ, ids);
044    }
045    
046    /**
047     * Build a PageIdQuery.
048     * @param ids the page ids.
049     */
050    public PageIdQuery(Collection<String> ids)
051    {
052        this(Operator.EQ, ids);
053    }
054    
055    /**
056     * Build a PageIdQuery.
057     * @param operator the operator.
058     * @param ids the page ids.
059     */
060    public PageIdQuery(Operator operator, String... ids)
061    {
062        this(operator, Arrays.asList(ids));
063    }
064    
065    /**
066     * Build a SiteQuery.
067     * @param operator the operator.
068     * @param ids the site names.
069     */
070    public PageIdQuery(Operator operator, Collection<String> ids)
071    {
072        if (Operator.EQ != operator && Operator.NE != operator)
073        {
074            throw new IllegalArgumentException("Test operator '" + operator + "' is invalid for a PageIdQuery.");
075        }
076        
077        _operator = operator;
078        _ids = new ArrayList<>(ids);
079    }
080    
081    @Override
082    public String build() throws QuerySyntaxException
083    {
084        int count = _ids.size();
085        
086        StringBuilder query = new StringBuilder();
087        
088        if (_operator == Operator.NE)
089        {
090            NotQuery.appendNegation(query);
091        }
092        if (count > 1)
093        {
094            query.append('(');
095        }
096        
097        boolean first = true;
098        for (String id : _ids)
099        {
100            if (!first)
101            {
102                query.append(" OR ");
103            }
104            query.append(SolrWebFieldNames.PAGE_IDS).append(":\"").append(id).append('"');
105            first = false;
106        }
107        
108        if (count > 1)
109        {
110            query.append(')');
111        }
112        
113        return query.toString();
114    }
115
116    @Override
117    public int hashCode()
118    {
119        final int prime = 31;
120        int result = 1;
121        result = prime * result + ((_ids == null) ? 0 : _ids.hashCode());
122        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
123        return result;
124    }
125
126    @Override
127    public boolean equals(Object obj)
128    {
129        if (this == obj)
130        {
131            return true;
132        }
133        if (obj == null)
134        {
135            return false;
136        }
137        if (getClass() != obj.getClass())
138        {
139            return false;
140        }
141        PageIdQuery other = (PageIdQuery) obj;
142        if (_ids == null)
143        {
144            if (other._ids != null)
145            {
146                return false;
147            }
148        }
149        else if (!_ids.equals(other._ids))
150        {
151            return false;
152        }
153        if (_operator != other._operator)
154        {
155            return false;
156        }
157        return true;
158    }
159}