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)
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.NE)
103        {
104            NotQuery.appendNegation(query);
105        }
106        query.append('(');
107        
108        boolean first = true;
109        for (String id : _ids)
110        {
111            if (!first)
112            {
113                query.append(" OR ");
114            }
115            query.append(SolrFieldNames.ALL_CONTENT_TYPES).append(":").append(id)
116                .append(" OR ").append(SolrFieldNames.ALL_MIXIN_TYPES).append(":").append(id);
117            first = false;
118        }
119        
120        query.append(')');
121        
122        return query.toString();
123    }
124
125    @Override
126    public int hashCode()
127    {
128        final int prime = 31;
129        int result = 1;
130        result = prime * result + ((_ids == null) ? 0 : _ids.hashCode());
131        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
132        return result;
133    }
134
135    @Override
136    public boolean equals(Object obj)
137    {
138        if (this == obj)
139        {
140            return true;
141        }
142        if (obj == null)
143        {
144            return false;
145        }
146        if (getClass() != obj.getClass())
147        {
148            return false;
149        }
150        ContentTypeOrMixinTypeQuery other = (ContentTypeOrMixinTypeQuery) obj;
151        if (_ids == null)
152        {
153            if (other._ids != null)
154            {
155                return false;
156            }
157        }
158        else if (!_ids.equals(other._ids))
159        {
160            return false;
161        }
162        if (_operator != other._operator)
163        {
164            return false;
165        }
166        return true;
167    }
168}