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 boolean appendNegation = _operator == Operator.NE && isTrue || _operator == Operator.EQ && !isTrue; 089 if (appendNegation) 090 { 091 query.append('('); 092 NotQuery.appendNegation(query); 093 } 094 095 query.append(_fieldPath).append("_b:") 096 .append(_operator == Operator.EXISTS ? QueryHelper.EXISTS_VALUE : "true"); 097 098 if (appendNegation) 099 { 100 query.append(')'); 101 } 102 103 return query.toString(); 104 } 105 106 @Override 107 public int hashCode() 108 { 109 final int prime = 31; 110 int result = super.hashCode(); 111 result = prime * result + ((_operator == null) ? 0 : _operator.hashCode()); 112 result = prime * result + ((_value == null) ? 0 : _value.hashCode()); 113 return result; 114 } 115 116 @Override 117 public boolean equals(Object obj) 118 { 119 if (this == obj) 120 { 121 return true; 122 } 123 if (!super.equals(obj)) 124 { 125 return false; 126 } 127 if (getClass() != obj.getClass()) 128 { 129 return false; 130 } 131 BooleanQuery other = (BooleanQuery) obj; 132 if (_operator != other._operator) 133 { 134 return false; 135 } 136 if (_value == null) 137 { 138 if (other._value != null) 139 { 140 return false; 141 } 142 } 143 else if (!_value.equals(other._value)) 144 { 145 return false; 146 } 147 return true; 148 } 149}