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 PageAttachmentQuery implements Query 030{ 031 /** The page ids */ 032 protected List<String> _pageIds; 033 034 /** 035 * Create an PageAttachmentQuery which searches on resources attached to the given page and its sub-pages. 036 * @param pageIds the page ids 037 */ 038 public PageAttachmentQuery(String... pageIds) 039 { 040 this(Arrays.asList(pageIds)); 041 } 042 043 /** 044 * Create an PageAttachmentQuery which searches on resources attached to the given page and its sub-pages. 045 * @param pageIds the page ids 046 */ 047 public PageAttachmentQuery(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.ATTACHMENT_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 083 @Override 084 public int hashCode() 085 { 086 final int prime = 31; 087 int result = 1; 088 result = prime * result + ((_pageIds == null) ? 0 : _pageIds.hashCode()); 089 return result; 090 } 091 092 @Override 093 public boolean equals(Object obj) 094 { 095 if (this == obj) 096 { 097 return true; 098 } 099 if (obj == null) 100 { 101 return false; 102 } 103 if (getClass() != obj.getClass()) 104 { 105 return false; 106 } 107 PageAttachmentQuery other = (PageAttachmentQuery) obj; 108 if (_pageIds == null) 109 { 110 if (other._pageIds != null) 111 { 112 return false; 113 } 114 } 115 else if (!_pageIds.equals(other._pageIds)) 116 { 117 return false; 118 } 119 return true; 120 } 121}