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            @Override
041            public AssetRenderer getAssetRenderer(long classPK, int type)
042                    throws PortalException, SystemException {
043    
044                    WikiPage page = WikiPageLocalServiceUtil.fetchWikiPage(classPK);
045    
046                    if (page == null) {
047                            if (type == TYPE_LATEST_APPROVED) {
048                                    page = WikiPageLocalServiceUtil.getPage(classPK);
049                            }
050                            else {
051                                    WikiPageResource pageResource =
052                                            WikiPageResourceLocalServiceUtil.getPageResource(classPK);
053    
054                                    page = WikiPageLocalServiceUtil.getPage(
055                                            pageResource.getNodeId(), pageResource.getTitle(), null);
056                            }
057                    }
058    
059                    WikiPageAssetRenderer wikiPageAssetRenderer = new WikiPageAssetRenderer(
060                            page);
061    
062                    wikiPageAssetRenderer.setAssetRendererType(type);
063    
064                    return wikiPageAssetRenderer;
065            }
066    
067            @Override
068            public String getClassName() {
069                    return WikiPage.class.getName();
070            }
071    
072            @Override
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    }