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.List;
020
021import org.apache.solr.client.solrj.util.ClientUtils;
022
023import org.ametys.core.user.UserIdentity;
024
025/**
026 * {@link Query} testing users.
027 */
028public class UsersQuery extends AbstractMultivaluedQuery<UserIdentity>
029{
030    /**
031     * Build a user query.
032     * @param fieldPath the field's path
033     * @param users The users to test.
034     */
035    public UsersQuery(String fieldPath, UserIdentity... users)
036    {
037        this(fieldPath, Arrays.asList(users));
038    }
039    
040    /**
041     * Build a user query.
042     * @param fieldPath the field's path
043     * @param users The users to test.
044     */
045    public UsersQuery(String fieldPath, List<UserIdentity> users)
046    {
047        this(fieldPath, Operator.EQ, users);
048    }
049    
050    /**
051     * Build a user query.
052     * @param fieldPath the field's path
053     * @param operator The query operator (can be EQ or NE).
054     * @param users The users to test.
055     */
056    public UsersQuery(String fieldPath, Operator operator, UserIdentity... users)
057    {
058        this(fieldPath, operator, Arrays.asList(users));
059    }
060    
061    /**
062     * Build a user query.
063     * @param fieldPath the field's path
064     * @param operator The query operator (can be EQ or NE).
065     * @param users The users to test.
066     */
067    public UsersQuery(String fieldPath, Operator operator, List<UserIdentity> users)
068    {
069        super(fieldPath, operator, LogicalOperator.OR, users);
070    }
071    
072    @Override
073    public String singleValueForQuery(UserIdentity user)
074    {
075        return ClientUtils.escapeQueryChars(UserIdentity.userIdentityToString(user));
076    }
077}