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.Collections;
019import java.util.List;
020import java.util.Map;
021import java.util.Optional;
022
023import org.ametys.cms.contenttype.ContentType;
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.cms.search.ui.model.SearchUICriterion;
028import org.ametys.runtime.i18n.I18nizableText;
029import org.ametys.runtime.parameter.Validator;
030import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition;
031import org.ametys.web.frontoffice.search.metamodel.Searchable;
032
033/**
034 * {@link SearchCriterionDefinition} for {@link ContentSearchable} proposing a search criterion (based on a {@link SearchUICriterion}).
035 */
036public class ContentSearchCriterionDefinition extends AbstractDefaultSearchCriterionDefinition
037{
038    /** The SearchUICriterion */
039    protected SearchUICriterion _searchUICriterion;
040    /** The content type on which this criterion definition applies. Can be empty if it applies to all types of contents. */
041    protected Optional<ContentType> _contentType;
042
043    /**
044     * Default constructor
045     * @param id The id
046     * @param pluginName The plugin name
047     * @param searchable the {@link Searchable}
048     * @param criterion The linked {@link SearchUICriterion}
049     * @param contentType The content type on which this criterion definition applies. Can be empty if it applies to all types of contents.
050     * @param validator The validator
051     */
052    public ContentSearchCriterionDefinition(
053            String id,
054            String pluginName,
055            Optional<Searchable> searchable,
056            SearchUICriterion criterion,
057            Optional<ContentType> contentType,
058            Optional<Validator> validator)
059    {
060        super(
061                id,
062                pluginName,
063                criterion.getLabel(),
064                criterion.getType(),
065                Optional.ofNullable(criterion.getWidget()),
066                Optional.ofNullable(criterion.getWidgetParameters()),
067                Optional.ofNullable(criterion.getEnumerator()),
068                validator.isPresent() ? validator : Optional.ofNullable(criterion.getValidator()),
069                Optional.ofNullable(criterion.getDefaultValue()),
070                searchable
071        );
072        _searchUICriterion = criterion;
073        _contentType = contentType;
074    }
075    
076    /**
077     * Gets the {@link SearchUICriterion} associated to this definition
078     * @return the {@link SearchUICriterion} associated to this definition
079     */
080    public SearchUICriterion getSearchUICriterion()
081    {
082        return _searchUICriterion;
083    }
084    
085    @Override
086    public List<I18nizableText> getContextPrefixLabels()
087    {
088        return Collections.singletonList(
089                _contentType
090                    .map(ContentType::getLabel)
091                    .orElse(new I18nizableText("plugin.web", "PLUGINS_WEB_SERVICE_SEARCH_SEARCHABLE_CONTENT_ALL_PREFIX_LABEL")));
092    }
093    
094    @Override
095    public Map<String, Object> toJSON() throws Exception
096    {
097        Map<String, Object> json = super.toJSON();
098        json.put("multiple", _searchUICriterion.isMultiple());
099        return json;
100    }
101    
102    @Override
103    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
104    {
105        return _searchUICriterion.getQuery(value, operator, Collections.EMPTY_MAP, language, contextualParameters);
106    }
107    
108    @Override
109    public Query getEmptyValueQuery(String language, Map<String, Object> contextualParameters)
110    {
111        Query existQuery = _searchUICriterion.getQuery(null, Operator.EXISTS, Collections.EMPTY_MAP, language, contextualParameters);
112        return new NotQuery(existQuery);
113    }
114}