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        if (_users.isEmpty())
082        {
083            return "";
084        }
085        
086        StringBuilder query = new StringBuilder();
087        
088        query.append(_fieldPath).append(":(");
089        
090        boolean first = true;
091        for (UserIdentity user : _users)
092        {
093            if (!first)
094            {
095                query.append(" OR ");
096            }
097            query.append(ClientUtils.escapeQueryChars(UserIdentity.userIdentityToString(user)));
098            first = false;
099        }
100        
101        query.append(')');
102        
103        if (_operator == Operator.NE)
104        {
105            query.insert(0, "(" + NotQuery.NEGATION_QUERY_PREFIX).append(')');
106        }
107        
108        return query.toString();
109    }
110
111    @Override
112    public int hashCode()
113    {
114        final int prime = 31;
115        int result = super.hashCode();
116        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
117        result = prime * result + ((_users == null) ? 0 : _users.hashCode());
118        return result;
119    }
120
121    @Override
122    public boolean equals(Object obj)
123    {
124        if (this == obj)
125        {
126            return true;
127        }
128        if (!super.equals(obj))
129        {
130            return false;
131        }
132        if (getClass() != obj.getClass())
133        {
134            return false;
135        }
136        UsersQuery other = (UsersQuery) obj;
137        if (_operator != other._operator)
138        {
139            return false;
140        }
141        if (_users == null)
142        {
143            if (other._users != null)
144            {
145                return false;
146            }
147        }
148        else if (!_users.equals(other._users))
149        {
150            return false;
151        }
152        return true;
153    }
154}