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.cms.search.query; 017 018import java.util.Arrays; 019import java.util.Collection; 020import java.util.HashSet; 021 022import org.apache.solr.client.solrj.util.ClientUtils; 023 024import org.ametys.core.user.UserIdentity; 025 026/** 027 * {@link Query} testing users. 028 */ 029public class UsersQuery extends AbstractFieldQuery 030{ 031 private Collection<UserIdentity> _users; 032 private Operator _operator; 033 034 /** 035 * Build a user query. 036 * @param fieldPath the field's path 037 * @param users The users to test. 038 */ 039 public UsersQuery(String fieldPath, UserIdentity... users) 040 { 041 this(fieldPath, Arrays.asList(users)); 042 } 043 044 /** 045 * Build a user query. 046 * @param fieldPath the field's path 047 * @param users The users to test. 048 */ 049 public UsersQuery(String fieldPath, Collection<UserIdentity> users) 050 { 051 this(fieldPath, Operator.EQ, users); 052 } 053 054 /** 055 * Build a user query. 056 * @param fieldPath the field's path 057 * @param operator The query operator (can be EQ or NE). 058 * @param users The users to test. 059 */ 060 public UsersQuery(String fieldPath, Operator operator, UserIdentity... users) 061 { 062 this(fieldPath, operator, Arrays.asList(users)); 063 } 064 065 /** 066 * Build a user query. 067 * @param fieldPath the field's path 068 * @param operator The query operator (can be EQ or NE). 069 * @param users The users to test. 070 */ 071 public UsersQuery(String fieldPath, Operator operator, Collection<UserIdentity> users) 072 { 073 super(fieldPath); 074 this._users = new HashSet<>(users); 075 this._operator = operator; 076 } 077 078 @Override 079 public String build() throws QuerySyntaxException 080 { 081 StringBuilder query = new StringBuilder(); 082 083 if (_operator == Operator.EXISTS) 084 { 085 query.append(_fieldPath).append(":").append(QueryHelper.EXISTS_VALUE); 086 087 return query.toString(); 088 } 089 090 if (_users.isEmpty()) 091 { 092 return ""; 093 } 094 095 query.append(_fieldPath).append(":("); 096 097 boolean first = true; 098 for (UserIdentity user : _users) 099 { 100 if (!first) 101 { 102 query.append(" OR "); 103 } 104 query.append(ClientUtils.escapeQueryChars(UserIdentity.userIdentityToString(user))); 105 first = false; 106 } 107 108 query.append(')'); 109 110 if (_operator == Operator.NE) 111 { 112 query.insert(0, "(" + NotQuery.NEGATION_QUERY_PREFIX).append(')'); 113 } 114 115 return query.toString(); 116 } 117 118 @Override 119 public int hashCode() 120 { 121 final int prime = 31; 122 int result = super.hashCode(); 123 result = prime * result + ((_operator == null) ? 0 : _operator.hashCode()); 124 result = prime * result + ((_users == null) ? 0 : _users.hashCode()); 125 return result; 126 } 127 128 @Override 129 public boolean equals(Object obj) 130 { 131 if (this == obj) 132 { 133 return true; 134 } 135 if (!super.equals(obj)) 136 { 137 return false; 138 } 139 if (getClass() != obj.getClass()) 140 { 141 return false; 142 } 143 UsersQuery other = (UsersQuery) obj; 144 if (_operator != other._operator) 145 { 146 return false; 147 } 148 if (_users == null) 149 { 150 if (other._users != null) 151 { 152 return false; 153 } 154 } 155 else if (!_users.equals(other._users)) 156 { 157 return false; 158 } 159 return true; 160 } 161}