001/* 002 * Copyright 2017 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.query; 017 018import java.util.Arrays; 019import java.util.Collection; 020import java.util.Collections; 021import java.util.List; 022import java.util.stream.Collectors; 023 024import org.apache.commons.lang3.StringUtils; 025 026/** 027 * Represents a search {@link Query} corresponding to the logical "or" between several other queries. 028 */ 029public class OrQuery implements Query 030{ 031 032 /** The list of queries. The queries on this list are distinct. */ 033 protected List<Query> _queries; 034 035 /** 036 * Build an OrQuery object. 037 * @param queries the queries. 038 */ 039 public OrQuery(Query... queries) 040 { 041 this(Arrays.asList(queries)); 042 } 043 044 /** 045 * Build an OrQuery object. 046 * @param queries the queries as a Collection. 047 */ 048 public OrQuery(Collection<Query> queries) 049 { 050 _queries = queries.stream().distinct().collect(Collectors.toList()); 051 } 052 053 /** 054 * Get the list of queries in this "or". 055 * @return the list of queries. 056 */ 057 public List<Query> getQueries() 058 { 059 return Collections.unmodifiableList(_queries); 060 } 061 062 @Override 063 public String build() throws QuerySyntaxException 064 { 065 boolean isFirst = true; 066 StringBuilder sb = new StringBuilder(); 067 068 if (_queries.size() > 1) 069 { 070 sb.append('('); 071 } 072 073 for (Query subQuery : _queries) 074 { 075 if (subQuery != null) 076 { 077 String exprAsString = subQuery.build(); 078 if (StringUtils.isNotBlank(exprAsString)) 079 { 080 if (!isFirst) 081 { 082 sb.append(" OR "); 083 } 084 sb.append(exprAsString); 085 isFirst = false; 086 } 087 } 088 } 089 090 if (isFirst) 091 { 092 return ""; 093 } 094 else 095 { 096 if (_queries.size() > 1) 097 { 098 sb.append(')'); 099 } 100 return sb.toString(); 101 } 102 } 103 104 @Override 105 public int hashCode() 106 { 107 final int prime = 31; 108 int result = 1; 109 result = prime * result + ((_queries == null) ? 0 : _queries.hashCode()); 110 return result; 111 } 112 113 @Override 114 public boolean equals(Object obj) 115 { 116 if (this == obj) 117 { 118 return true; 119 } 120 if (obj == null) 121 { 122 return false; 123 } 124 if (getClass() != obj.getClass()) 125 { 126 return false; 127 } 128 OrQuery other = (OrQuery) obj; 129 if (_queries == null) 130 { 131 if (other._queries != null) 132 { 133 return false; 134 } 135 } 136 else if (!_queries.equals(other._queries)) 137 { 138 return false; 139 } 140 return true; 141 } 142}