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.service.LayoutServiceUtil;
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     */
032    public class LinkToPageDDMFormFieldValueRenderer
033            extends BaseDDMFormFieldValueRenderer {
034    
035            @Override
036            public String getSupportedDDMFormFieldType() {
037                    return DDMFormFieldType.LINK_TO_PAGE;
038            }
039    
040            @Override
041            protected ValueAccessor getValueAcessor(Locale locale) {
042                    return new ValueAccessor(locale) {
043    
044                            @Override
045                            public String get(DDMFormFieldValue ddmFormFieldValue) {
046                                    Value value = ddmFormFieldValue.getValue();
047    
048                                    JSONObject jsonObject = createJSONObject(
049                                            value.getString(locale));
050    
051                                    long groupId = jsonObject.getLong("groupId");
052                                    boolean privateLayout = jsonObject.getBoolean("privateLayout");
053                                    long layoutId = jsonObject.getLong("layoutId");
054    
055                                    try {
056                                            return LayoutServiceUtil.getLayoutName(
057                                                    groupId, privateLayout, layoutId,
058                                                    LanguageUtil.getLanguageId(locale));
059                                    }
060                                    catch (PortalException pe) {
061                                            return LanguageUtil.format(
062                                                    locale, "is-temporarily-unavailable", "content");
063                                    }
064                            }
065    
066                            protected JSONObject createJSONObject(String json) {
067                                    try {
068                                            return JSONFactoryUtil.createJSONObject(json);
069                                    }
070                                    catch (JSONException jsone) {
071                                            throw new ValueAccessorException(jsone);
072                                    }
073                            }
074    
075                    };
076            }
077    
078    }