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)
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        boolean appendNegation = _operator == Operator.NE; 
068        if (appendNegation)
069        {
070            NotQuery.appendNegation(query).append('(');
071        }
072        
073        switch (_commentInclusion)  
074        {
075            case VALIDATED_AND_NON_VALIDATED:
076                query.append(SolrFieldNames.CONTENT_COMMENTS).append(":true");
077                break;
078            case VALIDATED:
079                query.append(SolrFieldNames.CONTENT_COMMENTS_VALIDATED).append(":true");
080                break;
081            case NON_VALIDATED:
082                query.append(SolrFieldNames.CONTENT_COMMENTS_NONVALIDATED).append(":true");
083                break;
084            default:
085                throw new IllegalArgumentException("CommentInclusion '" + _commentInclusion + "' is not supported.");
086        }
087        
088        if (appendNegation)
089        {
090            query.append(')');
091        }
092        
093        return query.toString();
094    }
095
096    @Override
097    public int hashCode()
098    {
099        return Objects.hash(_commentInclusion, _operator);
100    }
101
102    @Override
103    public boolean equals(Object obj)
104    {
105        if (this == obj)
106        {
107            return true;
108        }
109        if (obj == null)
110        {
111            return false;
112        }
113        if (getClass() != obj.getClass())
114        {
115            return false;
116        }
117        CommentQuery other = (CommentQuery) obj;
118        return _commentInclusion == other._commentInclusion && _operator == other._operator;
119    }
120}