001    /**
002     * Copyright (c) 2000-2012 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.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(classPK);
058    
059                                    page = WikiPageLocalServiceUtil.getPage(
060                                            wikiPageResource.getNodeId(), wikiPageResource.getTitle(),
061                                            null);
062                            }
063                    }
064    
065                    return new WikiPageAssetRenderer(page);
066            }
067    
068            public String getClassName() {
069                    return CLASS_NAME;
070            }
071    
072            public String getType() {
073                    return TYPE;
074            }
075    
076            @Override
077            public boolean hasPermission(
078                            PermissionChecker permissionChecker, long classPK, String actionId)
079                    throws Exception {
080    
081                    return WikiPagePermission.contains(
082                            permissionChecker, classPK, actionId);
083            }
084    
085            @Override
086            public boolean isLinkable() {
087                    return _LINKABLE;
088            }
089    
090            @Override
091            protected String getIconPath(ThemeDisplay themeDisplay) {
092                    return themeDisplay.getPathThemeImages() + "/common/pages.png";
093            }
094    
095            private static final boolean _LINKABLE = true;
096    
097    }