001/*
002 *  Copyright 2025 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.model.properties;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject;
022import org.ametys.cms.data.type.indexing.IndexableElementType;
023import org.ametys.cms.model.CMSDataContext;
024import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition;
025import org.ametys.cms.search.model.CriterionDefinitionHelper;
026import org.ametys.cms.search.model.IndexationAwareElementDefinitionHelper;
027
028/**
029 * Abstract class for indexable and searchable property
030 * @param <T> type of the property values
031 * @param <C> type of criterion
032 * @param <X> type of ametys object supported by this property
033 */
034public abstract class AbstractIndexableStaticProperty<T, C, X extends ModelAwareDataAwareAmetysObject> extends AbstractStaticProperty<T, X> implements CriterionDefinitionAwareElementDefinition<T, C, X>
035{
036    /** The criterion definition helper */
037    protected CriterionDefinitionHelper _criterionDefinitionHelper;
038    /** The indexable aware element definition helper */
039    protected IndexationAwareElementDefinitionHelper _indexationAwareElementDefinitionHelper;
040    
041    @Override
042    public void service(ServiceManager manager) throws ServiceException
043    {
044        super.service(manager);
045        _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE);
046        _indexationAwareElementDefinitionHelper = (IndexationAwareElementDefinitionHelper) manager.lookup(IndexationAwareElementDefinitionHelper.ROLE);
047    }
048
049    public String getSolrSortFieldName()
050    {
051        return _indexationAwareElementDefinitionHelper.getDefaultSolrSortFieldName(this);
052    }
053    
054    public IndexableElementType getCriterionType()
055    {
056        String typeId = getCriterionTypeId();
057        return _criterionDefinitionHelper.getCriterionDefinitionType(typeId);
058    }
059    
060    /**
061     * Get the identifier of type to use when rendering the property as a criterion
062     * @return the identifier of type to use for a criterion
063     */
064    protected String getCriterionTypeId()
065    {
066        CMSDataContext context = CMSDataContext.newInstance()
067                                               .withModelItem(this);
068        return getType().getCriterionTypeId(context);
069    }
070}