001    /**
002     * Copyright (c) 2000-2012 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.Validator;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
024    
025    import java.util.Map;
026    
027    /**
028     * @author Bruno Basto
029     */
030    public class StringFieldRenderer extends BaseFieldRenderer {
031    
032            @Override
033            protected String doRender(ThemeDisplay themeDisplay, Field field)
034                    throws Exception {
035    
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("radio") && !fieldType.equals("select")) {
047                            return value;
048                    }
049    
050                    JSONArray valuesJSONArray = JSONFactoryUtil.createJSONArray(value);
051    
052                    StringBundler sb = new StringBundler(valuesJSONArray.length() * 2);
053    
054                    for (int i = 0; i < valuesJSONArray.length(); i++) {
055                            Map<String, String> fieldsMap = ddmStructure.getFields(
056                                    field.getName(), FieldConstants.VALUE,
057                                    valuesJSONArray.getString(i));
058    
059                            if (fieldsMap == null) {
060                                    continue;
061                            }
062    
063                            sb.append(fieldsMap.get(FieldConstants.LABEL));
064    
065                            if ((i + 1) < valuesJSONArray.length()) {
066                                    sb.append(", ");
067                            }
068                    }
069    
070                    return sb.toString();
071            }
072    
073    }