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.Map;
020import java.util.Optional;
021
022import org.ametys.cms.contenttype.MetadataType;
023import org.ametys.runtime.i18n.I18nizableText;
024import org.ametys.runtime.parameter.Enumerator;
025import org.ametys.runtime.parameter.Parameter;
026import org.ametys.runtime.parameter.ParameterHelper;
027import org.ametys.runtime.parameter.Validator;
028import org.ametys.web.frontoffice.search.metamodel.EnumeratedValues;
029import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition;
030import org.ametys.web.frontoffice.search.metamodel.Searchable;
031
032/**
033 * Default implementation of {@link SearchCriterionDefinition}
034 */
035public abstract class AbstractDefaultSearchCriterionDefinition extends Parameter<MetadataType> implements SearchCriterionDefinition
036{
037    private Optional<Searchable> _searchable;
038
039    /**
040     * Constructs a {@link SearchCriterionDefinition}
041     * @param id The id
042     * @param pluginName The plugin name
043     * @param label The label
044     * @param type The type
045     * @param widget The widget
046     * @param widgetParams The widget parameters
047     * @param enumerator The enumerator
048     * @param validator The validator
049     * @param defaultValue The default value
050     * @param searchable the {@link Searchable}
051     */
052    public AbstractDefaultSearchCriterionDefinition(
053            String id,
054            String pluginName,
055            I18nizableText label,
056            MetadataType type,
057            Optional<String> widget,
058            Optional<Map<String, I18nizableText>> widgetParams,
059            Optional<Enumerator> enumerator,
060            Optional<Validator> validator,
061            Optional<Object> defaultValue,
062            Optional<Searchable> searchable)
063    {
064        setId(id);
065        setPluginName(pluginName);
066        setLabel(label);
067        setType(type);
068        widget.ifPresent(this::setWidget);
069        setWidgetParameters(widgetParams.orElse(Collections.emptyMap()));
070        enumerator.ifPresent(this::setEnumerator);
071        validator.ifPresent(this::setValidator);
072        defaultValue.ifPresent(this::setDefaultValue);
073        _searchable = searchable;
074    }
075    
076    @Override
077    public Optional<Searchable> getSearchable()
078    {
079        return _searchable;
080    }
081    
082    @Override
083    public boolean isEnumerated()
084    {
085        return getEnumerator() != null;
086    }
087    
088    @Override
089    public Optional<EnumeratedValues> getEnumeratedValues(Map<String, Object> contextualParameters)
090    {
091        return Optional.ofNullable(getEnumerator())
092                .map(enumerator -> new EnumeratorBasedEnumeratedValues(enumerator, getId()));
093    }
094    
095    @Override
096    public Map<String, Object> toJSON() throws Exception
097    {
098        return ParameterHelper.toJSON(this);
099    }
100    
101    /**
102     * Releases and destroys any resource it owns.
103     */
104    protected void dispose()
105    {
106        // Override if needed
107    }
108}
109