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 content language.
024 */
025public class ContentLanguageExpression implements Expression
026{
027    private Operator _operator;
028    private String _value;
029
030    /**
031     * Creates the expression.
032     * @param operator the operator to make the comparison (only Operator.EQ and Operator.NE allowed)
033     * @param value the content type value
034     */
035    public ContentLanguageExpression(Operator operator, String value)
036    {
037        if (Operator.EQ != operator && Operator.NE != operator)
038        {
039            throw new AmetysRepositoryException("Test operator '" + "' is unknown for test's expression.");
040        }
041
042        _operator = operator;
043        _value = value;
044    }
045
046    @Override
047    public String build()
048    {
049        return "@" + RepositoryConstants.NAMESPACE_PREFIX_INTERNAL + ':' + DefaultContent.METADATA_LANGUAGE + " " + _operator + "'" + _value.replaceAll("'", "''") + "'";
050    }
051}