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.cms.search.systemprop;
017
018import java.util.LinkedHashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.configuration.Configuration;
022import org.apache.avalon.framework.configuration.DefaultConfiguration;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.cms.contenttype.MetadataType;
027import org.ametys.cms.languages.Language;
028import org.ametys.cms.languages.LanguageEnumerator;
029import org.ametys.cms.languages.LanguagesManager;
030import org.ametys.cms.repository.Content;
031import org.ametys.cms.search.SearchField;
032import org.ametys.cms.search.model.SystemProperty;
033import org.ametys.cms.search.query.ContentLanguageQuery;
034import org.ametys.cms.search.query.Query;
035import org.ametys.cms.search.query.Query.Operator;
036import org.ametys.cms.search.solr.field.LanguageSearchField;
037
038/**
039 * {@link SystemProperty} which represents the language of a Content.
040 */
041public class LanguageSystemProperty extends AbstractSystemProperty
042{
043    
044    /** The language manager. */
045    protected LanguagesManager _languageManager;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        super.service(manager);
051        _languageManager = (LanguagesManager) manager.lookup(LanguagesManager.ROLE);
052    }
053    
054    @Override
055    public MetadataType getType()
056    {
057        return MetadataType.STRING;
058    }
059    
060    @Override
061    public boolean isMultiple()
062    {
063        return false;
064    }
065    
066    @Override
067    public boolean isSortable()
068    {
069        return true;
070    }
071    
072    @Override
073    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
074    {
075        String langValue = null;
076        if ("CURRENT".equals(value))
077        {
078            langValue = language;
079        }
080        else
081        {
082            langValue = (String) value;
083        }
084        
085        return new ContentLanguageQuery(operator, langValue);
086    }
087    
088    @Override
089    public String getRenderer()
090    {
091        return "Ametys.plugins.cms.search.SearchGridHelper.renderLanguage";
092    }
093    
094    @Override
095    public String getConverter()
096    {
097        return "Ametys.plugins.cms.search.SearchGridHelper.convertLanguage";
098    }
099    
100    @Override
101    public Integer getColumnWidth()
102    {
103        return 70;
104    }
105    
106    @Override
107    public SearchField getSearchField()
108    {
109        return new LanguageSearchField();
110    }
111    
112    @Override
113    public Object getValue(Content content)
114    {
115        return content.getLanguage();
116    }
117    
118    public Object getJsonValue(Content content, boolean full)
119    {
120        if (full)
121        {
122            Map<String, Object> infos = new LinkedHashMap<>();
123            
124            String languageCode = content.getLanguage();
125            infos.put("code", languageCode);
126            
127            if (languageCode != null)
128            {
129                Language language = _languageManager.getLanguage(languageCode);
130                if (language != null)
131                {
132                    infos.put("icon", language.getSmallIcon());
133                    infos.put("label", language.getLabel());
134                }
135            }
136            
137            return infos;
138        }
139        else
140        {
141            return getValue(content);
142        }
143    }
144    
145    @Override
146    public Object getSortValue(Content content)
147    {
148        Language language = _languageManager.getLanguage(content.getLanguage());
149        if (language != null)
150        {
151            return _i18nUtils.translate(language.getLabel(), content.getLanguage());
152        }
153        
154        return null;
155    }
156    
157    @Override
158    public EnumeratorDefinition getEnumeratorDefinition(Configuration configuration)
159    {
160        Configuration conf = new DefaultConfiguration("enumeration");
161        return new EnumeratorDefinition(LanguageEnumerator.class, conf);
162    }
163
164}