001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.storage;
016    
017    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.io.Serializable;
023    
024    import java.text.Format;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    import java.util.Locale;
029    
030    /**
031     * @author Bruno Basto
032     * @author Manuel de la Pe??a
033     */
034    public class DateFieldRenderer extends BaseFieldRenderer {
035    
036            @Override
037            protected String doRender(Field field, Locale locale) throws Exception {
038                    Format format = FastDateFormatFactoryUtil.getDate(locale);
039    
040                    List<String> values = new ArrayList<String>();
041    
042                    for (Serializable value : field.getValues(locale)) {
043                            if (Validator.isNull(value)) {
044                                    continue;
045                            }
046    
047                            values.add(format.format(value));
048                    }
049    
050                    return StringUtil.merge(values, StringPool.COMMA_AND_SPACE);
051            }
052    
053            @Override
054            protected String doRender(Field field, Locale locale, int valueIndex) {
055                    Serializable value = field.getValue(locale, valueIndex);
056    
057                    if (Validator.isNull(value)) {
058                            return StringPool.BLANK;
059                    }
060    
061                    Format format = FastDateFormatFactoryUtil.getDate(locale);
062    
063                    return format.format(value);
064            }
065    
066    }