001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.LocaleUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
024    import com.liferay.portlet.dynamicdatamapping.util.DDMImpl;
025    
026    import java.util.Locale;
027    import java.util.Map;
028    
029    /**
030     * @author Bruno Basto
031     */
032    public class StringFieldRenderer extends BaseFieldRenderer {
033    
034            @Override
035            protected String doRender(Field field, Locale locale) throws Exception {
036                    String value = String.valueOf(field.getValue());
037    
038                    if (Validator.isNull(value)) {
039                            return StringPool.BLANK;
040                    }
041    
042                    DDMStructure ddmStructure = field.getDDMStructure();
043    
044                    String fieldType = ddmStructure.getFieldType(field.getName());
045    
046                    if (!fieldType.equals(DDMImpl.TYPE_RADIO) &&
047                            !fieldType.equals(DDMImpl.TYPE_SELECT)) {
048    
049                            return value;
050                    }
051    
052                    JSONArray valuesJSONArray = JSONFactoryUtil.createJSONArray(value);
053    
054                    StringBundler sb = new StringBundler(valuesJSONArray.length() * 2);
055    
056                    for (int i = 0; i < valuesJSONArray.length(); i++) {
057                            Map<String, String> fieldsMap = ddmStructure.getFields(
058                                    field.getName(), FieldConstants.VALUE,
059                                    valuesJSONArray.getString(i), LocaleUtil.toLanguageId(locale));
060    
061                            if (fieldsMap == null) {
062                                    continue;
063                            }
064    
065                            sb.append(fieldsMap.get(FieldConstants.LABEL));
066    
067                            if ((i + 1) < valuesJSONArray.length()) {
068                                    sb.append(", ");
069                            }
070                    }
071    
072                    return sb.toString();
073            }
074    
075    }