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 org.ametys.cms.content.indexing.solr.SolrFieldNames; 019import org.ametys.cms.search.query.Query; 020import org.ametys.cms.search.query.QuerySyntaxException; 021import org.ametys.cms.search.query.Query.Operator; 022import org.ametys.web.indexing.solr.SolrWebFieldNames; 023 024/** 025 * Query testing the id of a page or its ancestor ids (enabling to search the subpages of a given one). 026 */ 027public class PageQuery implements Query 028{ 029 /** The operator. */ 030 protected Operator _operator; 031 /** The id. */ 032 protected String _pageId; 033 /** True to search for a whole hierarchy. */ 034 protected boolean _withSubPages; 035 036 /** 037 * Create a PageQuery which searches on the given page (and optionally its sub-pages). 038 * @param withSubPages true to search for sub-pages too. 039 */ 040 public PageQuery(boolean withSubPages) 041 { 042 _withSubPages = withSubPages; 043 _operator = Operator.EXISTS; 044 } 045 046 /** 047 * Create a PageQuery which searches on the given page (and optionally its sub-pages). 048 * @param pageId the page id. 049 * @param withSubPages true to search for sub-pages of the given page id too. 050 */ 051 public PageQuery(String pageId, boolean withSubPages) 052 { 053 _pageId = pageId; 054 _withSubPages = withSubPages; 055 _operator = Operator.EQ; 056 } 057 058 @Override 059 public String build() throws QuerySyntaxException 060 { 061 StringBuilder query = new StringBuilder(); 062 063 if (_operator == Operator.EXISTS) 064 { 065 if (_withSubPages) 066 { 067 query.append('('); 068 } 069 070 query.append(SolrFieldNames.ID).append(":*"); 071 072 if (_withSubPages) 073 { 074 query.append(" OR ").append(SolrWebFieldNames.PAGE_ANCESTOR_IDS).append(":*)"); 075 } 076 } 077 else 078 { 079 if (_withSubPages) 080 { 081 query.append('('); 082 } 083 084 query.append(SolrFieldNames.ID) 085 .append(":\"") 086 .append(_pageId) 087 .append("\""); 088 089 if (_withSubPages) 090 { 091 query.append(" OR ").append(SolrWebFieldNames.PAGE_ANCESTOR_IDS).append(":\"") 092 .append(_pageId).append("\")"); 093 } 094 } 095 096 return query.toString(); 097 } 098 099 @Override 100 public int hashCode() 101 { 102 final int prime = 31; 103 int result = 1; 104 result = prime * result + ((_pageId == null) ? 0 : _pageId.hashCode()); 105 result = prime * result + (_withSubPages ? 1231 : 1237); 106 return result; 107 } 108 109 @Override 110 public boolean equals(Object obj) 111 { 112 if (this == obj) 113 { 114 return true; 115 } 116 if (obj == null) 117 { 118 return false; 119 } 120 if (getClass() != obj.getClass()) 121 { 122 return false; 123 } 124 PageQuery other = (PageQuery) obj; 125 if (_pageId == null) 126 { 127 if (other._pageId != null) 128 { 129 return false; 130 } 131 } 132 else if (!_pageId.equals(other._pageId)) 133 { 134 return false; 135 } 136 if (_withSubPages != other._withSubPages) 137 { 138 return false; 139 } 140 return true; 141 } 142}