001    /**
002     * Copyright (c) 2000-2012 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.NoSuchLayoutException;
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.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.LayoutServiceUtil;
028    
029    import java.io.Serializable;
030    
031    import java.util.Locale;
032    
033    /**
034     * @author Bruno Basto
035     */
036    public class LinkToPageFieldRenderer extends BaseFieldRenderer {
037    
038            @Override
039            protected String doRender(Field field, Locale locale) {
040                    Serializable fieldValue = field.getValue();
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 groupId = fieldValueJSONObject.getLong("groupId");
063                    boolean privateLayout = fieldValueJSONObject.getBoolean(
064                            "privateLayout");
065                    long layoutId = fieldValueJSONObject.getLong("layoutId");
066    
067                    try {
068                            return LayoutServiceUtil.getLayoutName(
069                                    groupId, privateLayout, layoutId,
070                                    LanguageUtil.getLanguageId(locale));
071                    }
072                    catch (Exception e) {
073                            if (e instanceof NoSuchLayoutException ||
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                    LinkToPageFieldRenderer.class);
086    
087    }