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