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.messageboards;
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.LocalizedValuesMap;
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.LocalizationUtil;
028    import com.liferay.portal.kernel.util.PropsKeys;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portlet.messageboards.util.MBConstants;
031    import com.liferay.portlet.messageboards.util.MBUtil;
032    import com.liferay.util.RSSUtil;
033    
034    import java.util.Map;
035    
036    /**
037     * @author Jorge Ferrer
038     */
039    public class MBSettings {
040    
041            public static final String[] ALL_KEYS = {
042                    "emailFromAddress", "emailFromName", "emailMessageAddedBody",
043                    "emailMessageAddedSubject", "emailMessageUpdatedBody",
044                    "emailMessageUpdatedSubject", "messageFormat", "priorities", "ranks",
045                    "recentPostsDateOffset", "rssDelta", "rssDisplayStyle", "rssFeedType",
046                    "allowAnonymousPosting", "emailHtmlFormat", "emailMessageAddedEnabled",
047                    "emailMessageUpdatedEnabled", "enableFlags", "enableRatings",
048                    "enableRss", "subscribeByDefault", "threadAsQuestionByDefault"
049            };
050    
051            public static MBSettings getInstance(long groupId) throws PortalException {
052                    Settings settings = SettingsFactoryUtil.getGroupServiceSettings(
053                            groupId, MBConstants.SERVICE_NAME);
054    
055                    return new MBSettings(settings);
056            }
057    
058            public static MBSettings getInstance(
059                            long groupId, Map<String, String[]> parameterMap)
060                    throws PortalException {
061    
062                    Settings settings = SettingsFactoryUtil.getGroupServiceSettings(
063                            groupId, MBConstants.SERVICE_NAME);
064    
065                    ParameterMapSettings parameterMapSettings = new ParameterMapSettings(
066                            parameterMap, settings);
067    
068                    return new MBSettings(parameterMapSettings);
069            }
070    
071            public MBSettings(Settings settings) {
072                    _typedSettings = new TypedSettings(settings);
073            }
074    
075            public String getEmailFromAddress() {
076                    return _typedSettings.getValue("emailFromAddress");
077            }
078    
079            public String getEmailFromName() {
080                    return _typedSettings.getValue("emailFromName");
081            }
082    
083            public LocalizedValuesMap getEmailMessageAddedBody() {
084                    return _typedSettings.getLocalizedValuesMap("emailMessageAddedBody");
085            }
086    
087            public String getEmailMessageAddedBodyXml() {
088                    LocalizedValuesMap emailMessageBodyMap = getEmailMessageAddedBody();
089    
090                    return emailMessageBodyMap.getLocalizationXml();
091            }
092    
093            public LocalizedValuesMap getEmailMessageAddedSubject() {
094                    return _typedSettings.getLocalizedValuesMap("emailMessageAddedSubject");
095            }
096    
097            public String getEmailMessageAddedSubjectXml() {
098                    LocalizedValuesMap emailMessageAddedSubjectMap =
099                            getEmailMessageAddedSubject();
100    
101                    return emailMessageAddedSubjectMap.getLocalizationXml();
102            }
103    
104            public LocalizedValuesMap getEmailMessageUpdatedBody() {
105                    return _typedSettings.getLocalizedValuesMap("emailMessageUpdatedBody");
106            }
107    
108            public String getEmailMessageUpdatedBodyXml() {
109                    LocalizedValuesMap emailMessageUpdatedBodyMap =
110                            getEmailMessageUpdatedBody();
111    
112                    return emailMessageUpdatedBodyMap.getLocalizationXml();
113            }
114    
115            public LocalizedValuesMap getEmailMessageUpdatedSubject() {
116                    return _typedSettings.getLocalizedValuesMap(
117                            "emailMessageUpdatedSubject");
118            }
119    
120            public String getEmailMessageUpdatedSubjectXml() {
121                    LocalizedValuesMap emailMessageUpdatedSubjectMap =
122                            getEmailMessageUpdatedSubject();
123    
124                    return emailMessageUpdatedSubjectMap.getLocalizationXml();
125            }
126    
127            public String getMessageFormat() {
128                    String messageFormat = _typedSettings.getValue("messageFormat");
129    
130                    if (MBUtil.isValidMessageFormat(messageFormat)) {
131                            return messageFormat;
132                    }
133    
134                    return "html";
135            }
136    
137            public String[] getPriorities(String currentLanguageId) {
138                    return LocalizationUtil.getSettingsValues(
139                            _typedSettings.getWrappedSettings(), "priorities",
140                            currentLanguageId);
141            }
142    
143            public String[] getRanks(String languageId) {
144                    return LocalizationUtil.getSettingsValues(
145                            _typedSettings.getWrappedSettings(), "ranks", languageId);
146            }
147    
148            public String getRecentPostsDateOffset() {
149                    return _typedSettings.getValue("recentPostsDateOffset");
150            }
151    
152            public int getRSSDelta() {
153                    return _typedSettings.getIntegerValue("rssDelta");
154            }
155    
156            public String getRSSDisplayStyle() {
157                    return _typedSettings.getValue(
158                            "rssDisplayStyle", RSSUtil.DISPLAY_STYLE_FULL_CONTENT);
159            }
160    
161            public String getRSSFeedType() {
162                    return _typedSettings.getValue(
163                            "rssFeedType", RSSUtil.getFeedType(RSSUtil.ATOM, 1.0));
164            }
165    
166            public boolean isAllowAnonymousPosting() {
167                    return _typedSettings.getBooleanValue("allowAnonymousPosting");
168            }
169    
170            public boolean isEmailHtmlFormat() {
171                    return _typedSettings.getBooleanValue("emailHtmlFormat");
172            }
173    
174            public boolean isEmailMessageAddedEnabled() {
175                    return _typedSettings.getBooleanValue("emailMessageAddedEnabled");
176            }
177    
178            public boolean isEmailMessageUpdatedEnabled() {
179                    return _typedSettings.getBooleanValue("emailMessageUpdatedEnabled");
180            }
181    
182            public boolean isEnableFlags() {
183                    return _typedSettings.getBooleanValue("enableFlags");
184            }
185    
186            public boolean isEnableRatings() {
187                    return _typedSettings.getBooleanValue("enableRatings");
188            }
189    
190            public boolean isEnableRSS() {
191                    if (!PortalUtil.isRSSFeedsEnabled()) {
192                            return false;
193                    }
194    
195                    return _typedSettings.getBooleanValue("enableRss");
196            }
197    
198            public boolean isSubscribeByDefault() {
199                    return _typedSettings.getBooleanValue("subscribeByDefault");
200            }
201    
202            public boolean isThreadAsQuestionByDefault() {
203                    return _typedSettings.getBooleanValue("threadAsQuestionByDefault");
204            }
205    
206            private static FallbackKeys _getFallbackKeys() {
207                    FallbackKeys fallbackKeys = new FallbackKeys();
208    
209                    fallbackKeys.add(
210                            "allowAnonymousPosting",
211                            PropsKeys.MESSAGE_BOARDS_ANONYMOUS_POSTING_ENABLED);
212                    fallbackKeys.add(
213                            "emailFromAddress", PropsKeys.MESSAGE_BOARDS_EMAIL_FROM_ADDRESS,
214                            PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
215                    fallbackKeys.add(
216                            "emailFromName", PropsKeys.MESSAGE_BOARDS_EMAIL_FROM_NAME,
217                            PropsKeys.ADMIN_EMAIL_FROM_NAME);
218                    fallbackKeys.add(
219                            "emailHtmlFormat", PropsKeys.MESSAGE_BOARDS_EMAIL_HTML_FORMAT);
220                    fallbackKeys.add(
221                            "emailMessageAddedBody",
222                            PropsKeys.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_BODY);
223                    fallbackKeys.add(
224                            "emailMessageAddedEnabled",
225                            PropsKeys.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_ENABLED);
226                    fallbackKeys.add(
227                            "emailMessageAddedSubject",
228                            PropsKeys.MESSAGE_BOARDS_EMAIL_MESSAGE_ADDED_SUBJECT);
229                    fallbackKeys.add(
230                            "emailMessageUpdatedBody",
231                            PropsKeys.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_BODY);
232                    fallbackKeys.add(
233                            "emailMessageUpdatedEnabled",
234                            PropsKeys.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_ENABLED);
235                    fallbackKeys.add(
236                            "emailMessageUpdatedSubject",
237                            PropsKeys.MESSAGE_BOARDS_EMAIL_MESSAGE_UPDATED_SUBJECT);
238                    fallbackKeys.add("enableFlags", PropsKeys.MESSAGE_BOARDS_FLAGS_ENABLED);
239                    fallbackKeys.add(
240                            "enableRatings", PropsKeys.MESSAGE_BOARDS_RATINGS_ENABLED);
241                    fallbackKeys.add("enableRss", PropsKeys.MESSAGE_BOARDS_RSS_ENABLED);
242                    fallbackKeys.add(
243                            "messageFormat", PropsKeys.MESSAGE_BOARDS_MESSAGE_FORMATS_DEFAULT);
244                    fallbackKeys.add(
245                            "priorities", PropsKeys.MESSAGE_BOARDS_THREAD_PRIORITIES);
246                    fallbackKeys.add("ranks", PropsKeys.MESSAGE_BOARDS_USER_RANKS);
247                    fallbackKeys.add(
248                            "recentPostsDateOffset",
249                            PropsKeys.MESSAGE_BOARDS_RECENT_POSTS_DATE_OFFSET);
250                    fallbackKeys.add(
251                            "rssDelta", PropsKeys.SEARCH_CONTAINER_PAGE_DEFAULT_DELTA);
252                    fallbackKeys.add(
253                            "rssDisplayStyle", PropsKeys.RSS_FEED_DISPLAY_STYLE_DEFAULT);
254                    fallbackKeys.add("rssFeedType", PropsKeys.RSS_FEED_TYPE_DEFAULT);
255                    fallbackKeys.add(
256                            "subscribeByDefault",
257                            PropsKeys.MESSAGE_BOARDS_SUBSCRIBE_BY_DEFAULT);
258                    fallbackKeys.add(
259                            "threadAsQuestionByDefault",
260                            PropsKeys.MESSAGE_BOARDS_THREAD_AS_QUESTION_BY_DEFAULT);
261    
262                    return fallbackKeys;
263            }
264    
265            private static final String[] _MULTI_VALUED_KEYS = {"ranks"};
266    
267            private static final ResourceManager _resourceManager =
268                    new ClassLoaderResourceManager(MBSettings.class.getClassLoader());
269    
270            static {
271                    SettingsFactory settingsFactory =
272                            SettingsFactoryUtil.getSettingsFactory();
273    
274                    settingsFactory.registerSettingsMetadata(
275                            MBConstants.SERVICE_NAME, _getFallbackKeys(), _MULTI_VALUED_KEYS,
276                            _resourceManager);
277            }
278    
279            private final TypedSettings _typedSettings;
280    
281    }