001/*
002 *  Copyright 2016 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.ametys.cms.content.indexing.solr.SolrFieldNames;
019
020/**
021 * A query on the document type.
022 */
023public class DocumentTypeQuery implements Query
024{
025    
026    /** The document type. */
027    private String _type;
028    
029    /** <code>false</code> to negate the query. */
030    private boolean _positive;
031    
032    /**
033     * Build a document type query.
034     * @param type The document type to test.
035     */
036    public DocumentTypeQuery(String type)
037    {
038        this(type, true);
039    }
040    
041    /**
042     * Build a document type query.
043     * @param type The document type to test.
044     * @param positiveQuery <code>false</code> to negate the query.
045     */
046    public DocumentTypeQuery(String type, boolean positiveQuery)
047    {
048        this._type = type;
049        this._positive = positiveQuery;
050    }
051    
052    public String build() throws QuerySyntaxException
053    {
054        if (_positive)
055        {
056            return SolrFieldNames.DOCUMENT_TYPE + ":" + _type;
057        }
058        else
059        {
060            // FIXME Yes, really. Doing this is mandatory because pure negative
061            // queries work only as top-level clauses (see solr docs on the Standard Query Parser).
062            return "(" + NotQuery.NEGATION_QUERY_PREFIX + SolrFieldNames.DOCUMENT_TYPE + ":" + _type + ")";
063        }
064    }
065
066    @Override
067    public int hashCode()
068    {
069        final int prime = 31;
070        int result = 1;
071        result = prime * result + (_positive ? 1231 : 1237);
072        result = prime * result + ((_type == null) ? 0 : _type.hashCode());
073        return result;
074    }
075
076    @Override
077    public boolean equals(Object obj)
078    {
079        if (this == obj)
080        {
081            return true;
082        }
083        if (obj == null)
084        {
085            return false;
086        }
087        if (getClass() != obj.getClass())
088        {
089            return false;
090        }
091        DocumentTypeQuery other = (DocumentTypeQuery) obj;
092        if (_positive != other._positive)
093        {
094            return false;
095        }
096        if (_type == null)
097        {
098            if (other._type != null)
099            {
100                return false;
101            }
102        }
103        else if (!_type.equals(other._type))
104        {
105            return false;
106        }
107        return true;
108    }
109}