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.search.solr.field.ContentPrivacySearchField;
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 && Operator.EXISTS != 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
088        if (_operator == Operator.EXISTS)
089        {
090            buff.append(ContentPrivacySearchField.NAME).append(":*");
091        }
092        else
093        {
094            if (_operator == Operator.NE)
095            {
096                NotQuery.appendNegation(buff);
097            }
098            if (count > 1)
099            {
100                buff.append('(');
101            }
102            
103            boolean first = true;
104            for (String value : _values)
105            {
106                if (!first)
107                {
108                    buff.append(" OR ");
109                }
110                buff.append(ContentPrivacySearchField.NAME).append(':').append(value);
111                first = false;
112            }
113            
114            if (count > 1)
115            {
116                buff.append(')');
117            }
118        }
119        
120        return buff.toString();
121    }
122
123    @Override
124    public int hashCode()
125    {
126        final int prime = 31;
127        int result = 1;
128        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
129        result = prime * result + ((_values == null) ? 0 : _values.hashCode());
130        return result;
131    }
132
133    @Override
134    public boolean equals(Object obj)
135    {
136        if (this == obj)
137        {
138            return true;
139        }
140        if (obj == null)
141        {
142            return false;
143        }
144        if (getClass() != obj.getClass())
145        {
146            return false;
147        }
148        ContentPrivacyQuery other = (ContentPrivacyQuery) obj;
149        if (_operator != other._operator)
150        {
151            return false;
152        }
153        if (_values == null)
154        {
155            if (other._values != null)
156            {
157                return false;
158            }
159        }
160        else if (!_values.equals(other._values))
161        {
162            return false;
163        }
164        return true;
165    }
166}