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.Query;
023import org.ametys.cms.search.query.QuerySyntaxException;
024import org.ametys.web.indexing.solr.SolrWebFieldNames;
025
026/**
027 * Represents a {@link Query} testing the content privacy property.
028 */
029public class ContentPrivacyQuery implements Query
030{
031    
032    private Operator _operator;
033    private Collection<String> _values;
034    
035    /**
036     * Build a ContentPrivacyQuery.
037     * @param values the site names.
038     */
039    public ContentPrivacyQuery(String... values)
040    {
041        this(Operator.EQ, values);
042    }
043    
044    /**
045     * Build a ContentPrivacyQuery.
046     * @param values the site names.
047     */
048    public ContentPrivacyQuery(Collection<String> values)
049    {
050        this(Operator.EQ, values);
051    }
052    
053    /**
054     * Build a ContentPrivacyQuery.
055     * @param operator the operator.
056     * @param values the site names.
057     */
058    public ContentPrivacyQuery(Operator operator, String... values)
059    {
060        this(operator, Arrays.asList(values));
061    }
062    
063    /**
064     * Build a ContentPrivacyQuery.
065     * @param operator the operator.
066     * @param values the site names.
067     */
068    public ContentPrivacyQuery(Operator operator, Collection<String> values)
069    {
070        if (Operator.EQ != operator && Operator.NE != operator)
071        {
072            throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for test's expression.");
073        }
074        
075        _operator = operator;
076        _values = new HashSet<>(values);
077    }
078    
079    @Override
080    public String build() throws QuerySyntaxException
081    {
082        int count = _values.size();
083        
084        StringBuilder buff = new StringBuilder();
085        
086        if (_operator == Operator.NE)
087        {
088            buff.append('-');
089        }
090        if (count > 1)
091        {
092            buff.append('(');
093        }
094        
095        boolean first = true;
096        for (String value : _values)
097        {
098            if (!first)
099            {
100                buff.append(" OR ");
101            }
102            buff.append(SolrWebFieldNames.PRIVACY).append(':').append(value);
103            first = false;
104        }
105        
106        if (count > 1)
107        {
108            buff.append(')');
109        }
110        
111        return buff.toString();
112    }
113    
114}