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;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.resource.manager.ClassLoaderResourceManager;
019    import com.liferay.portal.kernel.resource.manager.ResourceManager;
020    import com.liferay.portal.kernel.settings.FallbackKeys;
021    import com.liferay.portal.kernel.settings.ModifiableSettings;
022    import com.liferay.portal.kernel.settings.ParameterMapSettings;
023    import com.liferay.portal.kernel.settings.Settings;
024    import com.liferay.portal.kernel.settings.SettingsFactory;
025    import com.liferay.portal.kernel.settings.SettingsFactoryUtil;
026    import com.liferay.portal.kernel.settings.TypedSettings;
027    import com.liferay.portal.kernel.util.PropsKeys;
028    import com.liferay.portal.model.Layout;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.PortletKeys;
031    
032    import java.io.IOException;
033    
034    import java.util.Map;
035    
036    import javax.portlet.ValidatorException;
037    
038    /**
039     * @author Iv??n Zaera
040     */
041    public class WikiPortletInstanceSettings {
042    
043            public static final String[] ALL_KEYS = {
044                    "displayStyle", "displayStyleGroupId", "hiddenNodes", "rssDelta",
045                    "rssDisplayStyle", "rssFeedType", "visibleNodes",
046                    "enableCommentRatings", "enableComments", "enablePageRatings",
047                    "enableRelatedAssets", "enableRss", "hiddenNodes", "visibleNodes"
048            };
049    
050            public static WikiPortletInstanceSettings getInstance(
051                            Layout layout, String portletId)
052                    throws PortalException {
053    
054                    Settings settings = SettingsFactoryUtil.getPortletInstanceSettings(
055                            layout, portletId);
056    
057                    return new WikiPortletInstanceSettings(settings);
058            }
059    
060            public static WikiPortletInstanceSettings getInstance(
061                            Layout layout, String portletId, Map<String, String[]> parameterMap)
062                    throws PortalException {
063    
064                    Settings settings = SettingsFactoryUtil.getPortletInstanceSettings(
065                            layout, portletId);
066    
067                    return new WikiPortletInstanceSettings(
068                            new ParameterMapSettings(parameterMap, settings));
069            }
070    
071            public WikiPortletInstanceSettings(Settings settings) {
072                    _typedSettings = new TypedSettings(settings);
073            }
074    
075            public String getDisplayStyle() {
076                    return _typedSettings.getValue("displayStyle");
077            }
078    
079            public long getDisplayStyleGroupId(long defaultDisplayStyleGroupId) {
080                    return _typedSettings.getLongValue(
081                            "displayStyleGroupId", defaultDisplayStyleGroupId);
082            }
083    
084            public String[] getHiddenNodes() {
085                    return _typedSettings.getValues("hiddenNodes");
086            }
087    
088            public int getRssDelta() {
089                    return _typedSettings.getIntegerValue("rssDelta");
090            }
091    
092            public String getRssDisplayStyle() {
093                    return _typedSettings.getValue("rssDisplayStyle");
094            }
095    
096            public String getRssFeedType() {
097                    return _typedSettings.getValue("rssFeedType");
098            }
099    
100            public String[] getVisibleNodes() {
101                    return _typedSettings.getValues("visibleNodes");
102            }
103    
104            public boolean isEnableCommentRatings() {
105                    return _typedSettings.getBooleanValue("enableCommentRatings");
106            }
107    
108            public boolean isEnableComments() {
109                    return _typedSettings.getBooleanValue("enableComments");
110            }
111    
112            public boolean isEnablePageRatings() {
113                    return _typedSettings.getBooleanValue("enablePageRatings");
114            }
115    
116            public boolean isEnableRelatedAssets() {
117                    return _typedSettings.getBooleanValue("enableRelatedAssets");
118            }
119    
120            public boolean isEnableRSS() {
121                    if (!PortalUtil.isRSSFeedsEnabled()) {
122                            return false;
123                    }
124    
125                    return _typedSettings.getBooleanValue("enableRss");
126            }
127    
128            public void setHiddenNodes(String[] hiddenNodes) {
129                    _typedSettings.setValues("hiddenNodes", hiddenNodes);
130            }
131    
132            public void setVisibleNodes(String[] visibleNodes) {
133                    _typedSettings.setValues("visibleNodes", visibleNodes);
134            }
135    
136            public void store() throws IOException, ValidatorException {
137                    Settings settings = _typedSettings.getWrappedSettings();
138    
139                    ModifiableSettings modifiableSettings =
140                            settings.getModifiableSettings();
141    
142                    modifiableSettings.store();
143            }
144    
145            private static FallbackKeys _getFallbackKeys() {
146                    FallbackKeys fallbackKeys = new FallbackKeys();
147    
148                    fallbackKeys.add("displayStyle", PropsKeys.WIKI_DISPLAY_STYLE);
149                    fallbackKeys.add(
150                            "enableComments", PropsKeys.WIKI_PAGE_COMMENTS_ENABLED);
151                    fallbackKeys.add(
152                            "enableCommentRatings", PropsKeys.WIKI_COMMENT_RATINGS_ENABLED);
153                    fallbackKeys.add(
154                            "enablePageRatings", PropsKeys.WIKI_PAGE_RATINGS_ENABLED);
155                    fallbackKeys.add(
156                            "enableRelatedAssets", PropsKeys.WIKI_RELATED_ASSETS_ENABLED);
157                    fallbackKeys.add("enableRss", PropsKeys.WIKI_RSS_ENABLED);
158                    fallbackKeys.add(
159                            "rssDelta", PropsKeys.SEARCH_CONTAINER_PAGE_DEFAULT_DELTA);
160                    fallbackKeys.add(
161                            "rssDisplayStyle", PropsKeys.RSS_FEED_DISPLAY_STYLE_DEFAULT);
162                    fallbackKeys.add("rssFeedType", PropsKeys.RSS_FEED_TYPE_DEFAULT);
163    
164                    return fallbackKeys;
165            }
166    
167            private static final String[] _MULTI_VALUED_KEYS = {
168                    "hiddenNodes", "visibleNodes"
169            };
170    
171            private static final ResourceManager _resourceManager =
172                    new ClassLoaderResourceManager(
173                            WikiPortletInstanceSettings.class.getClassLoader());
174    
175            static {
176                    SettingsFactory settingsFactory =
177                            SettingsFactoryUtil.getSettingsFactory();
178    
179                    settingsFactory.registerSettingsMetadata(
180                            PortletKeys.WIKI, _getFallbackKeys(), _MULTI_VALUED_KEYS,
181                            _resourceManager);
182                    settingsFactory.registerSettingsMetadata(
183                            PortletKeys.WIKI_ADMIN, _getFallbackKeys(), _MULTI_VALUED_KEYS,
184                            _resourceManager);
185                    settingsFactory.registerSettingsMetadata(
186                            PortletKeys.WIKI_DISPLAY, _getFallbackKeys(), _MULTI_VALUED_KEYS,
187                            _resourceManager);
188            }
189    
190            private final TypedSettings _typedSettings;
191    
192    }