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;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.cms.data.type.indexing.IndexableElementType;
028import org.ametys.cms.model.CMSDataContext;
029import org.ametys.cms.repository.Content;
030import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition;
031import org.ametys.cms.search.model.CriterionDefinitionHelper;
032import org.ametys.cms.search.model.SystemProperty;
033import org.ametys.cms.search.query.Query;
034import org.ametys.cms.search.query.Query.Operator;
035import org.ametys.cms.search.systemprop.AbstractSystemProperty;
036import org.ametys.cms.search.systemprop.IndexationAwareSystemProperty;
037import org.ametys.runtime.i18n.I18nizableText;
038import org.ametys.runtime.model.Enumerator;
039import org.ametys.runtime.model.StaticEnumerator;
040import org.ametys.runtime.model.type.DataContext;
041import org.ametys.runtime.model.type.ModelItemTypeConstants;
042import org.ametys.runtime.plugin.component.ThreadSafeComponentManager;
043import org.ametys.web.repository.content.WebContent;
044import org.ametys.web.search.query.ContentPrivacyQuery;
045
046/**
047 * {@link SystemProperty} which represents the privacy level of a Content ("public", "protected" or "private").
048 */
049public class ContentPrivacySystemProperty extends AbstractSystemProperty<String, Content> implements CriterionDefinitionAwareElementDefinition<String>, IndexationAwareSystemProperty<String, Content>
050{
051    /** System property identifier */
052    public static final String SYSTEM_PROPERTY_ID = "privacy";
053    
054    /** Solr field name. */
055    public static final String SOLR_FIELD_NAME = SYSTEM_PROPERTY_ID;
056    
057    private static final Map<String, I18nizableText> _ENUMERATOR_ENTRIES = new LinkedHashMap<>();
058    static
059    {
060        _ENUMERATOR_ENTRIES.put("public", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PUBLIC_LABEL"));
061        _ENUMERATOR_ENTRIES.put("protected", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PROTECTED_LABEL"));
062        _ENUMERATOR_ENTRIES.put("private", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PRIVATE_LABEL"));
063    }
064    
065    /** The criterion definition helper */
066    protected CriterionDefinitionHelper _criterionDefinitionHelper;
067    
068    @Override
069    public void service(ServiceManager manager) throws ServiceException
070    {
071        super.service(manager);
072        _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE);
073    }
074    
075    @Override
076    public Integer getColumnWidth()
077    {
078        return 60;
079    }
080    
081    @Override
082    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
083    {
084        if (value instanceof String[] values)
085        {
086            return new ContentPrivacyQuery(operator, values);
087        }
088        else
089        {
090            return new ContentPrivacyQuery(operator, (String) value);
091        }
092    }
093    
094    public String getSolrFieldName()
095    {
096        return SOLR_FIELD_NAME;
097    }
098    
099    public String getSolrSortFieldName()
100    {
101        return SOLR_FIELD_NAME + "_sort";
102    }
103    
104    public String getSolrFacetFieldName()
105    {
106        return SOLR_FIELD_NAME + "_dv";
107    }
108    
109    @Override
110    public Object getValue(Content content)
111    {
112        if (content instanceof WebContent webContent)
113        {
114            return webContent.getPrivacyLevel();
115        }
116        
117        return null;
118    }
119    
120    @Override
121    public Object valueToJSON(Content content, DataContext context)
122    {
123        Object value = getValue(content);
124        
125        if (value != null && _ENUMERATOR_ENTRIES.containsKey(value))
126        {
127            Map<String, Object> infos = new HashMap<>();
128            infos.put("value", value);
129            infos.put("label", _ENUMERATOR_ENTRIES.get(value));
130            return infos;
131        }
132        
133        return null;
134    }
135    
136    @Override
137    public Enumerator<String> getDefaultCriterionEnumerator(Configuration configuration, ThreadSafeComponentManager<Enumerator> enumeratorManager) throws ConfigurationException
138    {
139        StaticEnumerator<String> enumerator = new StaticEnumerator<>();
140        enumerator.addAll(_ENUMERATOR_ENTRIES);
141        return enumerator;
142    }
143    
144    @Override
145    protected String getTypeId()
146    {
147        return ModelItemTypeConstants.STRING_TYPE_ID;
148    }
149    
150    public IndexableElementType getDefaultCriterionType()
151    {
152        CMSDataContext context = CMSDataContext.newInstance()
153                                               .withModelItem(this);
154        
155        String typeId = getType().getDefaultCriterionTypeId(context);
156        return _criterionDefinitionHelper.getCriterionDefinitionType(typeId);
157    }
158}