001/*
002 *  Copyright 2015 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.search.systemprop;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.configuration.Configuration;
021import org.apache.avalon.framework.configuration.ConfigurationException;
022
023import org.ametys.cms.repository.Content;
024import org.ametys.cms.search.SearchField;
025import org.ametys.cms.search.model.SystemProperty;
026import org.ametys.cms.search.query.FullTextQuery;
027import org.ametys.cms.search.query.Query;
028import org.ametys.cms.search.query.Query.Operator;
029import org.ametys.runtime.model.type.ModelItemTypeConstants;
030
031/**
032 * {@link SystemProperty} which represents the full textual content of a Content.
033 */
034public class FulltextSystemProperty extends AbstractSystemProperty<String, Content>
035{
036    /** True for a stemmed full-text search, false otherwise. */
037    protected boolean _stemmed;
038    
039    @Override
040    public void configure(Configuration configuration) throws ConfigurationException
041    {
042        super.configure(configuration);
043        _stemmed = configuration.getChild("stemmed").getValueAsBoolean(false);
044    }
045    
046    @Override
047    public boolean isMultiple()
048    {
049        return false;
050    }
051    
052    @Override
053    public boolean isDisplayable()
054    {
055        return false;
056    }
057    
058    @Override
059    public boolean isSortable()
060    {
061        return false;
062    }
063    
064    @Override
065    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
066    {
067        return new FullTextQuery((String) value, language, _stemmed ? Operator.SEARCH_STEMMED : Operator.SEARCH);
068    }
069    
070    @Override
071    public SearchField getSearchField()
072    {
073        // No value to index, not sortable: the field won't be used.
074        return null;
075    }
076    
077    @Override
078    public Object getValue(Content content)
079    {
080        // No real "value" to return here.
081        // The values are indexed separately, while walking the content attribute tree.
082        return null;
083    }
084
085    @Override
086    protected String _getTypeId()
087    {
088        return ModelItemTypeConstants.STRING_TYPE_ID;
089    }
090}