001/* 002 * Copyright 2016 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.search.query; 017 018import java.util.ArrayList; 019import java.util.Arrays; 020import java.util.Collection; 021import java.util.List; 022 023import org.ametys.cms.search.query.Query; 024import org.ametys.web.indexing.solr.SolrWebFieldNames; 025 026/** 027 * Query testing the pageId of a resource (page attachment). 028 */ 029public class AttachmentQuery implements Query 030{ 031 /** The page ids */ 032 protected List<String> _pageIds; 033 034 /** 035 * Create an AttachmentQuery which searches on resources attached to the given page and its sub-pages. 036 * @param pageIds the page ids 037 */ 038 public AttachmentQuery(String... pageIds) 039 { 040 this(Arrays.asList(pageIds)); 041 } 042 043 /** 044 * Create an AttachmentQuery which searches on resources attached to the given page and its sub-pages. 045 * @param pageIds the page ids 046 */ 047 public AttachmentQuery(Collection<String> pageIds) 048 { 049 _pageIds = new ArrayList<>(pageIds); 050 } 051 052 @Override 053 public String build() 054 { 055 int count = _pageIds.size(); 056 057 StringBuilder query = new StringBuilder(); 058 059 if (count > 1) 060 { 061 query.append('('); 062 } 063 064 boolean first = true; 065 for (String pageId : _pageIds) 066 { 067 if (!first) 068 { 069 query.append(" OR "); 070 } 071 query.append(SolrWebFieldNames.PAGE_ID).append(":\"").append(pageId).append('"'); 072 first = false; 073 } 074 075 if (count > 1) 076 { 077 query.append(')'); 078 } 079 080 return query.toString(); 081 } 082}