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