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 org.apache.commons.lang3.BooleanUtils;
019
020/**
021 * Represents a {@link Query} testing a boolean field.
022 */
023public class BooleanQuery extends AbstractFieldQuery
024{
025    /** The operator. */
026    protected Operator _operator;
027    /** The boolean value to test. */
028    protected Boolean _value;
029    
030    /**
031     * Build a BooleanQuery testing the existence of the field.
032     * @param fieldPath the field's path
033     */
034    public BooleanQuery(String fieldPath)
035    {
036        this(fieldPath, Operator.EXISTS, null);
037    }
038    
039    /**
040     * Build a BooleanQuery.
041     * @param fieldPath the field's path
042     * @param value the value.
043     */
044    public BooleanQuery(String fieldPath, Boolean value)
045    {
046        this(fieldPath, Operator.EQ, value);
047    }
048    
049    /**
050     * Build a BooleanQuery.
051     * @param fieldPath the field's path
052     * @param op the operator.
053     * @param value the value.
054     */
055    public BooleanQuery(String fieldPath, Operator op, Boolean value)
056    {
057        super(fieldPath);
058        _operator = op;
059        _value = value;
060    }
061    
062    /**
063     * Get the operator.
064     * @return the operator.
065     */
066    public Operator getOperator()
067    {
068        return _operator;
069    }
070    
071    /**
072     * Get the value.
073     * @return the value.
074     */
075    public boolean getValue()
076    {
077        return _value;
078    }
079    
080    @Override
081    public String build() throws QuerySyntaxException
082    {
083        Boolean isTrue = BooleanUtils.isTrue(_value);
084        
085        StringBuilder query = new StringBuilder();
086        
087        // "field != true" or "field == false" => test "not true".
088        if (_operator == Operator.NE && isTrue || _operator == Operator.EQ && !isTrue)
089        {
090            NotQuery.appendNegation(query);
091        }
092        
093        query.append(_fieldPath).append("_b:")
094             .append(_operator == Operator.EXISTS ? QueryHelper.EXISTS_VALUE : "true");
095        
096        return query.toString();
097    }
098
099    @Override
100    public int hashCode()
101    {
102        final int prime = 31;
103        int result = super.hashCode();
104        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
105        result = prime * result + ((_value == null) ? 0 : _value.hashCode());
106        return result;
107    }
108
109    @Override
110    public boolean equals(Object obj)
111    {
112        if (this == obj)
113        {
114            return true;
115        }
116        if (!super.equals(obj))
117        {
118            return false;
119        }
120        if (getClass() != obj.getClass())
121        {
122            return false;
123        }
124        BooleanQuery other = (BooleanQuery) obj;
125        if (_operator != other._operator)
126        {
127            return false;
128        }
129        if (_value == null)
130        {
131            if (other._value != null)
132            {
133                return false;
134            }
135        }
136        else if (!_value.equals(other._value))
137        {
138            return false;
139        }
140        return true;
141    }
142}