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;
021import java.util.stream.Stream;
022
023import org.ametys.cms.contenttype.MetadataType;
024import org.ametys.cms.search.query.DublinCoreQuery;
025import org.ametys.cms.search.query.NotQuery;
026import org.ametys.cms.search.query.OrQuery;
027import org.ametys.cms.search.query.Query;
028import org.ametys.cms.search.query.Query.Operator;
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.runtime.parameter.Enumerator;
031import org.ametys.runtime.parameter.StaticEnumerator;
032import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition;
033import org.ametys.web.frontoffice.search.metamodel.Searchable;
034
035/**
036 * {@link SearchCriterionDefinition} for {@link ResourceSearchable} proposing a search criterion on a DublinCore metadata of the resource.
037 */
038public class DublinCoreSearchCriterionDefinition extends AbstractDefaultSearchCriterionDefinition
039{
040    private String _metadataName;
041
042    /**
043     * Default constructor
044     * @param id The id
045     * @param pluginName The plugin name
046     * @param label The label
047     * @param type The type
048     * @param enumeratorEntries The enumerator entries
049     * @param searchable the {@link Searchable}
050     * @param metadataName The DC metadata name
051     */
052    public DublinCoreSearchCriterionDefinition(String id, String pluginName, I18nizableText label, MetadataType type, Optional<Map<String, I18nizableText>> enumeratorEntries, Optional<Searchable> searchable, String metadataName)
053    {
054        super(id, pluginName, label, type, Optional.empty(), Optional.empty(), _getEnumerator(enumeratorEntries), Optional.empty(), Optional.empty(), searchable);
055        _metadataName = metadataName;
056    }
057    
058    private static Optional<Enumerator> _getEnumerator(Optional<Map<String, I18nizableText>> entries)
059    {
060        return entries.map(map ->
061        {
062            StaticEnumerator enumerator = new StaticEnumerator();
063            for (String value : map.keySet())
064            {
065                enumerator.add(map.get(value), value);
066            }
067            return enumerator;
068            
069        });
070    }
071    
072    /**
073     * Potentially retrieve multiple values (as a stream) from an untyped client object
074     * @param value The untyped client object
075     * @return The values
076     */
077    @SuppressWarnings("unchecked")
078    protected Stream<String> _retrieveValues(Object value)
079    {
080        if (value instanceof String[])
081        {
082            return Stream.of((String[]) value);
083        }
084        else if (value instanceof Collection< ? >)
085        {
086            return ((Collection<String>) value).stream();
087        }
088        else if (value instanceof String)
089        {
090            return Stream.of((String) value);
091        }
092        else
093        {
094            throw new IllegalArgumentException("Wrong type for object '" + value + "' : " + value.getClass().getTypeName());
095        }
096    }
097    
098    @Override
099    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
100    {
101        return _retrieveValues(value)
102//            .map(ClientUtils::escapeQueryChars)
103            .map(val -> new DublinCoreQuery(_metadataName, val))
104            .collect(OrQuery.collector());
105    }
106    
107    @Override
108    public Query getEmptyValueQuery(String language, Map<String, Object> contextualParameters)
109    {
110        Query existQuery = () -> _metadataName + ":[* TO *]";
111        return new NotQuery(existQuery);
112    }
113}