001/*
002 *  Copyright 2019 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.ArrayList;
019import java.util.Collection;
020import java.util.Collections;
021import java.util.List;
022import java.util.Optional;
023
024import org.apache.commons.lang3.ObjectUtils;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.cms.search.query.OrQuery;
028import org.ametys.cms.search.query.Query;
029import org.ametys.web.frontoffice.search.metamodel.AdditionalParameterValueMap;
030import org.ametys.web.frontoffice.search.metamodel.Returnable;
031import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition;
032import org.ametys.web.frontoffice.search.metamodel.Searchable;
033import org.ametys.web.search.query.PageContentQuery;
034
035/**
036 * {@link Searchable} for {@link Content}s
037 */
038public class ContentSearchable extends AbstractContentBasedSearchable
039{
040    /** Avalon Role */
041    public static final String ROLE = ContentSearchable.class.getName();
042    
043    /** The additional parameter for content types */
044    public static final String PARAMETER_CONTENT_TYPES = "contentTypes";
045    
046    private static final String __CRITERION_DEFINITIONS_PREFIX_ID = "ContentSearchable$";
047    
048    @Override
049    protected String associatedContentReturnableRole()
050    {
051        return ContentReturnable.ROLE;
052    }
053    
054    @Override
055    protected String getCriterionDefinitionPrefix()
056    {
057        return __CRITERION_DEFINITIONS_PREFIX_ID;
058    }
059    
060    @Override
061    protected Collection<String> getContentTypes(AdditionalParameterValueMap additionalParameterValues)
062    {
063        Collection<String> contentTypes = additionalParameterValues.getValue(PARAMETER_CONTENT_TYPES);
064        return ObjectUtils.defaultIfNull(contentTypes, Collections.emptyList());
065    }
066    
067    @Override
068    public Optional<Query> joinQuery(Query queryOnCriterion, SearchCriterionDefinition criterion, Collection<Returnable> returnables, AdditionalParameterValueMap additionalParameters)
069    {
070        List<Query> queries = new ArrayList<>();
071        if (returnables.contains(_pageReturnable))
072        {
073            queries.add(new PageContentQuery(queryOnCriterion));
074        }
075        if (returnables.contains(_associatedContentReturnable))
076        {
077            queries.add(queryOnCriterion);
078        }
079        
080        return queries.size() > 0 ? Optional.of(new OrQuery(queries)) : Optional.empty();
081    }
082}