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.exception.PortalException;
018    import com.liferay.portal.kernel.json.JSONException;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.language.LanguageUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.service.LayoutServiceUtil;
025    import com.liferay.portlet.dynamicdatamapping.model.DDMFormFieldType;
026    import com.liferay.portlet.dynamicdatamapping.model.Value;
027    import com.liferay.portlet.dynamicdatamapping.storage.DDMFormFieldValue;
028    
029    import java.util.Locale;
030    
031    /**
032     * @author Bruno Basto
033     */
034    public class LinkToPageDDMFormFieldValueRenderer
035            extends BaseDDMFormFieldValueRenderer {
036    
037            @Override
038            public String getSupportedDDMFormFieldType() {
039                    return DDMFormFieldType.LINK_TO_PAGE;
040            }
041    
042            @Override
043            protected ValueAccessor getValueAcessor(Locale locale) {
044                    return new ValueAccessor(locale) {
045    
046                            @Override
047                            public String get(DDMFormFieldValue ddmFormFieldValue) {
048                                    Value value = ddmFormFieldValue.getValue();
049    
050                                    JSONObject jsonObject = createJSONObject(
051                                            value.getString(locale));
052    
053                                    long groupId = jsonObject.getLong("groupId");
054                                    boolean privateLayout = jsonObject.getBoolean("privateLayout");
055                                    long layoutId = jsonObject.getLong("layoutId");
056    
057                                    try {
058                                            return LayoutServiceUtil.getLayoutName(
059                                                    groupId, privateLayout, layoutId,
060                                                    LanguageUtil.getLanguageId(locale));
061                                    }
062                                    catch (PortalException pe) {
063                                            if (_log.isWarnEnabled()) {
064                                                    _log.warn(pe, pe);
065                                            }
066    
067                                            return LanguageUtil.format(
068                                                    locale, "is-temporarily-unavailable", "content");
069                                    }
070                            }
071    
072                            protected JSONObject createJSONObject(String json) {
073                                    try {
074                                            return JSONFactoryUtil.createJSONObject(json);
075                                    }
076                                    catch (JSONException jsone) {
077                                            throw new ValueAccessorException(jsone);
078                                    }
079                            }
080    
081                    };
082            }
083    
084            private static final Log _log = LogFactoryUtil.getLog(
085                    LinkToPageDDMFormFieldValueRenderer.class);
086    
087    }