001/*
002 *  Copyright 2022 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 */
016
017package org.ametys.plugins.calendar.search;
018
019import java.util.Arrays;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.Optional;
023
024import org.apache.commons.lang3.ObjectUtils;
025
026import org.ametys.cms.search.query.Query;
027import org.ametys.web.frontoffice.search.metamodel.AdditionalParameterValueMap;
028import org.ametys.web.frontoffice.search.metamodel.Returnable;
029import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition;
030import org.ametys.web.frontoffice.search.metamodel.Searchable;
031import org.ametys.web.frontoffice.search.metamodel.impl.AbstractContentBasedSearchable;
032
033/**
034 * {@link Searchable} for contents with a start date
035 *
036 */
037public class CalendarContentSearchable extends AbstractContentBasedSearchable
038{
039    /** Avalon Role */
040    public static final String ROLE = CalendarContentSearchable.class.getName();
041    
042    /** The prefix for content with date searchable */
043    public static final String CRITERION_DEFINITION_PREFIX_ID = "ContentWithDateSearchable$";
044    
045    public Optional<Query> joinQuery(Query queryOnCriterion, SearchCriterionDefinition criterion, Collection<Returnable> returnables, AdditionalParameterValueMap additionalParameters)
046    {
047        if (returnables.contains(_associatedContentReturnable))
048        {
049            return Optional.of(queryOnCriterion);
050        }
051        else
052        {
053            return Optional.empty();
054        }
055    }
056    
057    @Override
058    public Collection<Returnable> relationsWith()
059    {
060        return Arrays.asList(_associatedContentReturnable);
061    }
062
063    @Override
064    protected String associatedContentReturnableRole()
065    {
066        return CalendarContentReturnable.ROLE;
067    }
068
069    @Override
070    protected String getCriterionDefinitionPrefix()
071    {
072        return CRITERION_DEFINITION_PREFIX_ID;
073    }
074
075    @Override
076    protected Collection<String> getContentTypes(AdditionalParameterValueMap additionalParameterValues)
077    {
078        Collection<String> contentTypes = additionalParameterValues.getValue(CalendarSearchService.SERVICE_PARAM_CONTENT_TYPES);
079        return ObjectUtils.defaultIfNull(contentTypes, Collections.emptyList());
080    }
081
082}