001/*
002 *  Copyright 2023 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.Collection;
019import java.util.List;
020import java.util.Map;
021import java.util.Optional;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.configuration.ConfigurationException;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.commons.lang3.StringUtils;
028import org.apache.solr.common.SolrInputDocument;
029
030import org.ametys.cms.contenttype.ContentTypesHelper;
031import org.ametys.cms.data.holder.impl.IndexableDataHolderHelper;
032import org.ametys.cms.data.type.indexing.IndexableElementType;
033import org.ametys.cms.model.CMSDataContext;
034import org.ametys.cms.repository.Content;
035import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition;
036import org.ametys.cms.search.model.CriterionDefinitionHelper;
037import org.ametys.cms.search.model.SystemProperty;
038import org.ametys.cms.search.query.FullTextQuery;
039import org.ametys.cms.search.query.Query;
040import org.ametys.cms.search.query.Query.Operator;
041import org.ametys.cms.search.solr.schema.SchemaDefinition;
042import org.ametys.runtime.model.View;
043import org.ametys.runtime.model.type.ModelItemTypeConstants;
044
045/**
046 * {@link SystemProperty} which represents the full textual content of a Content based on attributes of a view.
047 * If the view does not exist for content, nothing will be indexed.
048 */
049public class ViewBasedFullTextSystemProperty extends AbstractSystemProperty<String, Content> implements CriterionDefinitionAwareElementDefinition<String>, IndexationAwareSystemProperty<String, Content>
050{
051    /** The contents types helper */
052    protected ContentTypesHelper _contentTypesHelper;
053    /** The criterion definition helper */
054    protected CriterionDefinitionHelper _criterionDefinitionHelper;
055    
056    /** The view name to fill the property */
057    protected String _viewName;
058    
059    @Override
060    public void service(ServiceManager manager) throws ServiceException
061    {
062        super.service(manager);
063        _contentTypesHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE);
064        _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE);
065    }
066    
067    @Override
068    public void configure(Configuration configuration) throws ConfigurationException
069    {
070        super.configure(configuration);
071        _viewName = configuration.getChild("view").getValue();
072        if (StringUtils.isBlank(_viewName))
073        {
074            throw new ConfigurationException("'view' configuration is missing for indexing property '" + this.getName() + "'");
075        }
076    }
077    
078    @Override
079    public void indexValue(SolrInputDocument document, Content content, CMSDataContext context)
080    {
081        Optional<View> view = Optional.of(_viewName)
082                .map(v -> _contentTypesHelper.getView(v, content));
083        
084        if (view.isPresent())
085        {
086            CMSDataContext clonedContext = CMSDataContext.newInstance(context)
087                                                         .withIndexForFullTextField(true)
088                                                         .withFullTextFieldName(getName());
089            
090            IndexableDataHolderHelper.indexData(content, view.get(), document, document, StringUtils.EMPTY, clonedContext);
091        }
092    }
093    
094    @Override
095    public boolean isDisplayable()
096    {
097        return false;
098    }
099    
100    @Override
101    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
102    {
103        return new FullTextQuery((String) value, getName(), language, operator);
104    }
105    
106    public String getSolrFieldName()
107    {
108        // No value to index: the field won't be used.
109        return null;
110    }
111    
112    public String getSolrSortFieldName()
113    {
114        // Not sortable
115        return null;
116    }
117    
118    public String getSolrFacetFieldName()
119    {
120        // Not facetable
121        return null;
122    }
123    
124    public Collection<SchemaDefinition> getSchemaDefinitions()
125    {
126        // No specific schema definition
127        return List.of();
128    }
129    
130    @Override
131    public Object getValue(Content content)
132    {
133        // No real "value" to return here.
134        // The values are indexed separately, while walking the content attribute tree.
135        return null;
136    }
137
138    @Override
139    protected String getTypeId()
140    {
141        return ModelItemTypeConstants.STRING_TYPE_ID;
142    }
143    
144    public IndexableElementType getDefaultCriterionType()
145    {
146        CMSDataContext context = CMSDataContext.newInstance()
147                                               .withModelItem(this);
148        
149        String typeId = getType().getDefaultCriterionTypeId(context);
150        return _criterionDefinitionHelper.getCriterionDefinitionType(typeId);
151    }
152}