001    /**
002     * Copyright (c) 2000-present 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.render;
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.repository.model.FileEntry;
022    import com.liferay.portlet.documentlibrary.service.DLAppLocalServiceUtil;
023    import com.liferay.portlet.dynamicdatamapping.model.DDMFormFieldType;
024    import com.liferay.portlet.dynamicdatamapping.model.Value;
025    import com.liferay.portlet.dynamicdatamapping.storage.DDMFormFieldValue;
026    
027    import java.util.Locale;
028    
029    /**
030     * @author Bruno Basto
031     * @author Marcellus Tavares
032     */
033    public class DocumentLibraryDDMFormFieldValueRenderer
034            extends BaseDDMFormFieldValueRenderer {
035    
036            @Override
037            public String getSupportedDDMFormFieldType() {
038                    return DDMFormFieldType.DOCUMENT_LIBRARY;
039            }
040    
041            @Override
042            protected ValueAccessor getValueAcessor(Locale locale) {
043                    return new ValueAccessor(locale) {
044    
045                            @Override
046                            public String get(DDMFormFieldValue ddmFormFieldValue) {
047                                    Value value = ddmFormFieldValue.getValue();
048    
049                                    JSONObject jsonObject = createJSONObject(
050                                            value.getString(locale));
051    
052                                    String uuid = jsonObject.getString("uuid");
053                                    long groupId = jsonObject.getLong("groupId");
054    
055                                    try {
056                                            FileEntry fileEntry =
057                                                    DLAppLocalServiceUtil.getFileEntryByUuidAndGroupId(
058                                                            uuid, groupId);
059    
060                                            return fileEntry.getTitle();
061                                    }
062                                    catch (Exception e) {
063                                            return LanguageUtil.format(
064                                                    locale, "is-temporarily-unavailable", "content");
065                                    }
066                            }
067    
068                            protected JSONObject createJSONObject(String json) {
069                                    try {
070                                            return JSONFactoryUtil.createJSONObject(json);
071                                    }
072                                    catch (JSONException jsone) {
073                                            throw new ValueAccessorException(jsone);
074                                    }
075                            }
076    
077                    };
078            }
079    
080    }