001/*
002 *  Copyright 2014 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.cms.search.query;
017
018import org.apache.solr.client.solrj.util.ClientUtils;
019
020import org.ametys.cms.content.indexing.solr.SolrFieldNames;
021
022/**
023 * Represents a {@link Query} testing the content language.
024 */
025public class ContentLanguageQuery implements Query
026{
027    
028    /** The operator (can only be EQ or NE). */
029    private Operator _operator;
030    /** The language values to test. */
031    private String[] _values;
032    /** The logical operator between the values. */
033    private LogicalOperator _logicalOperator;
034    
035    /**
036     * Build a ContentLanguageQuery.
037     * @param value the language equality to test.
038     */
039    public ContentLanguageQuery(String value)
040    {
041        this(Operator.EQ, value);
042    }
043    
044    /**
045     * Build a ContentLanguageQuery.
046     * @param values the languages to test.
047     */
048    public ContentLanguageQuery(String... values)
049    {
050        this(Operator.EQ, values);
051    }
052    
053    /**
054     * Build a ContentLanguageQuery.
055     * @param operator the operator.
056     * @param value the language code.
057     */
058    public ContentLanguageQuery(Operator operator, String value)
059    {
060        this(operator, LogicalOperator.OR, new String[] {value});
061    }
062    
063    /**
064     * Build a ContentLanguageQuery.
065     * @param operator the operator.
066     * @param values the language codes.
067     */
068    public ContentLanguageQuery(Operator operator, String... values)
069    {
070        this(operator, LogicalOperator.OR, values);
071    }
072    
073    /**
074     * Build a ContentLanguageQuery.
075     * @param operator the operator.
076     * @param logicalOperator the logical operator.
077     * @param values the language codes.
078     */
079    public ContentLanguageQuery(Operator operator, LogicalOperator logicalOperator, String... values)
080    {
081        if (Operator.EQ != operator && Operator.NE != operator)
082        {
083            throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for test's expression.");
084        }
085        
086        _operator = operator;
087        _logicalOperator = logicalOperator;
088        _values = values;
089    }
090    
091    /**
092     * Get the operator.
093     * @return the operator
094     */
095    public Operator getOperator()
096    {
097        return _operator;
098    }
099    
100    /**
101     * Get the values.
102     * @return the values
103     */
104    public String[] getValues()
105    {
106        return _values;
107    }
108    
109    /**
110     * Get the logical operator.
111     * @return the logical operator.
112     */
113    public LogicalOperator getLogicalOperator()
114    {
115        return _logicalOperator;
116    }
117    
118    @Override
119    public String build() throws QuerySyntaxException
120    {
121        StringBuilder query = new StringBuilder();
122        
123        if (_values.length > 0)
124        {
125            if (_values.length > 1)
126            {
127                query.append('(');
128            }
129            for (int i = 0; i < _values.length; i++)
130            {
131                if (i > 0)
132                {
133                    query.append(' ').append(_logicalOperator).append(' ');
134                }
135                
136                if (_operator == Operator.NE)
137                {
138                    query.append('-');
139                }
140                
141                query.append(SolrFieldNames.CONTENT_LANGUAGE).append(":\"").append(ClientUtils.escapeQueryChars(_values[i])).append('"');
142            }
143            if (_values.length > 1)
144            {
145                query.append(')');
146            }
147        }
148        
149        return query.toString();
150    }
151    
152}