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    /** The logical operator to use in xpath query */
029    public enum LogicalOperator 
030    {
031        /** Logical operator AND */
032        AND
033        {
034            @Override
035            public String toString()
036            {
037                return " and ";
038            }
039        },
040        /** Logical operator OR */
041        OR
042        {
043            @Override
044            public String toString()
045            {
046                return " or ";
047            }
048        }
049    }
050    
051    private Operator _operator;
052    private int[] _values;
053    private LogicalOperator _logicalOperator;
054
055    /**
056     * Creates the expression.
057     * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed)
058     * @param value the step value 
059     */
060    public WorkflowStepExpression(Operator operator, int value)
061    {
062        if (Operator.EQ != operator && Operator.NE != operator)
063        {
064            throw new AmetysRepositoryException("Test operator '" + "' is unknown for test's expression.");
065        }
066
067        _operator = operator;
068        _values = new int[]{value};
069    }
070    
071    /**
072     * Creates the expression.
073     * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed)
074     * @param values the tags value in a array
075     * @param logicalOperator the logical operator to use for given value
076     */
077    public WorkflowStepExpression(Operator operator, int[] values, LogicalOperator logicalOperator)
078    {
079        if (Operator.EQ != operator && Operator.NE != operator)
080        {
081            throw new AmetysRepositoryException("Test operator '" + "' is unknown for test's expression.");
082        }
083
084        _operator = operator;
085        _values = values;
086        _logicalOperator = logicalOperator;
087    }
088    
089    @Override
090    public String build()
091    {
092        StringBuffer sb = new StringBuffer();
093        
094        if (_values.length > 0)
095        {
096            sb.append('(');
097            for (int i = 0; i < _values.length; i++)
098            {
099                if (i != 0)
100                {
101                    sb.append(_logicalOperator);
102                }
103                sb.append("@").append(RepositoryConstants.NAMESPACE_PREFIX_INTERNAL).append(':').append(WorkflowAwareContentHelper.METADATA_CURRENT_STEP_ID).append(' ').append(_operator).append(' ').append(_values[i]);
104            }
105            sb.append(')');
106        }
107        
108        return sb.toString();
109    }
110}