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