001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }