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