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