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.SearchField;
025import org.ametys.cms.search.model.SystemProperty;
026import org.ametys.cms.search.query.HasLiveVersionQuery;
027import org.ametys.cms.search.query.Query;
028import org.ametys.cms.search.query.Query.Operator;
029import org.ametys.cms.search.solr.field.HasLiveVersionSearchField;
030import org.ametys.plugins.repository.version.VersionAwareAmetysObject;
031import org.ametys.runtime.model.type.ModelItemTypeConstants;
032
033/**
034 * {@link SystemProperty} which indicate if the content has a live version. The current version or a past one.
035 */
036public class HasLiveVersionSystemProperty extends AbstractSystemProperty<Boolean, Content>
037{
038    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
039    {
040        boolean state = parseBoolean(value);
041        return new HasLiveVersionQuery(operator, state);
042    }
043
044    public SearchField getSearchField()
045    {
046        return new HasLiveVersionSearchField();
047    }
048
049    public Object getValue(Content ametysObject)
050    {
051        if (ametysObject instanceof VersionAwareAmetysObject versionAware)
052        {
053            String[] labels = versionAware.getAllLabels();
054            if (ArrayUtils.contains(labels, CmsConstants.LIVE_LABEL))
055            {
056                return true;
057            }
058        }
059        return false;
060    }
061
062    @Override
063    protected String _getTypeId()
064    {
065        return ModelItemTypeConstants.BOOLEAN_TYPE_ID;
066    }
067
068}