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.JSONArray;
018    import com.liferay.portal.kernel.json.JSONException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portlet.dynamicdatamapping.model.DDMFormField;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMFormFieldOptions;
024    import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
025    import com.liferay.portlet.dynamicdatamapping.model.Value;
026    import com.liferay.portlet.dynamicdatamapping.storage.DDMFormFieldValue;
027    
028    import java.util.Locale;
029    
030    /**
031     * @author Bruno Basto
032     * @author Marcellus Tavares
033     */
034    public abstract class BaseListDDMFormFieldValueRenderer
035            extends BaseDDMFormFieldValueRenderer {
036    
037            @Override
038            protected ValueAccessor getValueAcessor(Locale locale) {
039                    return new ValueAccessor(locale) {
040    
041                            @Override
042                            public String get(DDMFormFieldValue ddmFormFieldValue) {
043                                    Value value = ddmFormFieldValue.getValue();
044    
045                                    JSONArray jsonArray = createJSONArray(value.getString(locale));
046    
047                                    StringBundler sb = new StringBundler(jsonArray.length() * 2);
048    
049                                    for (int i = 0; i < jsonArray.length(); i++) {
050                                            LocalizedValue label = getDDMFormFieldOptionLabel(
051                                                    ddmFormFieldValue, jsonArray.getString(i));
052    
053                                            if (label == null) {
054                                                    continue;
055                                            }
056    
057                                            sb.append(label.getString(locale));
058    
059                                            if ((i + 1) < jsonArray.length()) {
060                                                    sb.append(StringPool.COMMA_AND_SPACE);
061                                            }
062                                    }
063    
064                                    return sb.toString();
065                            }
066    
067                            protected JSONArray createJSONArray(String json) {
068                                    try {
069                                            return JSONFactoryUtil.createJSONArray(json);
070                                    }
071                                    catch (JSONException jsone) {
072                                            throw new ValueAccessorException(jsone);
073                                    }
074                            }
075    
076                            protected LocalizedValue getDDMFormFieldOptionLabel(
077                                    DDMFormFieldValue ddmFormFieldValue, String optionValue) {
078    
079                                    DDMFormField ddmFormField = getDDMFormField(ddmFormFieldValue);
080    
081                                    DDMFormFieldOptions ddmFormFieldOptions =
082                                            ddmFormField.getDDMFormFieldOptions();
083    
084                                    return ddmFormFieldOptions.getOptionLabels(optionValue);
085                            }
086    
087                    };
088            }
089    
090    }