001/*
002 *  Copyright 2014 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;
019
020import org.ametys.cms.content.indexing.solr.SolrFieldNames;
021
022/**
023 * Represents a {@link Query} testing the current workflow step of a content.
024 * It can only be used to search on the current step.
025 */
026public class WorkflowStepQuery implements Query
027{
028    
029    private Operator _operator;
030    private int[] _values;
031    private LogicalOperator _logicalOperator;
032    
033    /**
034     * Build a WorkflowStepQuery.
035     * @param value the step id to test.
036     */
037    public WorkflowStepQuery(int value)
038    {
039        this(new int[] {value});
040    }
041    
042    /**
043     * Build a WorkflowStepQuery.
044     * @param values the step IDs to test.
045     */
046    public WorkflowStepQuery(int... values)
047    {
048        this(Operator.EQ, values);
049    }
050    
051    /**
052     * Build a WorkflowStepQuery.
053     * @param operator the operator.
054     * @param value the step id to test.
055     */
056    public WorkflowStepQuery(Operator operator, int value)
057    {
058        this(operator, new int[] {value});
059    }
060    
061    /**
062     * Build a WorkflowStepQuery.
063     * @param operator the operator.
064     * @param values the step IDs to test.
065     */
066    public WorkflowStepQuery(Operator operator, int... values)
067    {
068        this(operator, LogicalOperator.OR, values);
069    }
070    
071    /**
072     * Build a WorkflowStepQuery.
073     * @param operator the operator.
074     * @param logicalOperator the logical operator.
075     * @param values the step IDs to test.
076     */
077    public WorkflowStepQuery(Operator operator, LogicalOperator logicalOperator, int... values)
078    {
079        if (Operator.EQ != operator && Operator.NE != operator)
080        {
081            throw new IllegalArgumentException("Test operator '" + operator + "' is unknown for test's expression.");
082        }
083        
084        _operator = operator;
085        _logicalOperator = logicalOperator;
086        _values = values;
087    }
088
089    /**
090     * Get the operator.
091     * @return the operator
092     */
093    public Operator getOperator()
094    {
095        return _operator;
096    }
097
098    /**
099     * Get the values.
100     * @return the values
101     */
102    public int[] getValues()
103    {
104        return _values;
105    }
106
107    /**
108     * Get the logicalOperator.
109     * @return the logicalOperator
110     */
111    public LogicalOperator getLogicalOperator()
112    {
113        return _logicalOperator;
114    }
115    
116    @Override
117    public String build() throws QuerySyntaxException
118    {
119        StringBuilder query = new StringBuilder();
120        
121        if (_values.length > 0)
122        {
123            if (_values.length > 1)
124            {
125                query.append('(');
126            }
127            for (int i = 0; i < _values.length; i++)
128            {
129                if (i > 0)
130                {
131                    query.append(' ').append(_logicalOperator).append(' ');
132                }
133                
134                if (_operator == Operator.NE)
135                {
136                    NotQuery.appendNegation(query);
137                }
138                
139                query.append(SolrFieldNames.WORKFLOW_STEP).append(':').append(_values[i]);
140            }
141            if (_values.length > 1)
142            {
143                query.append(')');
144            }
145        }
146        
147        return query.toString();
148    }
149
150    @Override
151    public int hashCode()
152    {
153        final int prime = 31;
154        int result = 1;
155        result = prime * result + ((_logicalOperator == null) ? 0 : _logicalOperator.hashCode());
156        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
157        result = prime * result + Arrays.hashCode(_values);
158        return result;
159    }
160
161    @Override
162    public boolean equals(Object obj)
163    {
164        if (this == obj)
165        {
166            return true;
167        }
168        if (obj == null)
169        {
170            return false;
171        }
172        if (getClass() != obj.getClass())
173        {
174            return false;
175        }
176        WorkflowStepQuery other = (WorkflowStepQuery) obj;
177        if (_logicalOperator != other._logicalOperator)
178        {
179            return false;
180        }
181        if (_operator != other._operator)
182        {
183            return false;
184        }
185        if (!Arrays.equals(_values, other._values))
186        {
187            return false;
188        }
189        return true;
190    }
191}