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 java.util.ArrayList;
019import java.util.Arrays;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.List;
023
024import org.ametys.cms.content.indexing.solr.SolrFieldNames;
025
026/**
027 * Represents a {@link Query} testing the content types of contents.
028 */
029public class ContentTypeQuery implements Query
030{
031    
032    private Operator _operator;
033    private List<String> _ids;
034    
035    /**
036     * Build a ContentTypeQuery.
037     * @param ids the content type ids.
038     */
039    public ContentTypeQuery(String... ids)
040    {
041        this(Operator.EQ, ids);
042    }
043    
044    /**
045     * Build a ContentTypeQuery.
046     * @param ids the content type ids.
047     */
048    public ContentTypeQuery(Collection<String> ids)
049    {
050        this(Operator.EQ, ids);
051    }
052    
053    /**
054     * Build a ContentTypeQuery.
055     * @param operator the operator.
056     * @param ids the content type ids.
057     */
058    public ContentTypeQuery(Operator operator, String... ids)
059    {
060        this(operator, Arrays.asList(ids));
061    }
062    
063    /**
064     * Build a ContentTypeQuery.
065     * @param operator the operator.
066     * @param ids the content type ids.
067     */
068    public ContentTypeQuery(Operator operator, Collection<String> ids)
069    {
070        if (Operator.EQ != operator && Operator.NE != operator && Operator.EXISTS != operator)
071        {
072            throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for test's expression.");
073        }
074        
075        _operator = operator;
076        _ids = new ArrayList<>(ids);
077    }
078    
079    /**
080     * Get the operator.
081     * @return the operator
082     */
083    public Operator getOperator()
084    {
085        return _operator;
086    }
087    
088    /**
089     * Get the content type ids.
090     * @return the content type ids.
091     */
092    public List<String> getIds()
093    {
094        return Collections.unmodifiableList(_ids);
095    }
096    
097    @Override
098    public String build() throws QuerySyntaxException
099    {
100        
101        StringBuilder query = new StringBuilder();
102
103        if (_operator == Operator.EXISTS)
104        {
105            query.append(SolrFieldNames.ALL_CONTENT_TYPES).append(":").append(QueryHelper.EXISTS_VALUE);
106            
107            return query.toString();
108        }
109        
110        if (_operator == Operator.NE)
111        {
112            NotQuery.appendNegation(query);
113        }
114        
115        if (_ids.contains(""))
116        {
117            query.append(SolrFieldNames.ALL_CONTENT_TYPES).append(":*");
118        }
119        else
120        {
121            int count = _ids.size();
122            if (count > 1)
123            {
124                query.append('(');
125            }
126
127            boolean first = true;
128            for (String id : _ids)
129            {
130                if (!first)
131                {
132                    query.append(" OR ");
133                }
134                query.append(SolrFieldNames.ALL_CONTENT_TYPES).append(':').append(id);
135                first = false;
136            }
137            
138            if (count > 1)
139            {
140                query.append(')');
141            }
142        }
143        
144        return query.toString();
145    }
146
147    @Override
148    public int hashCode()
149    {
150        final int prime = 31;
151        int result = 1;
152        result = prime * result + ((_ids == null) ? 0 : _ids.hashCode());
153        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
154        return result;
155    }
156
157    @Override
158    public boolean equals(Object obj)
159    {
160        if (this == obj)
161        {
162            return true;
163        }
164        if (obj == null)
165        {
166            return false;
167        }
168        if (getClass() != obj.getClass())
169        {
170            return false;
171        }
172        ContentTypeQuery other = (ContentTypeQuery) obj;
173        if (_ids == null)
174        {
175            if (other._ids != null)
176            {
177                return false;
178            }
179        }
180        else if (!_ids.equals(other._ids))
181        {
182            return false;
183        }
184        if (_operator != other._operator)
185        {
186            return false;
187        }
188        return true;
189    }
190}
191