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