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.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.Validator;
024    
025    import java.io.Serializable;
026    
027    import java.util.Locale;
028    
029    /**
030     * @author Bruno Basto
031     */
032    public class FileUploadFieldRenderer extends BaseFieldRenderer {
033    
034            @Override
035            protected String doRender(Field field, Locale locale) {
036                    Serializable fieldValue = field.getValue();
037    
038                    if (Validator.isNull(fieldValue) ||
039                            fieldValue.equals(JSONFactoryUtil.getNullJSON())) {
040    
041                            return StringPool.BLANK;
042                    }
043    
044                    JSONObject fieldValueJSONObject = null;
045    
046                    try {
047                            fieldValueJSONObject = JSONFactoryUtil.createJSONObject(
048                                    String.valueOf(fieldValue));
049                    }
050                    catch (JSONException jsone) {
051                            if (_log.isDebugEnabled()) {
052                                    _log.debug("Unable to parse JSON", jsone);
053                            }
054    
055                            return StringPool.BLANK;
056                    }
057    
058                    return fieldValueJSONObject.getString("name");
059            }
060    
061            private static Log _log = LogFactoryUtil.getLog(
062                    FileUploadFieldRenderer.class);
063    
064    }