001    /**
002     * Copyright (c) 2000-2011 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.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.theme.ThemeDisplay;
020    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
021    
022    import java.util.Map;
023    
024    /**
025     * @author Bruno Basto
026     */
027    public class StringFieldRenderer extends BaseFieldRenderer {
028    
029            @Override
030            protected String doRender(ThemeDisplay themeDisplay, Field field)
031                    throws Exception {
032    
033                    String value = String.valueOf(field.getValue());
034    
035                    DDMStructure ddmStructure = field.getDDMStructure();
036    
037                    String fieldType = ddmStructure.getFieldType(field.getName());
038    
039                    if (!fieldType.equals("radio") && !fieldType.equals("select")) {
040                            return value;
041                    }
042    
043                    String[] values = StringUtil.split(value);
044    
045                    StringBundler sb = new StringBundler(values.length * 2);
046    
047                    for (int i = 0; i < values.length; i++) {
048                            Map<String, String> fields = ddmStructure.getFields(
049                                    field.getName(), FieldConstants.VALUE, values[i]);
050    
051                            if (fields == null) {
052                                    continue;
053                            }
054    
055                            sb.append(fields.get(FieldConstants.LABEL));
056    
057                            if ((i + 1) < values.length) {
058                                    sb.append(", ");
059                            }
060                    }
061    
062                    value = sb.toString();
063    
064                    return value;
065            }
066    
067    }