001/*
002 *  Copyright 2018 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.contenttype.MetadataType;
022import org.ametys.cms.search.query.NotQuery;
023import org.ametys.cms.search.query.Query;
024import org.ametys.cms.search.query.Query.Operator;
025import org.ametys.runtime.i18n.I18nizableText;
026import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition;
027import org.ametys.web.frontoffice.search.metamodel.Searchable;
028import org.ametys.web.search.misc.SiteQueryHelper;
029import org.ametys.web.search.query.SiteQuery;
030import org.ametys.web.site.SiteEnumerator;
031
032/**
033 * {@link SearchCriterionDefinition} proposing a search criterion on the site of the indexed document.
034 */
035public class SiteSearchCriterionDefinition extends AbstractDefaultSearchCriterionDefinition
036{
037    private SiteQueryHelper _siteQueryHelper;
038
039    /**
040     * Default constructor
041     * @param id The id
042     * @param pluginName The plugin name
043     * @param label The label
044     * @param siteEnumerator The {@link SiteEnumerator}
045     * @param siteQueryHelper The {@link SiteQueryHelper}
046     * @param searchable The {@link Searchable}
047     */
048    public SiteSearchCriterionDefinition(
049            String id, 
050            String pluginName, 
051            I18nizableText label, 
052            SiteEnumerator siteEnumerator,
053            SiteQueryHelper siteQueryHelper,
054            Optional<Searchable> searchable)
055    {
056        super(id, pluginName, label, MetadataType.STRING, _widget(), _widgetParameters(), Optional.of(siteEnumerator), Optional.empty(), Optional.empty(), searchable);
057        _siteQueryHelper = siteQueryHelper;
058    }
059    
060    private static Optional<String> _widget()
061    {
062        return Optional.of("edition.select-sites-criterion");
063    }
064    
065    private static Optional<Map<String, I18nizableText>> _widgetParameters()
066    {
067        return Optional.empty();
068    }
069
070    @Override
071    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
072    {
073        return _siteQueryHelper.getQuery(value, operator, contextualParameters, getId());
074    }
075    
076    @Override
077    public Query getEmptyValueQuery(String language, Map<String, Object> contextualParameters)
078    {
079        Query existQuery = new SiteQuery();
080        return new NotQuery(existQuery);
081    }
082}