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