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    import com.liferay.portlet.dynamicdatamapping.util.DDMImpl;
025    
026    import java.util.Map;
027    
028    /**
029     * @author Bruno Basto
030     */
031    public class StringFieldRenderer extends BaseFieldRenderer {
032    
033            @Override
034            protected String doRender(ThemeDisplay themeDisplay, Field field)
035                    throws Exception {
036    
037                    String value = String.valueOf(field.getValue());
038    
039                    if (Validator.isNull(value)) {
040                            return StringPool.BLANK;
041                    }
042    
043                    DDMStructure ddmStructure = field.getDDMStructure();
044    
045                    String fieldType = ddmStructure.getFieldType(field.getName());
046    
047                    if (!fieldType.equals(DDMImpl.TYPE_RADIO) &&
048                            !fieldType.equals(DDMImpl.TYPE_SELECT)) {
049    
050                            return value;
051                    }
052    
053                    JSONArray valuesJSONArray = JSONFactoryUtil.createJSONArray(value);
054    
055                    StringBundler sb = new StringBundler(valuesJSONArray.length() * 2);
056    
057                    for (int i = 0; i < valuesJSONArray.length(); i++) {
058                            Map<String, String> fieldsMap = ddmStructure.getFields(
059                                    field.getName(), FieldConstants.VALUE,
060                                    valuesJSONArray.getString(i));
061    
062                            if (fieldsMap == null) {
063                                    continue;
064                            }
065    
066                            sb.append(fieldsMap.get(FieldConstants.LABEL));
067    
068                            if ((i + 1) < valuesJSONArray.length()) {
069                                    sb.append(", ");
070                            }
071                    }
072    
073                    return sb.toString();
074            }
075    
076    }