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.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            @Override
044            public AssetRenderer getAssetRenderer(long classPK, int type)
045                    throws PortalException, SystemException {
046    
047                    WikiPage page = null;
048    
049                    try {
050                            page = WikiPageLocalServiceUtil.getWikiPage(classPK);
051                    }
052                    catch (NoSuchPageException nspe) {
053                            if (type == TYPE_LATEST_APPROVED) {
054                                    page = WikiPageLocalServiceUtil.getPage(classPK);
055                            }
056                            else {
057                                    WikiPageResource wikiPageResource =
058                                            WikiPageResourceLocalServiceUtil.getPageResource(classPK);
059    
060                                    page = WikiPageLocalServiceUtil.getPage(
061                                            wikiPageResource.getNodeId(), wikiPageResource.getTitle(),
062                                            null);
063                            }
064                    }
065    
066                    return new WikiPageAssetRenderer(page);
067            }
068    
069            @Override
070            public String getClassName() {
071                    return CLASS_NAME;
072            }
073    
074            @Override
075            public String getType() {
076                    return TYPE;
077            }
078    
079            @Override
080            public boolean hasPermission(
081                            PermissionChecker permissionChecker, long classPK, String actionId)
082                    throws Exception {
083    
084                    return WikiPagePermission.contains(
085                            permissionChecker, classPK, actionId);
086            }
087    
088            @Override
089            public boolean isLinkable() {
090                    return _LINKABLE;
091            }
092    
093            @Override
094            protected String getIconPath(ThemeDisplay themeDisplay) {
095                    return themeDisplay.getPathThemeImages() + "/common/pages.png";
096            }
097    
098            private static final boolean _LINKABLE = true;
099    
100    }