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.Arrays; 019 020import org.apache.solr.client.solrj.util.ClientUtils; 021 022import org.ametys.cms.content.indexing.solr.SolrFieldNames; 023 024/** 025 * Represents a {@link Query} testing the content language. 026 */ 027public class ContentLanguageQuery implements Query 028{ 029 030 /** The operator (can only be EQ or NE). */ 031 private Operator _operator; 032 /** The language values to test. */ 033 private String[] _values; 034 /** The logical operator between the values. */ 035 private LogicalOperator _logicalOperator; 036 037 /** 038 * Build a ContentLanguageQuery. 039 * @param value the language equality to test. 040 */ 041 public ContentLanguageQuery(String value) 042 { 043 this(Operator.EQ, value); 044 } 045 046 /** 047 * Build a ContentLanguageQuery. 048 * @param values the languages to test. 049 */ 050 public ContentLanguageQuery(String... values) 051 { 052 this(Operator.EQ, values); 053 } 054 055 /** 056 * Build a ContentLanguageQuery. 057 * @param operator the operator. 058 * @param value the language code. 059 */ 060 public ContentLanguageQuery(Operator operator, String value) 061 { 062 this(operator, LogicalOperator.OR, new String[] {value}); 063 } 064 065 /** 066 * Build a ContentLanguageQuery. 067 * @param operator the operator. 068 * @param values the language codes. 069 */ 070 public ContentLanguageQuery(Operator operator, String... values) 071 { 072 this(operator, LogicalOperator.OR, values); 073 } 074 075 /** 076 * Build a ContentLanguageQuery. 077 * @param operator the operator. 078 * @param logicalOperator the logical operator. 079 * @param values the language codes. 080 */ 081 public ContentLanguageQuery(Operator operator, LogicalOperator logicalOperator, String... values) 082 { 083 if (Operator.EQ != operator && Operator.NE != operator) 084 { 085 throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for test's expression."); 086 } 087 088 _operator = operator; 089 _logicalOperator = logicalOperator; 090 _values = values; 091 } 092 093 /** 094 * Get the operator. 095 * @return the operator 096 */ 097 public Operator getOperator() 098 { 099 return _operator; 100 } 101 102 /** 103 * Get the values. 104 * @return the values 105 */ 106 public String[] getValues() 107 { 108 return _values; 109 } 110 111 /** 112 * Get the logical operator. 113 * @return the logical operator. 114 */ 115 public LogicalOperator getLogicalOperator() 116 { 117 return _logicalOperator; 118 } 119 120 @Override 121 public String build() throws QuerySyntaxException 122 { 123 StringBuilder query = new StringBuilder(); 124 125 if (_values.length > 0) 126 { 127 if (_values.length > 1) 128 { 129 query.append('('); 130 } 131 for (int i = 0; i < _values.length; i++) 132 { 133 if (i > 0) 134 { 135 query.append(' ').append(_logicalOperator).append(' '); 136 } 137 138 if (_operator == Operator.NE) 139 { 140 NotQuery.appendNegation(query); 141 } 142 143 query.append(SolrFieldNames.CONTENT_LANGUAGE).append(":\"").append(ClientUtils.escapeQueryChars(_values[i])).append('"'); 144 } 145 if (_values.length > 1) 146 { 147 query.append(')'); 148 } 149 } 150 151 return query.toString(); 152 } 153 154 @Override 155 public int hashCode() 156 { 157 final int prime = 31; 158 int result = 1; 159 result = prime * result + ((_logicalOperator == null) ? 0 : _logicalOperator.hashCode()); 160 result = prime * result + ((_operator == null) ? 0 : _operator.hashCode()); 161 result = prime * result + Arrays.hashCode(_values); 162 return result; 163 } 164 165 @Override 166 public boolean equals(Object obj) 167 { 168 if (this == obj) 169 { 170 return true; 171 } 172 if (obj == null) 173 { 174 return false; 175 } 176 if (getClass() != obj.getClass()) 177 { 178 return false; 179 } 180 ContentLanguageQuery other = (ContentLanguageQuery) obj; 181 if (_logicalOperator != other._logicalOperator) 182 { 183 return false; 184 } 185 if (_operator != other._operator) 186 { 187 return false; 188 } 189 if (!Arrays.equals(_values, other._values)) 190 { 191 return false; 192 } 193 return true; 194 } 195}