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    /** Request attribut for locale */
047    public static final String REQUEST_ATTR_LOCALE = "locale";
048    
049    /** The Avalon Role */
050    public static final String ROLE = I18nizableSerializer.class.getName();
051    
052    private I18nUtils _i18nUtils;
053    private Logger _logger;
054
055    private Context _context;
056    
057    /**
058     * Constructor
059     */
060    public I18nizableSerializer()
061    {
062        super(I18nizable.class);
063    }
064    
065    @Override
066    public void service(ServiceManager manager) throws ServiceException
067    {
068        _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE);
069    }
070
071    @Override
072    public void contextualize(Context context) throws ContextException
073    {
074        _context = context;
075    }
076    
077    @Override
078    public void enableLogging(Logger logger)
079    {
080        _logger = logger;
081    }
082    
083    @Override
084    public void serialize(I18nizable i18nizable, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonGenerationException
085    {
086        if (i18nizable instanceof I18nizableText)
087        {
088            I18nizableText value = (I18nizableText) i18nizable;
089            if (value.isI18n())
090            {
091                String msg = _i18nUtils.translate(value, _getLangCode());
092                if (msg == null)
093                {
094                    if (_logger.isWarnEnabled())
095                    {
096                        _logger.warn("Translation not found for key " + value.getKey() + " in catalogue " + value.getCatalogue());
097                    }
098                    
099                    jgen.writeString(value.getCatalogue() + ':' + value.getKey());
100                }
101                else
102                {
103                    jgen.writeString(msg);
104                }
105            }
106            else
107            {
108                jgen.writeString(value.getLabel());
109            }
110        }
111        else
112        {
113            jgen.writeString(_i18nUtils.translate(i18nizable, _getLangCode()));
114        }
115    }
116    
117    private String _getLangCode()
118    {
119        Request request = ContextHelper.getRequest(_context);
120        return request != null ? (String) request.getAttribute(REQUEST_ATTR_LOCALE) : null;
121    }
122}