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.Query;
024import org.ametys.cms.search.query.QuerySyntaxException;
025import org.ametys.web.indexing.solr.SolrWebFieldNames;
026
027/**
028 * Represents a {@link Query} testing the page property of a content or resource.
029 */
030public class PageIdQuery implements Query
031{
032    
033    private Operator _operator;
034    private List<String> _ids;
035    
036    /**
037     * Build a PageIdQuery.
038     * @param ids the page ids.
039     */
040    public PageIdQuery(String... ids)
041    {
042        this(Operator.EQ, ids);
043    }
044    
045    /**
046     * Build a PageIdQuery.
047     * @param ids the page ids.
048     */
049    public PageIdQuery(Collection<String> ids)
050    {
051        this(Operator.EQ, ids);
052    }
053    
054    /**
055     * Build a PageIdQuery.
056     * @param operator the operator.
057     * @param ids the page ids.
058     */
059    public PageIdQuery(Operator operator, String... ids)
060    {
061        this(operator, Arrays.asList(ids));
062    }
063    
064    /**
065     * Build a SiteQuery.
066     * @param operator the operator.
067     * @param ids the site names.
068     */
069    public PageIdQuery(Operator operator, Collection<String> ids)
070    {
071        if (Operator.EQ != operator && Operator.NE != operator)
072        {
073            throw new IllegalArgumentException("Test operator '" + operator + "' is invalid for a PageIdQuery.");
074        }
075        
076        _operator = operator;
077        _ids = new ArrayList<>(ids);
078    }
079    
080    @Override
081    public String build() throws QuerySyntaxException
082    {
083        int count = _ids.size();
084        
085        StringBuilder query = new StringBuilder();
086        
087        if (_operator == Operator.NE)
088        {
089            query.append('-');
090        }
091        if (count > 1)
092        {
093            query.append('(');
094        }
095        
096        boolean first = true;
097        for (String id : _ids)
098        {
099            if (!first)
100            {
101                query.append(" OR ");
102            }
103            query.append(SolrWebFieldNames.PAGE_IDS).append(":\"").append(id).append('"');
104            first = false;
105        }
106        
107        if (count > 1)
108        {
109            query.append(')');
110        }
111        
112        return query.toString();
113    }
114    
115}