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.Query;
025import org.ametys.cms.search.query.QuerySyntaxException;
026import org.ametys.web.indexing.solr.SolrWebFieldNames;
027
028/**
029 * Represents a {@link Query} testing the site property.
030 */
031public class SiteQuery implements Query
032{
033    private Operator _operator;
034    private List<String> _names;
035    
036    
037    /**
038     * Build a SiteQuery to test if the site property exits
039     */
040    public SiteQuery()
041    {
042        this(Operator.EQ, "*");
043    }
044    
045    /**
046     * Build a SiteQuery to test if the site property is equals to one of the given site names
047     * @param names the site names.
048     */
049    public SiteQuery(String... names)
050    {
051        this(Operator.EQ, names);
052    }
053    
054    /**
055     * Build a SiteQuery to test if the site property is equals to one of the given site names
056     * @param names the site names.
057     */
058    public SiteQuery(Collection<String> names)
059    {
060        this(Operator.EQ, names);
061    }
062    
063    /**
064     * Build a SiteQuery to test if the site property is equals or different to one of the given site names
065     * @param operator the operator (equals ot not-equals)
066     * @param names the site names.
067     */
068    public SiteQuery(Operator operator, String... names)
069    {
070        this(operator, Arrays.asList(names));
071    }
072    
073    /**
074     * Build a SiteQuery.
075     * @param operator the operator.
076     * @param names the site names.
077     */
078    public SiteQuery(Operator operator, Collection<String> names)
079    {
080        if (Operator.EQ != operator && Operator.NE != operator)
081        {
082            throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for test's expression.");
083        }
084        
085        _operator = operator;
086        _names = new ArrayList<>(names);
087    }
088    
089    /**
090     * Get the operator.
091     * @return the operator
092     */
093    public Operator getOperator()
094    {
095        return _operator;
096    }
097    
098    /**
099     * Get the site names.
100     * @return the site names.
101     */
102    public List<String> getNames()
103    {
104        return Collections.unmodifiableList(_names);
105    }
106    
107    @Override
108    public String build() throws QuerySyntaxException
109    {
110        int count = _names.size();
111        
112        StringBuilder buff = new StringBuilder();
113        
114        if (_operator == Operator.NE)
115        {
116            buff.append('-');
117        }
118        if (count > 1)
119        {
120            buff.append('(');
121        }
122        
123        boolean first = true;
124        for (String id : _names)
125        {
126            if (!first)
127            {
128                buff.append(" OR ");
129            }
130            buff.append(SolrWebFieldNames.SITE_NAME).append(':').append(id);
131            first = false;
132        }
133        
134        if (count > 1)
135        {
136            buff.append(')');
137        }
138        
139        return buff.toString();
140    }
141    
142}