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.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMFormField;
024    import com.liferay.portlet.dynamicdatamapping.model.DDMFormFieldOptions;
025    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
026    import com.liferay.portlet.dynamicdatamapping.model.LocalizedValue;
027    import com.liferay.portlet.dynamicdatamapping.util.DDMImpl;
028    
029    import java.io.Serializable;
030    
031    import java.util.ArrayList;
032    import java.util.List;
033    import java.util.Locale;
034    
035    /**
036     * @author Bruno Basto
037     */
038    public class StringFieldRenderer extends BaseFieldRenderer {
039    
040            @Override
041            protected String doRender(Field field, Locale locale) throws Exception {
042                    String fieldType = getFieldType(field);
043    
044                    List<String> values = new ArrayList<String>();
045    
046                    for (Serializable value : field.getValues(locale)) {
047                            String valueString = String.valueOf(value);
048    
049                            if (Validator.isNull(valueString)) {
050                                    continue;
051                            }
052    
053                            if (fieldType.equals(DDMImpl.TYPE_RADIO) ||
054                                    fieldType.equals(DDMImpl.TYPE_SELECT)) {
055    
056                                    valueString = handleJSON(field, valueString, locale);
057                            }
058    
059                            values.add(valueString);
060                    }
061    
062                    return StringUtil.merge(values, StringPool.COMMA_AND_SPACE);
063            }
064    
065            @Override
066            protected String doRender(Field field, Locale locale, int valueIndex)
067                    throws Exception {
068    
069                    Serializable value = field.getValue(locale, valueIndex);
070    
071                    if (Validator.isNull(value)) {
072                            return StringPool.BLANK;
073                    }
074    
075                    String fieldType = getFieldType(field);
076    
077                    if (fieldType.equals(DDMImpl.TYPE_RADIO) ||
078                            fieldType.equals(DDMImpl.TYPE_SELECT)) {
079    
080                            return handleJSON(field, String.valueOf(value), locale);
081                    }
082    
083                    return String.valueOf(value);
084            }
085    
086            protected LocalizedValue getFieldOptionLabel(
087                            Field field, String optionValue)
088                    throws Exception {
089    
090                    DDMStructure ddmStructure = field.getDDMStructure();
091    
092                    DDMFormField ddmFormField = ddmStructure.getDDMFormField(
093                            field.getName());
094    
095                    DDMFormFieldOptions ddmFormFieldOptions =
096                            ddmFormField.getDDMFormFieldOptions();
097    
098                    return ddmFormFieldOptions.getOptionLabels(optionValue);
099            }
100    
101            protected String getFieldType(Field field) throws Exception {
102                    DDMStructure ddmStructure = field.getDDMStructure();
103    
104                    return ddmStructure.getFieldType(field.getName());
105            }
106    
107            protected String handleJSON(Field field, String json, Locale locale)
108                    throws Exception {
109    
110                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray(json);
111    
112                    StringBundler sb = new StringBundler(jsonArray.length() * 2);
113    
114                    for (int i = 0; i < jsonArray.length(); i++) {
115                            LocalizedValue label = getFieldOptionLabel(
116                                    field, jsonArray.getString(i));
117    
118                            if (label == null) {
119                                    continue;
120                            }
121    
122                            sb.append(label.getString(locale));
123    
124                            if ((i + 1) < jsonArray.length()) {
125                                    sb.append(StringPool.COMMA_AND_SPACE);
126                            }
127                    }
128    
129                    return sb.toString();
130            }
131    
132    }