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