001    /**
002     * Copyright (c) 2000-2013 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.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    }