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