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.language.LanguageUtil;
021    import com.liferay.portal.kernel.log.Log;
022    import com.liferay.portal.kernel.log.LogFactoryUtil;
023    import com.liferay.portal.kernel.repository.model.FileEntry;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portlet.documentlibrary.NoSuchFileEntryException;
029    import com.liferay.portlet.documentlibrary.service.DLAppServiceUtil;
030    
031    import java.io.Serializable;
032    
033    /**
034     * @author Bruno Basto
035     */
036    public class DocumentLibraryFieldRenderer extends BaseFieldRenderer {
037    
038            @Override
039            protected String doRender(ThemeDisplay themeDisplay, Field field) {
040                    Serializable fieldValue = field.getValue();
041    
042                    if (Validator.isNull(fieldValue) ||
043                            fieldValue.equals(JSONFactoryUtil.getNullJSON())) {
044    
045                            return StringPool.BLANK;
046                    }
047    
048                    JSONObject fieldValueJSONObject = null;
049    
050                    try {
051                            fieldValueJSONObject = JSONFactoryUtil.createJSONObject(
052                                    String.valueOf(fieldValue));
053                    }
054                    catch (JSONException jsone) {
055                            if (_log.isDebugEnabled()) {
056                                    _log.debug("Unable to parse JSON", jsone);
057                            }
058    
059                            return StringPool.BLANK;
060                    }
061    
062                    long fileEntryGroupId = fieldValueJSONObject.getLong("groupId");
063                    String fileEntryUUID = fieldValueJSONObject.getString("uuid");
064    
065                    try {
066                            FileEntry fileEntry = DLAppServiceUtil.getFileEntryByUuidAndGroupId(
067                                    fileEntryUUID, fileEntryGroupId);
068    
069                            return fileEntry.getTitle();
070                    }
071                    catch (Exception e) {
072                            if (e instanceof NoSuchFileEntryException ||
073                                    e instanceof PrincipalException) {
074    
075                                    return LanguageUtil.format(
076                                            themeDisplay.getLocale(), "is-temporarily-unavailable",
077                                            "content");
078                            }
079                    }
080    
081                    return StringPool.BLANK;
082            }
083    
084            private static Log _log = LogFactoryUtil.getLog(
085                    DocumentLibraryFieldRenderer.class);
086    
087    }