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