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.content.indexing.solr.SolrResourceGroupedMimeTypes;
022import org.ametys.cms.contenttype.MetadataType;
023import org.ametys.cms.search.query.MimeTypeGroupQuery;
024import org.ametys.cms.search.query.OrQuery;
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;
030
031/**
032 * {@link SearchCriterionDefinition} for {@link ResourceSearchable} proposing a search criterion on the format of the resource.
033 */
034public class ResourceFormatSearchCriterionDefinition extends DublinCoreSearchCriterionDefinition
035{
036    /** The format enumerator */
037    protected static final Map<String, I18nizableText> ENUMERATOR_ENTRIES = Map.of(
038        SolrResourceGroupedMimeTypes.PDF_GROUP, new I18nizableText("plugin.web", "PLUGINS_WEB_DOCUMENT_FORMAT_PDF"),
039        SolrResourceGroupedMimeTypes.IMAGE_GROUP, new I18nizableText("plugin.web", "PLUGINS_WEB_DOCUMENT_FORMAT_IMAGE"),
040        SolrResourceGroupedMimeTypes.TEXT_GROUP, new I18nizableText("plugin.web", "PLUGINS_WEB_DOCUMENT_FORMAT_TEXT"),
041        SolrResourceGroupedMimeTypes.PRES_GROUP, new I18nizableText("plugin.web", "PLUGINS_WEB_DOCUMENT_FORMAT_PRES"),
042        SolrResourceGroupedMimeTypes.SPREADSHEET_GROUP, new I18nizableText("plugin.web", "PLUGINS_WEB_DOCUMENT_FORMAT_SPREADSHEET"),
043        SolrResourceGroupedMimeTypes.VIDEO_GROUP, new I18nizableText("plugin.web", "PLUGINS_WEB_DOCUMENT_FORMAT_VIDEO"),
044        SolrResourceGroupedMimeTypes.AUDIO_GROUP, new I18nizableText("plugin.web", "PLUGINS_WEB_DOCUMENT_FORMAT_AUDIO")
045    );
046    
047    /**
048     * Default constructor
049     * @param id The id
050     * @param pluginName The plugin name
051     * @param label The label
052     * @param searchable the {@link Searchable}
053     */
054    public ResourceFormatSearchCriterionDefinition(String id, String pluginName, I18nizableText label, Optional<Searchable> searchable)
055    {
056        super(id, pluginName, label, MetadataType.STRING, Optional.of(ENUMERATOR_ENTRIES), searchable, "format");
057    }
058    
059    @Override
060    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
061    {
062        return _retrieveValues(value)
063                .filter(ENUMERATOR_ENTRIES::containsKey)
064                .map(MimeTypeGroupQuery::new)
065                .collect(OrQuery.collector());
066    }
067}