001/*
002 *  Copyright 2015 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.Collections;
022import java.util.List;
023
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 sitemap property.
030 */
031public class SitemapQuery implements Query
032{
033    
034    private Operator _operator;
035    private List<String> _names;
036    
037    /**
038     * Build a SitemapQuery.
039     * @param names the site names.
040     */
041    public SitemapQuery(String... names)
042    {
043        this(Operator.EQ, names);
044    }
045    
046    /**
047     * Build a SitemapQuery.
048     * @param names the site names.
049     */
050    public SitemapQuery(Collection<String> names)
051    {
052        this(Operator.EQ, names);
053    }
054    
055    /**
056     * Build a SitemapQuery.
057     * @param operator the operator.
058     * @param names the site names.
059     */
060    public SitemapQuery(Operator operator, String... names)
061    {
062        this(operator, Arrays.asList(names));
063    }
064    
065    /**
066     * Build a SitemapQuery.
067     * @param operator the operator.
068     * @param names the site names.
069     */
070    public SitemapQuery(Operator operator, Collection<String> names)
071    {
072        if (Operator.EQ != operator && Operator.NE != operator)
073        {
074            throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for test's expression.");
075        }
076        
077        _operator = operator;
078        _names = new ArrayList<>(names);
079    }
080    
081    /**
082     * Get the operator.
083     * @return the operator
084     */
085    public Operator getOperator()
086    {
087        return _operator;
088    }
089    
090    /**
091     * Get the site names.
092     * @return the site names.
093     */
094    public List<String> getNames()
095    {
096        return Collections.unmodifiableList(_names);
097    }
098    
099    @Override
100    public String build() throws QuerySyntaxException
101    {
102        int count = _names.size();
103        
104        StringBuilder buff = new StringBuilder();
105        
106        if (_operator == Operator.NE)
107        {
108            buff.append('-');
109        }
110        if (count > 1)
111        {
112            buff.append('(');
113        }
114        
115        boolean first = true;
116        for (String id : _names)
117        {
118            if (!first)
119            {
120                buff.append(" OR ");
121            }
122            buff.append(SolrWebFieldNames.SITEMAP_NAME).append(':').append(id);
123            first = false;
124        }
125        
126        if (count > 1)
127        {
128            buff.append(')');
129        }
130        
131        return buff.toString();
132    }
133    
134}