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;
023
024import org.ametys.cms.contenttype.MetadataType;
025import org.ametys.cms.repository.Content;
026import org.ametys.cms.search.SearchField;
027import org.ametys.cms.search.model.SystemProperty;
028import org.ametys.cms.search.query.Query;
029import org.ametys.cms.search.query.Query.Operator;
030import org.ametys.cms.search.systemprop.AbstractSystemProperty;
031import org.ametys.runtime.i18n.I18nizableText;
032import org.ametys.web.repository.content.WebContent;
033import org.ametys.web.repository.site.Site;
034import org.ametys.web.search.query.ContentPrivacyQuery;
035import org.ametys.web.search.solr.field.ContentPrivacySearchField;
036
037/**
038 * {@link SystemProperty} which represents the privacy level of a Content ("public", "protected" or "private").
039 */
040public class ContentPrivacySystemProperty extends AbstractSystemProperty
041{
042    
043    private static final Map<String, I18nizableText> _ENUMERATOR_ENTRIES = new LinkedHashMap<>();
044    static
045    {
046        _ENUMERATOR_ENTRIES.put("public", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PUBLIC_LABEL"));
047        _ENUMERATOR_ENTRIES.put("protected", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PROTECTED_LABEL"));
048        _ENUMERATOR_ENTRIES.put("private", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PRIVATE_LABEL"));
049    }
050    
051    @Override
052    public MetadataType getType()
053    {
054        return MetadataType.STRING;
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        String privacy = null;
091        
092        if (content instanceof WebContent)
093        {
094            privacy = content.getMetadataHolder().getString("privacy", null);
095            if (privacy == null)
096            {
097                Site site = ((WebContent) content).getSite();
098                privacy = site.getValue("content-privacy", true, null);
099            }
100        }
101        
102        return privacy;
103    }
104    
105    @Override
106    public Object getJsonValue(Content content, boolean full)
107    {
108        Object value = getValue(content);
109        
110        if (value != null && _ENUMERATOR_ENTRIES.containsKey(value))
111        {
112            Map<String, Object> infos = new HashMap<>();
113            infos.put("value", value);
114            infos.put("label", _ENUMERATOR_ENTRIES.get(value));
115            return infos;
116        }
117        
118        return null;
119    }
120    
121    @Override
122    public EnumeratorDefinition getEnumeratorDefinition(Configuration configuration)
123    {
124        return new EnumeratorDefinition(_ENUMERATOR_ENTRIES);
125    }
126
127}