001/*
002 *  Copyright 2013 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.core.util;
017
018import java.io.IOException;
019
020import org.apache.avalon.framework.component.Component;
021import org.apache.avalon.framework.context.Context;
022import org.apache.avalon.framework.context.ContextException;
023import org.apache.avalon.framework.context.Contextualizable;
024import org.apache.avalon.framework.logger.LogEnabled;
025import org.apache.avalon.framework.logger.Logger;
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028import org.apache.avalon.framework.service.Serviceable;
029import org.apache.cocoon.components.ContextHelper;
030import org.apache.cocoon.environment.Request;
031
032import org.ametys.runtime.i18n.I18nizable;
033import org.ametys.runtime.i18n.I18nizableText;
034
035import com.fasterxml.jackson.core.JsonGenerationException;
036import com.fasterxml.jackson.core.JsonGenerator;
037import com.fasterxml.jackson.databind.SerializerProvider;
038import com.fasterxml.jackson.databind.ser.std.StdSerializer;
039
040/**
041 * Serializer for {@link I18nizable} objects.
042 * Returns the translated message for JSON value.
043 */
044public class I18nizableSerializer extends StdSerializer<I18nizable> implements Component, Serviceable, LogEnabled, Contextualizable
045{
046    /** The Avalon Role */
047    public static final String ROLE = I18nizableSerializer.class.getName();
048    
049    private I18nUtils _i18nUtils;
050    private Logger _logger;
051
052    private Context _context;
053    
054    /**
055     * Constructor
056     */
057    public I18nizableSerializer()
058    {
059        super(I18nizable.class);
060    }
061    
062    @Override
063    public void service(ServiceManager manager) throws ServiceException
064    {
065        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
066    }
067
068    @Override
069    public void contextualize(Context context) throws ContextException
070    {
071        _context = context;
072    }
073    
074    @Override
075    public void enableLogging(Logger logger)
076    {
077        _logger = logger;
078    }
079    
080    @Override
081    public void serialize(I18nizable i18nizable, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException
082    {
083        if (i18nizable instanceof I18nizableText)
084        {
085            I18nizableText value = (I18nizableText) i18nizable;
086            if (value.isI18n())
087            {
088                String msg = _i18nUtils.translate(value, _getLangCode());
089                if (msg == null)
090                {
091                    if (_logger.isWarnEnabled())
092                    {
093                        _logger.warn("Translation not found for key " + value.getKey() + " in catalogue " + value.getCatalogue());
094                    }
095                    
096                    jgen.writeString(value.getCatalogue() + ':' + value.getKey());
097                }
098                else
099                {
100                    jgen.writeString(msg);
101                }
102            }
103            else
104            {
105                jgen.writeString(value.getLabel());
106            }
107        }
108        else
109        {
110            jgen.writeString(_i18nUtils.translate(i18nizable, _getLangCode()));
111        }
112    }
113    
114    private String _getLangCode()
115    {
116        Request request = ContextHelper.getRequest(_context);
117        return request != null ? (String) request.getAttribute("locale") : null;
118    }
119}