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.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.LocaleUtil;
022    import com.liferay.portal.kernel.util.StringBundler;
023    import com.liferay.portal.model.LayoutRevision;
024    import com.liferay.portal.model.LayoutSetBranch;
025    import com.liferay.portal.model.User;
026    import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
027    import com.liferay.portal.service.LayoutSetBranchLocalServiceUtil;
028    import com.liferay.portal.service.UserLocalServiceUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.asset.model.AssetEntry;
032    import com.liferay.portlet.asset.model.AssetRenderer;
033    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
034    import com.liferay.portlet.asset.service.AssetEntryLocalServiceUtil;
035    
036    /**
037     * @author Raymond Aug??
038     */
039    public class LayoutRevisionAssetRendererFactory
040            extends BaseAssetRendererFactory {
041    
042            public static final String TYPE = "layout_revision";
043    
044            @Override
045            public AssetEntry getAssetEntry(long assetEntryId)
046                    throws PortalException, SystemException {
047    
048                    return getAssetEntry(getClassName(), assetEntryId);
049            }
050    
051            @Override
052            public AssetEntry getAssetEntry(String className, long classPK)
053                    throws PortalException, SystemException {
054    
055                    LayoutRevision layoutRevision =
056                            LayoutRevisionLocalServiceUtil.getLayoutRevision(classPK);
057    
058                    LayoutSetBranch layoutSetBranch =
059                            LayoutSetBranchLocalServiceUtil.getLayoutSetBranch(
060                                    layoutRevision.getLayoutSetBranchId());
061    
062                    User user = UserLocalServiceUtil.fetchUserById(
063                            layoutRevision.getUserId());
064    
065                    if (user == null) {
066                            if (_log.isDebugEnabled()) {
067                                    _log.debug(
068                                            "User " + layoutRevision.getUserId() + " does not exist");
069                            }
070    
071                            user = UserLocalServiceUtil.getDefaultUser(
072                                    layoutRevision.getCompanyId());
073                    }
074    
075                    AssetEntry assetEntry = AssetEntryLocalServiceUtil.createAssetEntry(
076                            classPK);
077    
078                    assetEntry.setGroupId(layoutRevision.getGroupId());
079                    assetEntry.setCompanyId(user.getCompanyId());
080                    assetEntry.setUserId(user.getUserId());
081                    assetEntry.setUserName(user.getFullName());
082                    assetEntry.setCreateDate(layoutRevision.getCreateDate());
083                    assetEntry.setClassNameId(
084                            PortalUtil.getClassNameId(LayoutRevision.class.getName()));
085                    assetEntry.setClassPK(layoutRevision.getLayoutRevisionId());
086    
087                    StringBundler sb = new StringBundler();
088    
089                    sb.append(layoutRevision.getHTMLTitle(LocaleUtil.getSiteDefault()));
090                    sb.append(" [");
091                    sb.append(layoutSetBranch.getName());
092                    sb.append("]");
093    
094                    assetEntry.setTitle(sb.toString());
095    
096                    return assetEntry;
097            }
098    
099            @Override
100            public AssetRenderer getAssetRenderer(long layoutRevisionId, int type)
101                    throws PortalException, SystemException {
102    
103                    LayoutRevision layoutRevision =
104                            LayoutRevisionLocalServiceUtil.getLayoutRevision(layoutRevisionId);
105    
106                    LayoutRevisionAssetRenderer layoutRevisionAssetRenderer =
107                            new LayoutRevisionAssetRenderer(layoutRevision);
108    
109                    layoutRevisionAssetRenderer.setAssetRendererType(type);
110    
111                    return layoutRevisionAssetRenderer;
112            }
113    
114            @Override
115            public String getClassName() {
116                    return LayoutRevision.class.getName();
117            }
118    
119            @Override
120            public String getType() {
121                    return TYPE;
122            }
123    
124            @Override
125            public boolean isCategorizable() {
126                    return false;
127            }
128    
129            @Override
130            public boolean isSelectable() {
131                    return _SELECTABLE;
132            }
133    
134            @Override
135            protected String getIconPath(ThemeDisplay themeDisplay) {
136                    return themeDisplay.getPathThemeImages() + "/common/pages.png";
137            }
138    
139            private static final boolean _SELECTABLE = false;
140    
141            private static Log _log = LogFactoryUtil.getLog(
142                    LayoutRevisionAssetRendererFactory.class);
143    
144    }