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