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