001/*
002 *  Copyright 2020 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.web.frontoffice.search.metamodel.impl;
017
018import java.util.Map;
019import java.util.Optional;
020
021import org.ametys.cms.search.query.FullTextQuery;
022import org.ametys.cms.search.query.NotQuery;
023import org.ametys.cms.search.query.OrQuery;
024import org.ametys.cms.search.query.Query;
025import org.ametys.cms.search.query.Query.Operator;
026import org.ametys.cms.search.query.StringQuery;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition;
029import org.ametys.web.frontoffice.search.metamodel.Searchable;
030
031/**
032 * A title or wording {@link SearchCriterionDefinition}, based on {@link FullTextQuery FullTextQueries}
033 */
034public class TitleOrWordingSearchCriterionDefinition extends WordingSearchCriterionDefinition
035{
036    /**
037     * Constructs a {@link TitleOrWordingSearchCriterionDefinition}
038     * @param id The id
039     * @param pluginName The plugin name
040     * @param label The label
041     * @param searchable the {@link Searchable}
042     * @param wordingType the type of {@link TitleOrWordingSearchCriterionDefinition}
043     */
044    public TitleOrWordingSearchCriterionDefinition(
045            String id, 
046            String pluginName, 
047            I18nizableText label, 
048            Optional<Searchable> searchable,
049            WordingType wordingType)
050    {
051        super(id, pluginName, label, searchable, wordingType);
052    }
053    
054    @Override
055    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
056    {
057        Query titleQuery = new StringQuery("title", operator, ((String) value).trim(), language);
058        switch (_wordingType)
059        {
060            case TEXTFIELD:
061                Query fullTextQuery = _getTextFieldQuery(value, operator, language);
062                return new OrQuery(titleQuery, fullTextQuery);
063            case ALL_WORDS:
064            case NO_WORDS:
065                Query wordsQuery = _getWordsQuery(value, operator, language);
066                
067                Query query = new OrQuery(titleQuery, wordsQuery);
068                if (_wordingType == WordingType.NO_WORDS)
069                {
070                    query = new NotQuery(query);
071                }
072                
073                return query;
074            default:
075                throw new IllegalStateException("Unknown wording type: " + _wordingType);
076        }
077    }
078}