001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 int getStatus() {
076                    return _layoutRevision.getStatus();
077            }
078    
079            @Override
080            public String getSummary(Locale locale) {
081                    StringBundler sb = new StringBundler(16);
082    
083                    sb.append("<strong>");
084                    sb.append(LanguageUtil.get(locale, "page"));
085                    sb.append(":</strong> ");
086                    sb.append(_layoutRevision.getHTMLTitle(locale));
087                    sb.append("<br /><strong>");
088                    sb.append(LanguageUtil.get(locale, "site-pages-variation"));
089                    sb.append(":</strong> ");
090                    sb.append(LanguageUtil.get(locale, _layoutSetBranch.getName()));
091                    sb.append("<br /><strong>");
092                    sb.append(LanguageUtil.get(locale, "page-variation"));
093                    sb.append(":</strong> ");
094                    sb.append(LanguageUtil.get(locale, _layoutBranch.getName()));
095                    sb.append("<br /><strong>");
096                    sb.append(LanguageUtil.get(locale, "revision-id"));
097                    sb.append(":</strong> ");
098                    sb.append(_layoutRevision.getLayoutRevisionId());
099    
100                    return sb.toString();
101            }
102    
103            @Override
104            public String getTitle(Locale locale) {
105                    return _layoutRevision.getHTMLTitle(locale);
106            }
107    
108            @Override
109            public String getURLViewInContext(
110                    LiferayPortletRequest liferayPortletRequest,
111                    LiferayPortletResponse liferayPortletResponse,
112                    String noSuchEntryRedirect) {
113    
114                    try {
115                            ThemeDisplay themeDisplay =
116                                    (ThemeDisplay)liferayPortletRequest.getAttribute(
117                                            WebKeys.THEME_DISPLAY);
118    
119                            Layout layout = LayoutLocalServiceUtil.getLayout(
120                                    _layoutRevision.getPlid());
121    
122                            String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
123    
124                            layoutURL = HttpUtil.addParameter(
125                                    layoutURL, "layoutSetBranchId",
126                                    _layoutRevision.getLayoutSetBranchId());
127                            layoutURL = HttpUtil.addParameter(
128                                    layoutURL, "layoutRevisionId",
129                                    _layoutRevision.getLayoutRevisionId());
130    
131                            return layoutURL;
132                    }
133                    catch (Exception e) {
134                            return StringPool.BLANK;
135                    }
136            }
137    
138            @Override
139            public long getUserId() {
140                    return _layoutRevision.getUserId();
141            }
142    
143            @Override
144            public String getUserName() {
145                    return _layoutRevision.getUserName();
146            }
147    
148            @Override
149            public String getUuid() {
150                    return null;
151            }
152    
153            @Override
154            public boolean isPreviewInContext() {
155                    return true;
156            }
157    
158            @Override
159            public String render(
160                            RenderRequest renderRequest, RenderResponse renderResponse,
161                            String template)
162                    throws Exception {
163    
164                    if (template.equals(TEMPLATE_FULL_CONTENT)) {
165                            renderRequest.setAttribute(
166                                    WebKeys.LAYOUT_REVISION, _layoutRevision);
167    
168                            return "/html/portlet/layouts_admin/asset/" + template + ".jsp";
169                    }
170                    else {
171                            return null;
172                    }
173            }
174    
175            private LayoutBranch _layoutBranch;
176            private LayoutRevision _layoutRevision;
177            private LayoutSetBranch _layoutSetBranch;
178    
179    }