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 or the mixins of contents.
028 */
029public class ContentTypeOrMixinTypeQuery implements Query
030{
031    
032    private Operator _operator;
033    private List<String> _ids;
034    
035    /**
036     * Build a ContentTypeOrMixinTypeQuery.
037     * @param ids the content type ids.
038     */
039    public ContentTypeOrMixinTypeQuery(String... ids)
040    {
041        this(Operator.EQ, ids);
042    }
043    
044    /**
045     * Build a ContentTypeOrMixinTypeQuery.
046     * @param ids the content type ids.
047     */
048    public ContentTypeOrMixinTypeQuery(Collection<String> ids)
049    {
050        this(Operator.EQ, ids);
051    }
052    
053    /**
054     * Build a ContentTypeOrMixinTypeQuery.
055     * @param operator the operator.
056     * @param ids the content type ids.
057     */
058    public ContentTypeOrMixinTypeQuery(Operator operator, String... ids)
059    {
060        this(operator, Arrays.asList(ids));
061    }
062    
063    /**
064     * Build a ContentTypeOrMixinTypeQuery.
065     * @param operator the operator.
066     * @param ids the content type ids.
067     */
068    public ContentTypeOrMixinTypeQuery(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 or mixin ids.
090     * @return the content type or mixin 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        StringBuilder query = new StringBuilder();
101        
102        if (_operator == Operator.EXISTS)
103        {
104            query.append(SolrFieldNames.ALL_CONTENT_TYPES).append(":").append(QueryHelper.EXISTS_VALUE)
105            .append(" OR ").append(SolrFieldNames.ALL_MIXIN_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        query.append('(');
115        
116        boolean first = true;
117        for (String id : _ids)
118        {
119            if (!first)
120            {
121                query.append(" OR ");
122            }
123            query.append(SolrFieldNames.ALL_CONTENT_TYPES).append(":").append(id)
124                .append(" OR ").append(SolrFieldNames.ALL_MIXIN_TYPES).append(":").append(id);
125            first = false;
126        }
127        
128        query.append(')');
129        
130        return query.toString();
131    }
132
133    @Override
134    public int hashCode()
135    {
136        final int prime = 31;
137        int result = 1;
138        result = prime * result + ((_ids == null) ? 0 : _ids.hashCode());
139        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
140        return result;
141    }
142
143    @Override
144    public boolean equals(Object obj)
145    {
146        if (this == obj)
147        {
148            return true;
149        }
150        if (obj == null)
151        {
152            return false;
153        }
154        if (getClass() != obj.getClass())
155        {
156            return false;
157        }
158        ContentTypeOrMixinTypeQuery other = (ContentTypeOrMixinTypeQuery) obj;
159        if (_ids == null)
160        {
161            if (other._ids != null)
162            {
163                return false;
164            }
165        }
166        else if (!_ids.equals(other._ids))
167        {
168            return false;
169        }
170        if (_operator != other._operator)
171        {
172            return false;
173        }
174        return true;
175    }
176}