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.JSONException;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.Validator;
025    
026    import java.io.Serializable;
027    
028    import java.util.ArrayList;
029    import java.util.List;
030    import java.util.Locale;
031    
032    /**
033     * @author Bruno Basto
034     */
035    public class FileUploadFieldRenderer extends BaseFieldRenderer {
036    
037            @Override
038            protected String doRender(Field field, Locale locale) throws Exception {
039                    List<String> values = new ArrayList<String>();
040    
041                    for (Serializable value : field.getValues(locale)) {
042                            String valueString = String.valueOf(value);
043    
044                            if (Validator.isNull(valueString)) {
045                                    continue;
046                            }
047    
048                            values.add(handleJSON(valueString));
049                    }
050    
051                    return StringUtil.merge(values, StringPool.COMMA_AND_SPACE);
052            }
053    
054            @Override
055            protected String doRender(Field field, Locale locale, int valueIndex) {
056                    Serializable value = field.getValue(locale, valueIndex);
057    
058                    if (Validator.isNull(value)) {
059                            return StringPool.BLANK;
060                    }
061    
062                    return handleJSON(String.valueOf(value));
063            }
064    
065            protected String handleJSON(String json) {
066                    JSONObject jsonObject = null;
067    
068                    try {
069                            jsonObject = JSONFactoryUtil.createJSONObject(json);
070                    }
071                    catch (JSONException jsone) {
072                            if (_log.isDebugEnabled()) {
073                                    _log.debug("Unable to parse JSON", jsone);
074                            }
075    
076                            return StringPool.BLANK;
077                    }
078    
079                    return jsonObject.getString("name");
080            }
081    
082            private static Log _log = LogFactoryUtil.getLog(
083                    FileUploadFieldRenderer.class);
084    
085    }