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.HashSet; 021import java.util.List; 022import java.util.Optional; 023import java.util.Set; 024 025import org.ametys.cms.repository.Content; 026import org.ametys.cms.search.query.OrQuery; 027import org.ametys.cms.search.query.Query; 028import org.ametys.web.frontoffice.search.metamodel.AdditionalParameterValueMap; 029import org.ametys.web.frontoffice.search.metamodel.Returnable; 030import org.ametys.web.frontoffice.search.metamodel.SearchServiceCriterionDefinition; 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 Set<String> getContentTypeIds(AdditionalParameterValueMap additionalParameterValues) 061 { 062 Collection<String> contentTypes = additionalParameterValues.getValue(PARAMETER_CONTENT_TYPES); 063 return contentTypes != null 064 ? new HashSet<>(contentTypes) 065 : Set.of(); 066 } 067 068 @Override 069 public Optional<Query> joinQuery(Query queryOnCriterion, SearchServiceCriterionDefinition criterion, Collection<Returnable> returnables, AdditionalParameterValueMap additionalParameters) 070 { 071 List<Query> queries = new ArrayList<>(); 072 if (returnables.contains(_pageReturnable)) 073 { 074 queries.add(new PageContentQuery(queryOnCriterion)); 075 } 076 if (returnables.contains(_associatedContentReturnable)) 077 { 078 queries.add(queryOnCriterion); 079 } 080 081 return queries.size() > 0 ? Optional.of(new OrQuery(queries)) : Optional.empty(); 082 } 083}