001/*
002 *  Copyright 2022 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.time.ZonedDateTime;
019
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022
023import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject;
024import org.ametys.cms.data.type.indexing.IndexableElementType;
025import org.ametys.cms.model.CMSDataContext;
026import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition;
027import org.ametys.cms.search.model.CriterionDefinitionHelper;
028import org.ametys.cms.search.model.SystemProperty;
029
030/**
031 * Abstract class providing base functionality for a date time-typed {@link SystemProperty}.
032 * @param <X> type of ametys object supported by this property
033 */
034public abstract class AbstractDateTimeIndexableSystemProperty<X extends ModelAwareDataAwareAmetysObject> extends AbstractDateTimeSystemProperty<X> implements CriterionDefinitionAwareElementDefinition<ZonedDateTime>, IndexationAwareSystemProperty<ZonedDateTime, X>
035{
036    /** The criterion definition helper */
037    protected CriterionDefinitionHelper _criterionDefinitionHelper;
038    
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        super.service(manager);
043        _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE);
044    }
045    
046    public String getDefaultCriterionWidget()
047    {
048        return "edition.date";
049    }
050
051    public String getSolrSortFieldName()
052    {
053        return getSolrFieldName();
054    }
055    
056    public String getSolrFacetFieldName()
057    {
058        // For now, there is no pertinent way to do facets on date data
059        // It would be nice to have facets on date intervals, but this behavior does not exist yet in ametys 
060        return null;
061    }
062    
063    public IndexableElementType getDefaultCriterionType()
064    {
065        CMSDataContext context = CMSDataContext.newInstance()
066                                               .withModelItem(this);
067        
068        String typeId = getType().getDefaultCriterionTypeId(context);
069        return _criterionDefinitionHelper.getCriterionDefinitionType(typeId);
070    }
071}