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.json.JSONException;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.language.LanguageUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMFormFieldType;
024    import com.liferay.portlet.dynamicdatamapping.model.Value;
025    import com.liferay.portlet.dynamicdatamapping.storage.DDMFormFieldValue;
026    
027    import java.text.NumberFormat;
028    
029    import java.util.Locale;
030    
031    /**
032     * @author Sergio Gonz??lez
033     * @author Marcellus Tavares
034     */
035    public class GeolocationDDMFormFieldValueRenderer
036            extends BaseDDMFormFieldValueRenderer {
037    
038            @Override
039            public String getSupportedDDMFormFieldType() {
040                    return DDMFormFieldType.GEOLOCATION;
041            }
042    
043            @Override
044            protected ValueAccessor getValueAcessor(Locale locale) {
045                    return new ValueAccessor(locale) {
046    
047                            @Override
048                            public String get(DDMFormFieldValue ddmFormFieldValue) {
049                                    Value value = ddmFormFieldValue.getValue();
050    
051                                    JSONObject jsonObject = createJSONObject(
052                                            value.getString(locale));
053    
054                                    StringBundler sb = new StringBundler(7);
055    
056                                    sb.append(LanguageUtil.get(locale, "latitude"));
057                                    sb.append(": ");
058    
059                                    double latitude = jsonObject.getDouble("latitude");
060    
061                                    NumberFormat numberFormat = NumberFormat.getNumberInstance(
062                                            locale);
063    
064                                    sb.append(numberFormat.format(latitude));
065    
066                                    sb.append(StringPool.COMMA_AND_SPACE);
067                                    sb.append(LanguageUtil.get(locale, "longitude"));
068                                    sb.append(": ");
069    
070                                    double longitude = jsonObject.getDouble("longitude");
071    
072                                    sb.append(numberFormat.format(longitude));
073    
074                                    return sb.toString();
075                            }
076    
077                            protected JSONObject createJSONObject(String json) {
078                                    try {
079                                            return JSONFactoryUtil.createJSONObject(json);
080                                    }
081                                    catch (JSONException jsone) {
082                                            throw new ValueAccessorException(jsone);
083                                    }
084                            }
085    
086                    };
087            }
088    
089    }