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.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
025    import com.liferay.portlet.dynamicdatamapping.util.DDMImpl;
026    
027    import java.io.Serializable;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    import java.util.Locale;
032    import java.util.Map;
033    
034    /**
035     * @author Bruno Basto
036     */
037    public class StringFieldRenderer extends BaseFieldRenderer {
038    
039            @Override
040            protected String doRender(Field field, Locale locale) throws Exception {
041                    String fieldType = getFieldType(field);
042    
043                    List<String> values = new ArrayList<String>();
044    
045                    for (Serializable value : field.getValues(locale)) {
046                            String valueString = String.valueOf(value);
047    
048                            if (Validator.isNull(valueString)) {
049                                    continue;
050                            }
051    
052                            if (fieldType.equals(DDMImpl.TYPE_RADIO) ||
053                                    fieldType.equals(DDMImpl.TYPE_SELECT)) {
054    
055                                    valueString = handleJSON(field, valueString, locale);
056                            }
057    
058                            values.add(valueString);
059                    }
060    
061                    return StringUtil.merge(values, StringPool.COMMA_AND_SPACE);
062            }
063    
064            @Override
065            protected String doRender(Field field, Locale locale, int valueIndex)
066                    throws Exception {
067    
068                    Serializable value = field.getValue(locale, valueIndex);
069    
070                    if (Validator.isNull(value)) {
071                            return StringPool.BLANK;
072                    }
073    
074                    String fieldType = getFieldType(field);
075    
076                    if (fieldType.equals(DDMImpl.TYPE_RADIO) ||
077                            fieldType.equals(DDMImpl.TYPE_SELECT)) {
078    
079                            return handleJSON(field, String.valueOf(value), locale);
080                    }
081    
082                    return String.valueOf(value);
083            }
084    
085            protected String getFieldType(Field field) throws Exception {
086                    DDMStructure ddmStructure = field.getDDMStructure();
087    
088                    return ddmStructure.getFieldType(field.getName());
089            }
090    
091            protected String handleJSON(Field field, String json, Locale locale)
092                    throws Exception {
093    
094                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray(json);
095    
096                    DDMStructure ddmStructure = field.getDDMStructure();
097    
098                    StringBundler sb = new StringBundler(jsonArray.length() * 2);
099    
100                    for (int i = 0; i < jsonArray.length(); i++) {
101                            Map<String, String> fieldsMap = ddmStructure.getFields(
102                                    field.getName(), FieldConstants.VALUE, jsonArray.getString(i),
103                                    LocaleUtil.toLanguageId(locale));
104    
105                            if (fieldsMap == null) {
106                                    continue;
107                            }
108    
109                            sb.append(fieldsMap.get(FieldConstants.LABEL));
110    
111                            if ((i + 1) < jsonArray.length()) {
112                                    sb.append(StringPool.COMMA_AND_SPACE);
113                            }
114                    }
115    
116                    return sb.toString();
117            }
118    
119    }