001    /**
002     * Copyright (c) 2000-present 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.render;
016    
017    import com.liferay.portal.kernel.util.FastDateFormatFactoryUtil;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portlet.dynamicdatamapping.model.DDMFormFieldType;
021    import com.liferay.portlet.dynamicdatamapping.model.Value;
022    import com.liferay.portlet.dynamicdatamapping.storage.DDMFormFieldValue;
023    
024    import java.text.Format;
025    
026    import java.util.Locale;
027    
028    /**
029     * @author Bruno Basto
030     * @author Marcellus Tavares
031     */
032    public class DateDDMFormFieldValueRenderer
033            extends BaseDDMFormFieldValueRenderer {
034    
035            @Override
036            public String getSupportedDDMFormFieldType() {
037                    return DDMFormFieldType.DATE;
038            }
039    
040            @Override
041            protected ValueAccessor getValueAcessor(Locale locale) {
042                    return new ValueAccessor(locale) {
043    
044                            @Override
045                            public String get(DDMFormFieldValue ddmFormFieldValue) {
046                                    Value value = ddmFormFieldValue.getValue();
047    
048                                    String valueString = value.getString(locale);
049    
050                                    if (Validator.isNull(valueString)) {
051                                            return StringPool.BLANK;
052                                    }
053    
054                                    long valueLong = Long.valueOf(valueString);
055    
056                                    Format format = FastDateFormatFactoryUtil.getDate(locale);
057    
058                                    return format.format(valueLong);
059                            }
060    
061                    };
062            }
063    
064    }