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.portal.asset;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.portlet.LiferayPortletRequest;
019    import com.liferay.portal.kernel.portlet.LiferayPortletResponse;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.LayoutBranch;
025    import com.liferay.portal.model.LayoutRevision;
026    import com.liferay.portal.model.LayoutSetBranch;
027    import com.liferay.portal.service.LayoutLocalServiceUtil;
028    import com.liferay.portal.service.LayoutSetBranchLocalServiceUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.WebKeys;
032    import com.liferay.portlet.asset.model.BaseAssetRenderer;
033    
034    import java.util.Locale;
035    
036    import javax.portlet.RenderRequest;
037    import javax.portlet.RenderResponse;
038    
039    /**
040     * @author Raymond Aug??
041     */
042    public class LayoutRevisionAssetRenderer extends BaseAssetRenderer {
043    
044            public LayoutRevisionAssetRenderer(LayoutRevision layoutRevision) {
045                    _layoutRevision = layoutRevision;
046    
047                    try {
048                            _layoutBranch = layoutRevision.getLayoutBranch();
049    
050                            _layoutSetBranch =
051                                    LayoutSetBranchLocalServiceUtil.getLayoutSetBranch(
052                                            _layoutRevision.getLayoutSetBranchId());
053                    }
054                    catch (Exception e) {
055                            throw new IllegalStateException(e);
056                    }
057            }
058    
059            @Override
060            public String getClassName() {
061                    return LayoutRevision.class.getName();
062            }
063    
064            @Override
065            public long getClassPK() {
066                    return _layoutRevision.getLayoutRevisionId();
067            }
068    
069            @Override
070            public long getGroupId() {
071                    return _layoutRevision.getGroupId();
072            }
073    
074            @Override
075            public String getSummary(Locale locale) {
076                    StringBundler sb = new StringBundler(16);
077    
078                    sb.append("<strong>");
079                    sb.append(LanguageUtil.get(locale, "page"));
080                    sb.append(":</strong> ");
081                    sb.append(_layoutRevision.getHTMLTitle(locale));
082                    sb.append("<br /><strong>");
083                    sb.append(LanguageUtil.get(locale, "site-pages-variation"));
084                    sb.append(":</strong> ");
085                    sb.append(LanguageUtil.get(locale, _layoutSetBranch.getName()));
086                    sb.append("<br /><strong>");
087                    sb.append(LanguageUtil.get(locale, "page-variation"));
088                    sb.append(":</strong> ");
089                    sb.append(LanguageUtil.get(locale, _layoutBranch.getName()));
090                    sb.append("<br /><strong>");
091                    sb.append(LanguageUtil.get(locale, "revision-id"));
092                    sb.append(":</strong> ");
093                    sb.append(_layoutRevision.getLayoutRevisionId());
094    
095                    return sb.toString();
096            }
097    
098            @Override
099            public String getTitle(Locale locale) {
100                    return _layoutRevision.getHTMLTitle(locale);
101            }
102    
103            @Override
104            public String getURLViewInContext(
105                    LiferayPortletRequest liferayPortletRequest,
106                    LiferayPortletResponse liferayPortletResponse,
107                    String noSuchEntryRedirect) {
108    
109                    try {
110                            ThemeDisplay themeDisplay =
111                                    (ThemeDisplay)liferayPortletRequest.getAttribute(
112                                            WebKeys.THEME_DISPLAY);
113    
114                            Layout layout = LayoutLocalServiceUtil.getLayout(
115                                    _layoutRevision.getPlid());
116    
117                            String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
118    
119                            layoutURL = HttpUtil.addParameter(
120                                    layoutURL, "layoutSetBranchId",
121                                    _layoutRevision.getLayoutSetBranchId());
122                            layoutURL = HttpUtil.addParameter(
123                                    layoutURL, "layoutRevisionId",
124                                    _layoutRevision.getLayoutRevisionId());
125    
126                            return layoutURL;
127                    }
128                    catch (Exception e) {
129                            return StringPool.BLANK;
130                    }
131            }
132    
133            @Override
134            public long getUserId() {
135                    return _layoutRevision.getUserId();
136            }
137    
138            @Override
139            public String getUserName() {
140                    return _layoutRevision.getUserName();
141            }
142    
143            @Override
144            public String getUuid() {
145                    return null;
146            }
147    
148            @Override
149            public boolean isPreviewInContext() {
150                    return true;
151            }
152    
153            @Override
154            public String render(
155                            RenderRequest renderRequest, RenderResponse renderResponse,
156                            String template)
157                    throws Exception {
158    
159                    if (template.equals(TEMPLATE_FULL_CONTENT)) {
160                            renderRequest.setAttribute(
161                                    WebKeys.LAYOUT_REVISION, _layoutRevision);
162    
163                            return "/html/portlet/layouts_admin/asset/" + template + ".jsp";
164                    }
165                    else {
166                            return null;
167                    }
168            }
169    
170            private LayoutBranch _layoutBranch;
171            private LayoutRevision _layoutRevision;
172            private LayoutSetBranch _layoutSetBranch;
173    
174    }