001/*
002 *  Copyright 2015 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.web.search.systemprop;
017
018import java.util.HashMap;
019import java.util.LinkedHashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.configuration.Configuration;
023import org.apache.avalon.framework.configuration.ConfigurationException;
024
025import org.ametys.cms.repository.Content;
026import org.ametys.cms.search.model.SystemProperty;
027import org.ametys.cms.search.query.Query;
028import org.ametys.cms.search.query.Query.Operator;
029import org.ametys.cms.search.systemprop.AbstractIndexableSystemProperty;
030import org.ametys.runtime.i18n.I18nizableText;
031import org.ametys.runtime.model.Enumerator;
032import org.ametys.runtime.model.StaticEnumerator;
033import org.ametys.runtime.model.type.DataContext;
034import org.ametys.runtime.model.type.ModelItemTypeConstants;
035import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
036import org.ametys.web.repository.content.WebContent;
037import org.ametys.web.search.query.ContentPrivacyQuery;
038
039/**
040 * {@link SystemProperty} which represents the privacy level of a Content ("public", "protected" or "private").
041 */
042public class ContentPrivacySystemProperty extends AbstractIndexableSystemProperty<String, String, Content>
043{
044    /** System property identifier */
045    public static final String SYSTEM_PROPERTY_ID = "privacy";
046    
047    /** Solr field name. */
048    public static final String SOLR_FIELD_NAME = SYSTEM_PROPERTY_ID;
049    
050    private static final Map<String, I18nizableText> _ENUMERATOR_ENTRIES = new LinkedHashMap<>();
051    static
052    {
053        _ENUMERATOR_ENTRIES.put("public", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PUBLIC_LABEL"));
054        _ENUMERATOR_ENTRIES.put("protected", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PROTECTED_LABEL"));
055        _ENUMERATOR_ENTRIES.put("private", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PRIVATE_LABEL"));
056    }
057    
058    @Override
059    public Integer getColumnWidth()
060    {
061        return 60;
062    }
063    
064    @Override
065    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
066    {
067        if (value instanceof String[] values)
068        {
069            return new ContentPrivacyQuery(operator, values);
070        }
071        else
072        {
073            return new ContentPrivacyQuery(operator, (String) value);
074        }
075    }
076    
077    @Override
078    public String getSolrFieldName()
079    {
080        return SOLR_FIELD_NAME;
081    }
082    
083    @Override
084    public String getSolrSortFieldName()
085    {
086        return SOLR_FIELD_NAME + "_sort";
087    }
088    
089    @Override
090    public String getSolrFacetFieldName()
091    {
092        return SOLR_FIELD_NAME + "_dv";
093    }
094    
095    @Override
096    public Object getValue(Content content)
097    {
098        if (content instanceof WebContent webContent)
099        {
100            return webContent.getPrivacyLevel();
101        }
102        
103        return null;
104    }
105    
106    @Override
107    public Object valueToJSON(Content content, DataContext context)
108    {
109        Object value = getValue(content);
110        
111        if (value != null && _ENUMERATOR_ENTRIES.containsKey(value))
112        {
113            Map<String, Object> infos = new HashMap<>();
114            infos.put("value", value);
115            infos.put("label", _ENUMERATOR_ENTRIES.get(value));
116            return infos;
117        }
118        
119        return null;
120    }
121    
122    @Override
123    public Enumerator<String> getDefaultCriterionEnumerator(Configuration configuration, ThreadSafeComponentManager<Enumerator> enumeratorManager) throws ConfigurationException
124    {
125        StaticEnumerator<String> enumerator = new StaticEnumerator<>();
126        enumerator.addAll(_ENUMERATOR_ENTRIES);
127        return enumerator;
128    }
129    
130    @Override
131    protected String getTypeId()
132    {
133        return ModelItemTypeConstants.STRING_TYPE_ID;
134    }
135}