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.storage;
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.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    
028    import java.io.Serializable;
029    
030    import java.text.NumberFormat;
031    
032    import java.util.ArrayList;
033    import java.util.List;
034    import java.util.Locale;
035    
036    /**
037     * @author Sergio Gonz??lez
038     */
039    public class GeolocationFieldRenderer extends BaseFieldRenderer {
040    
041            @Override
042            protected String doRender(Field field, Locale locale) throws Exception {
043                    List<String> values = new ArrayList<>();
044    
045                    for (Serializable value : field.getValues(locale)) {
046                            String valueString = String.valueOf(value);
047    
048                            if (Validator.isNull(valueString)) {
049                                    continue;
050                            }
051    
052                            values.add(handleJSON(valueString, locale));
053                    }
054    
055                    return StringUtil.merge(values, StringPool.COMMA_AND_SPACE);
056            }
057    
058            @Override
059            protected String doRender(Field field, Locale locale, int valueIndex) {
060                    Serializable value = field.getValue(locale, valueIndex);
061    
062                    if (Validator.isNull(value)) {
063                            return StringPool.BLANK;
064                    }
065    
066                    return handleJSON(String.valueOf(value), locale);
067            }
068    
069            protected String handleJSON(String value, Locale locale) {
070                    JSONObject jsonObject = null;
071    
072                    try {
073                            jsonObject = JSONFactoryUtil.createJSONObject(value);
074                    }
075                    catch (JSONException jsone) {
076                            if (_log.isDebugEnabled()) {
077                                    _log.debug("Unable to parse JSON", jsone);
078                            }
079    
080                            return StringPool.BLANK;
081                    }
082    
083                    StringBundler sb = new StringBundler(7);
084    
085                    sb.append(LanguageUtil.get(locale, "latitude"));
086                    sb.append(": ");
087    
088                    double latitude = jsonObject.getDouble("latitude");
089    
090                    NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
091    
092                    sb.append(numberFormat.format(latitude));
093    
094                    sb.append(StringPool.COMMA_AND_SPACE);
095                    sb.append(LanguageUtil.get(locale, "longitude"));
096                    sb.append(": ");
097    
098                    double longitude = jsonObject.getDouble("longitude");
099    
100                    sb.append(numberFormat.format(longitude));
101    
102                    return sb.toString();
103            }
104    
105            private static final Log _log = LogFactoryUtil.getLog(
106                    GeolocationFieldRenderer.class);
107    
108    }