001    /**
002     * Copyright (c) 2000-present 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.context;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.util.ArrayUtil;
019    import com.liferay.portal.theme.ThemeDisplay;
020    import com.liferay.portal.util.WebKeys;
021    import com.liferay.portlet.wiki.WikiPortletInstanceSettings;
022    import com.liferay.portlet.wiki.model.WikiNode;
023    import com.liferay.portlet.wiki.service.WikiNodeServiceUtil;
024    import com.liferay.portlet.wiki.util.WikiUtil;
025    
026    import java.util.List;
027    
028    import javax.servlet.http.HttpServletRequest;
029    
030    /**
031     * @author Iv??n Zaera
032     */
033    public class WikiConfigurationDisplayContext {
034    
035            public WikiConfigurationDisplayContext(
036                    HttpServletRequest request,
037                    WikiPortletInstanceSettings wikiPortletInstanceSettings) {
038    
039                    _wikiPortletInstanceSettings = wikiPortletInstanceSettings;
040    
041                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
042                            WebKeys.THEME_DISPLAY);
043    
044                    _scopeGroupId = themeDisplay.getScopeGroupId();
045            }
046    
047            public List<String> getAllNodeNames() throws PortalException {
048                    if (_allNodeNames == null) {
049                            _populateNodes();
050                    }
051    
052                    return _allNodeNames;
053            }
054    
055            public List<WikiNode> getAllNodes() throws PortalException {
056                    if (_allNodes == null) {
057                            _populateNodes();
058                    }
059    
060                    return _allNodes;
061            }
062    
063            public String[] getVisibleNodeNames() throws PortalException {
064                    if (_visibleNodeNames == null) {
065                            _populateNodes();
066                    }
067    
068                    return _visibleNodeNames;
069            }
070    
071            private void _populateNodes() throws PortalException {
072                    _allNodes = WikiNodeServiceUtil.getNodes(_scopeGroupId);
073    
074                    _allNodeNames = WikiUtil.getNodeNames(_allNodes);
075    
076                    _visibleNodeNames = _wikiPortletInstanceSettings.getVisibleNodes();
077    
078                    if (ArrayUtil.isNotEmpty(_visibleNodeNames)) {
079                            _allNodes = WikiUtil.orderNodes(_allNodes, _visibleNodeNames);
080                    }
081                    else {
082                            _visibleNodeNames = _allNodeNames.toArray(
083                                    new String[_allNodeNames.size()]);
084                    }
085            }
086    
087            private List<String> _allNodeNames;
088            private List<WikiNode> _allNodes;
089            private final long _scopeGroupId;
090            private String[] _visibleNodeNames;
091            private final WikiPortletInstanceSettings _wikiPortletInstanceSettings;
092    
093    }