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.Objects; 019 020import org.ametys.cms.content.indexing.solr.SolrFieldNames; 021 022/** 023 * Represents a {@link Query} testing if contents have comments. 024 */ 025public class CommentQuery implements Query 026{ 027 private CommentInclusion _commentInclusion; 028 private Operator _operator; 029 030 /** 031 * The type (validated, non validated or both) of comments to include for {@link CommentQuery} 032 */ 033 public static enum CommentInclusion 034 { 035 /** Request only contents with both validated and non-validated comments */ 036 VALIDATED_AND_NON_VALIDATED, 037 /** Request only contents with validated comments */ 038 VALIDATED, 039 /** Request only contents with non validated-comments */ 040 NON_VALIDATED, 041 } 042 043 /** 044 * Constructor 045 * @param commentInclusion The type of comments to include in search 046 * (request only contents with {@link CommentInclusion#VALIDATED validated} comments, 047 * or only contents with {@link CommentInclusion#NON_VALIDATED non validated} comments, 048 * or {@link CommentInclusion#VALIDATED_AND_NON_VALIDATED both}) 049 * @param operator The operator. Only {@link Query.Operator#EQ} and {@link Query.Operator#NE} are allowed. 050 */ 051 public CommentQuery(CommentInclusion commentInclusion, Operator operator) 052 { 053 if (Operator.EQ != operator && Operator.NE != operator && Operator.EXISTS != operator) 054 { 055 throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for comment query"); 056 } 057 058 _commentInclusion = commentInclusion; 059 _operator = operator; 060 } 061 062 @Override 063 public String build() throws QuerySyntaxException 064 { 065 StringBuilder query = new StringBuilder(); 066 067 if (_operator == Operator.EXISTS) 068 { 069 query.append(SolrFieldNames.CONTENT_COMMENTS).append(":true"); 070 return query.toString(); 071 } 072 073 boolean appendNegation = _operator == Operator.NE; 074 if (appendNegation) 075 { 076 NotQuery.appendNegation(query).append('('); 077 } 078 079 switch (_commentInclusion) 080 { 081 case VALIDATED_AND_NON_VALIDATED: 082 query.append(SolrFieldNames.CONTENT_COMMENTS).append(":true"); 083 break; 084 case VALIDATED: 085 query.append(SolrFieldNames.CONTENT_COMMENTS_VALIDATED).append(":true"); 086 break; 087 case NON_VALIDATED: 088 query.append(SolrFieldNames.CONTENT_COMMENTS_NONVALIDATED).append(":true"); 089 break; 090 default: 091 throw new IllegalArgumentException("CommentInclusion '" + _commentInclusion + "' is not supported."); 092 } 093 094 if (appendNegation) 095 { 096 query.append(')'); 097 } 098 099 return query.toString(); 100 } 101 102 @Override 103 public int hashCode() 104 { 105 return Objects.hash(_commentInclusion, _operator); 106 } 107 108 @Override 109 public boolean equals(Object obj) 110 { 111 if (this == obj) 112 { 113 return true; 114 } 115 if (obj == null) 116 { 117 return false; 118 } 119 if (getClass() != obj.getClass()) 120 { 121 return false; 122 } 123 CommentQuery other = (CommentQuery) obj; 124 return _commentInclusion == other._commentInclusion && _operator == other._operator; 125 } 126}