001    /**
002     * Copyright (c) 2000-2011 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.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(
040                    ThemeDisplay themeDisplay, Serializable fieldValue) {
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    }