001/* 002 * Copyright 2020 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.cms.search.solr; 017 018import java.util.ArrayList; 019import java.util.HashSet; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023 024import org.ametys.cms.search.QueryBuilder; 025import org.ametys.cms.search.query.AndQuery; 026import org.ametys.cms.search.query.Query; 027import org.ametys.cms.search.query.QuerySyntaxException; 028import org.ametys.cms.search.query.WorkflowStepQuery; 029 030import com.google.common.primitives.Ints; 031 032/** 033 * Helper to build Solr content queries. 034 */ 035public final class SolrContentQueryHelper 036{ 037 private SolrContentQueryHelper() 038 { 039 // Nothing 040 } 041 042 /** 043 * Gets the content types from JS parameters when using 'search-ui.solr' model 044 * @param jsParameters The JS parameters 045 * @return The content types 046 */ 047 @SuppressWarnings("unchecked") 048 public static Set<String> getContentTypes(Map<String, Object> jsParameters) 049 { 050 Map<String, Object> values = (Map<String, Object>) jsParameters.get("values"); 051 Object cTypesObj = values.get("contentTypes"); 052 if (cTypesObj != null && cTypesObj instanceof List<?>) 053 { 054 return new HashSet<>((List<String>) cTypesObj); 055 } 056 return Set.of(); 057 } 058 059 /** 060 * Build a Solr query string from inputs coming from a SolrQuerySearch (query, content types, workflow steps) 061 * @param queryBuilder The {@link QueryBuilder} component 062 * @param baseQuery The main query 063 * @param contentTypesOrMixins The content types -or mixins- (can be empty) 064 * @param wfSteps The workflow steps (can be empty) 065 * @return The built query 066 * @throws QuerySyntaxException If a query syntax is invalid 067 */ 068 public static String buildQuery(QueryBuilder queryBuilder, String baseQuery, Set<String> contentTypesOrMixins, Set<Integer> wfSteps) throws QuerySyntaxException 069 { 070 // Base query 071 List<Query> queriesInFinalQuery = new ArrayList<>(); 072 queriesInFinalQuery.add(() -> baseQuery); 073 074 // Content types 075 if (!contentTypesOrMixins.isEmpty()) 076 { 077 Query cTypeQuery = queryBuilder.createContentTypeOrMixinQuery(null, contentTypesOrMixins, true); 078 queriesInFinalQuery.add(0, cTypeQuery); 079 } 080 081 // Workflow steps 082 if (!wfSteps.isEmpty()) 083 { 084 WorkflowStepQuery wfStepQuery = new WorkflowStepQuery(Ints.toArray(wfSteps)); 085 queriesInFinalQuery.add(0, wfStepQuery); 086 } 087 088 Query query = new AndQuery(queriesInFinalQuery); 089 return query.build(); 090 } 091}