001    /**
002     * Copyright (c) 2000-2011 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.portlet.wiki.asset;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.security.permission.PermissionChecker;
020    import com.liferay.portal.theme.ThemeDisplay;
021    import com.liferay.portlet.asset.model.AssetRenderer;
022    import com.liferay.portlet.asset.model.BaseAssetRendererFactory;
023    import com.liferay.portlet.wiki.NoSuchPageException;
024    import com.liferay.portlet.wiki.model.WikiPage;
025    import com.liferay.portlet.wiki.model.WikiPageResource;
026    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
027    import com.liferay.portlet.wiki.service.WikiPageResourceLocalServiceUtil;
028    import com.liferay.portlet.wiki.service.permission.WikiPagePermission;
029    
030    /**
031     * @author Julio Camarero
032     * @author Juan Fernández
033     * @author Jorge Ferrer
034     * @author Raymond Augé
035     * @author Sergio González
036     */
037    public class WikiPageAssetRendererFactory extends BaseAssetRendererFactory {
038    
039            public static final String CLASS_NAME = WikiPage.class.getName();
040    
041            public static final String TYPE = "wiki";
042    
043            public AssetRenderer getAssetRenderer(long classPK, int type)
044                    throws PortalException, SystemException {
045    
046                    WikiPage page = null;
047    
048                    try {
049                            page = WikiPageLocalServiceUtil.getWikiPage(classPK);
050                    }
051                    catch (NoSuchPageException nspe) {
052                            if (type == TYPE_LATEST_APPROVED) {
053                                    page = WikiPageLocalServiceUtil.getPage(classPK);
054                            }
055                            else {
056                                    WikiPageResource wikiPageResource =
057                                            WikiPageResourceLocalServiceUtil.getPageResource(
058                                                    classPK);
059    
060                                    page = WikiPageLocalServiceUtil.getPage(
061                                            wikiPageResource.getNodeId(),
062                                            wikiPageResource.getTitle(), null);
063                            }
064                    }
065    
066                    return new WikiPageAssetRenderer(page);
067            }
068    
069            public String getClassName() {
070                    return CLASS_NAME;
071            }
072    
073            public String getType() {
074                    return TYPE;
075            }
076    
077            @Override
078            public boolean hasPermission(
079                            PermissionChecker permissionChecker, long classPK, String actionId)
080                    throws Exception {
081    
082                    return WikiPagePermission.contains(
083                            permissionChecker, classPK, actionId);
084            }
085    
086            @Override
087            public boolean isLinkable() {
088                    return _LINKABLE;
089            }
090    
091            @Override
092            protected String getIconPath(ThemeDisplay themeDisplay) {
093                    return themeDisplay.getPathThemeImages() + "/common/pages.png";
094            }
095    
096            private static final boolean _LINKABLE = true;
097    
098    }