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 && Operator.EXISTS != 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 (_operator == Operator.EXISTS)
122        {
123            query.append(SolrFieldNames.WORKFLOW_STEP).append(":").append(QueryHelper.EXISTS_VALUE);
124            
125            return query.toString();
126        }
127        
128        if (_values.length > 0)
129        {
130            if (_values.length > 1)
131            {
132                query.append('(');
133            }
134            for (int i = 0; i < _values.length; i++)
135            {
136                if (i > 0)
137                {
138                    query.append(' ').append(_logicalOperator).append(' ');
139                }
140                
141                if (_operator == Operator.NE)
142                {
143                    NotQuery.appendNegation(query);
144                }
145                
146                query.append(SolrFieldNames.WORKFLOW_STEP).append(':').append(_values[i]);
147            }
148            if (_values.length > 1)
149            {
150                query.append(')');
151            }
152        }
153        
154        return query.toString();
155    }
156
157    @Override
158    public int hashCode()
159    {
160        final int prime = 31;
161        int result = 1;
162        result = prime * result + ((_logicalOperator == null) ? 0 : _logicalOperator.hashCode());
163        result = prime * result + ((_operator == null) ? 0 : _operator.hashCode());
164        result = prime * result + Arrays.hashCode(_values);
165        return result;
166    }
167
168    @Override
169    public boolean equals(Object obj)
170    {
171        if (this == obj)
172        {
173            return true;
174        }
175        if (obj == null)
176        {
177            return false;
178        }
179        if (getClass() != obj.getClass())
180        {
181            return false;
182        }
183        WorkflowStepQuery other = (WorkflowStepQuery) obj;
184        if (_logicalOperator != other._logicalOperator)
185        {
186            return false;
187        }
188        if (_operator != other._operator)
189        {
190            return false;
191        }
192        if (!Arrays.equals(_values, other._values))
193        {
194            return false;
195        }
196        return true;
197    }
198}