001/*
002 *  Copyright 2013 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.plugins.repository.query.expression;
017
018/**
019 * Constructs an {@link Expression} corresponding to a full text search
020 */
021public class FullTextExpression implements Expression
022{
023    private String _value;
024    private MetadataExpression _metadata;
025    
026    /**
027     * Creates a full text Expression.
028     * @param value the String value
029     */
030    public FullTextExpression (String value)
031    {
032        _value = value;
033        _metadata = null;
034    }
035    
036    /**
037     * Creates a full text Expression.
038     * @param metadata the metadata name
039     * @param value the String value
040     */
041    public FullTextExpression (String metadata, String value)
042    {
043        _value = value;
044        _metadata = new MetadataExpression(metadata);
045    }
046    
047    @Override
048    public String build()
049    {
050        if (_metadata != null)
051        {
052            return "jcr:contains(" + _metadata.build() + ", '" + _value.replaceAll("'", "''") + "')";
053        }
054        else
055        {
056            return "jcr:contains(., '" + _value.replaceAll("'", "''") + "')";
057        }
058    }
059}