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