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.Collection;
019import java.util.Map;
020import java.util.Optional;
021
022import org.ametys.cms.contenttype.MetadataType;
023import org.ametys.cms.languages.LanguageEnumerator;
024import org.ametys.cms.search.query.NotQuery;
025import org.ametys.cms.search.query.Query;
026import org.ametys.cms.search.query.Query.Operator;
027import org.ametys.runtime.i18n.I18nizableText;
028import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition;
029import org.ametys.web.frontoffice.search.metamodel.Searchable;
030import org.ametys.web.search.query.SitemapQuery;
031
032/**
033 * {@link SearchCriterionDefinition} proposing a search criterion on the sitemap of the indexed document.
034 */
035public class SitemapSearchCriterionDefinition extends AbstractDefaultSearchCriterionDefinition
036{
037    /**
038     * Default constructor
039     * @param id The id
040     * @param pluginName The plugin name
041     * @param label The label
042     * @param siteEnumerator The {@link LanguageEnumerator}
043     * @param searchable The {@link Searchable}
044     */
045    public SitemapSearchCriterionDefinition(
046            String id, 
047            String pluginName, 
048            I18nizableText label, 
049            LanguageEnumerator siteEnumerator,
050            Optional<Searchable> searchable)
051    {
052        super(id, pluginName, label, MetadataType.STRING, Optional.empty(), Optional.empty(), Optional.of(siteEnumerator), Optional.empty(), Optional.empty(), searchable);
053    }
054
055    @Override
056    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
057    {
058        String[] sitemaps;
059        if (value instanceof String)
060        {
061            sitemaps = ((String) value).split(",");
062        }
063        else if (value instanceof Collection<?>)
064        {
065            sitemaps = ((Collection<?>) value)
066                    .stream()
067                    .filter(String.class::isInstance)
068                    .map(String.class::cast)
069                    .toArray(String[]::new);
070        }
071        else
072        {
073            throw new IllegalArgumentException("Not handled type of value: '" + value + "'");
074        }
075        
076        return new SitemapQuery(sitemaps);
077    }
078    
079    @Override
080    public Query getEmptyValueQuery(String language, Map<String, Object> contextualParameters)
081    {
082        Query existQuery = new SitemapQuery();
083        return new NotQuery(existQuery);
084    }
085}