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 "(*:* AND -" + SolrFieldNames.DOCUMENT_TYPE + ":" + _type + ")";
063        }
064    }
065
066}