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.Collection;
019import java.util.HashMap;
020import java.util.LinkedHashMap;
021import java.util.Map;
022
023import org.apache.avalon.framework.configuration.Configuration;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.cms.contenttype.MetadataType;
028import org.ametys.cms.repository.Content;
029import org.ametys.cms.search.SearchField;
030import org.ametys.cms.search.model.SystemProperty;
031import org.ametys.cms.search.query.Query;
032import org.ametys.cms.search.query.Query.Operator;
033import org.ametys.cms.search.systemprop.AbstractSystemProperty;
034import org.ametys.runtime.i18n.I18nizableText;
035import org.ametys.web.repository.content.WebContent;
036import org.ametys.web.search.query.ContentPrivacyQuery;
037import org.ametys.web.search.solr.field.ContentPrivacySearchField;
038import org.ametys.web.site.SiteConfigurationExtensionPoint;
039
040/**
041 * {@link SystemProperty} which represents the privacy level of a Content ("public", "protected" or "private").
042 */
043public class ContentPrivacySystemProperty extends AbstractSystemProperty
044{
045    
046    private static final Map<String, I18nizableText> _ENUMERATOR_ENTRIES = new LinkedHashMap<>();
047    static
048    {
049        _ENUMERATOR_ENTRIES.put("public", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PUBLIC_LABEL"));
050        _ENUMERATOR_ENTRIES.put("protected", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PROTECTED_LABEL"));
051        _ENUMERATOR_ENTRIES.put("private", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PRIVATE_LABEL"));
052    }
053    
054    private SiteConfigurationExtensionPoint _siteConfigurationEP;
055    
056    @Override
057    public void service(ServiceManager manager) throws ServiceException
058    {
059        super.service(manager);
060        _siteConfigurationEP = (SiteConfigurationExtensionPoint) manager.lookup(SiteConfigurationExtensionPoint.ROLE);
061    }
062    
063    @Override
064    public MetadataType getType()
065    {
066        return MetadataType.STRING;
067    }
068    
069    @Override
070    public boolean isMultiple()
071    {
072        return false;
073    }
074    
075    @Override
076    public boolean isSortable()
077    {
078        return true;
079    }
080    
081    @Override
082    public Integer getColumnWidth()
083    {
084        return 60;
085    }
086    
087    @Override
088    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
089    {
090        return new ContentPrivacyQuery(operator, (String) value);
091    }
092    
093    @Override
094    public String getRenderer()
095    {
096        return "Ametys.cms.content.EditContentsGrid.renderEnumeratedString";
097    }
098    
099    @Override
100    public String getConverter()
101    {
102        return "Ametys.cms.content.EditContentsGrid.convertEnumeratedString";
103    }
104    
105    @Override
106    public SearchField getSearchField()
107    {
108        return new ContentPrivacySearchField();
109    }
110    
111    @Override
112    public Object getValue(Content content)
113    {
114        String privacy = null;
115        
116        if (content instanceof WebContent)
117        {
118            privacy = content.getMetadataHolder().getString("privacy", null);
119            if (privacy == null)
120            {
121                privacy = _siteConfigurationEP.getValueAsString(((WebContent) content).getSiteName(), "content-privacy");
122            }
123        }
124        
125        return privacy;
126    }
127    
128    @Override
129    public Object getFullValue(Content content)
130    {
131        Object value = getValue(content);
132        
133        if (value != null && _ENUMERATOR_ENTRIES.containsKey(value))
134        {
135            Map<String, Object> infos = new HashMap<>();
136            infos.put("value", value);
137            infos.put("label", _ENUMERATOR_ENTRIES.get(value));
138            return infos;
139        }
140        
141        return null;
142    }
143    
144    @Override
145    public EnumeratorDefinition getEnumeratorDefinition(Collection<String> contentTypes, Configuration configuration)
146    {
147        return new EnumeratorDefinition(_ENUMERATOR_ENTRIES);
148    }
149
150}