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;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.cms.data.type.indexing.IndexableElementType;
026import org.ametys.cms.model.CMSDataContext;
027import org.ametys.cms.repository.Content;
028import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition;
029import org.ametys.cms.search.model.CriterionDefinitionHelper;
030import org.ametys.cms.search.model.SystemProperty;
031import org.ametys.cms.search.query.FullTextQuery;
032import org.ametys.cms.search.query.Query;
033import org.ametys.cms.search.query.Query.Operator;
034import org.ametys.runtime.model.type.ModelItemTypeConstants;
035
036/**
037 * {@link SystemProperty} which represents the full textual content of a Content.
038 */
039public class FulltextSystemProperty extends AbstractSystemProperty<String, Content> implements CriterionDefinitionAwareElementDefinition<String> 
040{
041    /** The criterion definition helper */
042    protected CriterionDefinitionHelper _criterionDefinitionHelper;
043    
044    /** True for a stemmed full-text search, false otherwise. */
045    protected boolean _stemmed;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        super.service(manager);
051        _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE);
052    }
053    
054    @Override
055    public void configure(Configuration configuration) throws ConfigurationException
056    {
057        super.configure(configuration);
058        _stemmed = configuration.getChild("stemmed").getValueAsBoolean(false);
059    }
060    
061    @Override
062    public boolean isDisplayable()
063    {
064        return false;
065    }
066    
067    @Override
068    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
069    {
070        return new FullTextQuery((String) value, language, _stemmed ? Operator.SEARCH_STEMMED : Operator.SEARCH);
071    }
072    
073    @Override
074    public Object getValue(Content content)
075    {
076        // No real "value" to return here.
077        // The values are indexed separately, while walking the content attribute tree.
078        return null;
079    }
080
081    @Override
082    protected String getTypeId()
083    {
084        return ModelItemTypeConstants.STRING_TYPE_ID;
085    }
086    
087    public IndexableElementType getDefaultCriterionType()
088    {
089        CMSDataContext context = CMSDataContext.newInstance()
090                                               .withModelItem(this);
091        
092        String typeId = getType().getDefaultCriterionTypeId(context);
093        return _criterionDefinitionHelper.getCriterionDefinitionType(typeId);
094    }
095}