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.Map;
019
020import org.apache.commons.lang3.ArrayUtils;
021
022import org.ametys.cms.CmsConstants;
023import org.ametys.cms.repository.Content;
024import org.ametys.cms.search.model.SystemProperty;
025import org.ametys.cms.search.query.HasLiveVersionQuery;
026import org.ametys.cms.search.query.Query;
027import org.ametys.cms.search.query.Query.Operator;
028import org.ametys.plugins.repository.version.VersionAwareAmetysObject;
029import org.ametys.runtime.model.type.ModelItemTypeConstants;
030
031/**
032 * {@link SystemProperty} which indicate if the content has a live version. The current version or a past one.
033 */
034public class HasLiveVersionSystemProperty extends AbstractIndexableSystemProperty<Boolean, Boolean, Content>
035{
036    /** Solr field name. */
037    public static final String SOLR_FIELD_NAME = "hasLive";
038    
039    @Override
040    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
041    {
042        return new HasLiveVersionQuery(operator, (Boolean) value);
043    }
044
045    @Override
046    public String getSolrFieldName()
047    {
048        return SOLR_FIELD_NAME;
049    }
050    
051    @Override
052    public String getSolrSortFieldName()
053    {
054        return SOLR_FIELD_NAME;
055    }
056    
057    @Override
058    public String getSolrFacetFieldName()
059    {
060        return SOLR_FIELD_NAME + "_dv";
061    }
062    
063    public Object getValue(Content ametysObject)
064    {
065        if (ametysObject instanceof VersionAwareAmetysObject versionAware)
066        {
067            String[] labels = versionAware.getAllLabels();
068            if (ArrayUtils.contains(labels, CmsConstants.LIVE_LABEL))
069            {
070                return true;
071            }
072        }
073        return false;
074    }
075
076    @Override
077    protected String getTypeId()
078    {
079        return ModelItemTypeConstants.BOOLEAN_TYPE_ID;
080    }
081}