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