001/*
002 *  Copyright 2010 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.repository;
017
018import org.ametys.plugins.repository.AmetysRepositoryException;
019import org.ametys.plugins.repository.RepositoryConstants;
020import org.ametys.plugins.repository.query.expression.Expression;
021
022/**
023 * Constructs an {@link Expression} testing the current workflow step of a content.
024 * This can be used only to search on the current step. To search on history steps, use the {@link CurrentStepExpression} instead
025 */
026public class WorkflowStepExpression implements Expression
027{
028    private Operator _operator;
029    private int[] _values;
030    private LogicalOperator _logicalOperator;
031
032    /**
033     * Creates the expression.
034     * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed)
035     * @param value the step value 
036     */
037    public WorkflowStepExpression(Operator operator, int value)
038    {
039        if (Operator.EQ != operator && Operator.NE != operator)
040        {
041            throw new AmetysRepositoryException("Test operator '" + "' is unknown for test's expression.");
042        }
043
044        _operator = operator;
045        _values = new int[]{value};
046    }
047    
048    /**
049     * Creates the expression.
050     * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed)
051     * @param values the tags value in a array
052     * @param logicalOperator the logical operator to use for given value
053     */
054    public WorkflowStepExpression(Operator operator, int[] values, LogicalOperator logicalOperator)
055    {
056        if (Operator.EQ != operator && Operator.NE != operator)
057        {
058            throw new AmetysRepositoryException("Test operator '" + "' is unknown for test's expression.");
059        }
060
061        _operator = operator;
062        _values = values;
063        _logicalOperator = logicalOperator;
064    }
065    
066    @Override
067    public String build()
068    {
069        StringBuffer sb = new StringBuffer();
070        
071        if (_values.length > 0)
072        {
073            sb.append('(');
074            for (int i = 0; i < _values.length; i++)
075            {
076                if (i != 0)
077                {
078                    sb.append(_logicalOperator);
079                }
080                sb.append("@").append(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL).append(':').append(WorkflowAwareContentHelper.METADATA_CURRENT_STEP_ID).append(' ').append(_operator).append(' ').append(_values[i]);
081            }
082            sb.append(')');
083        }
084        
085        return sb.toString();
086    }
087}