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.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.PropsKeys;
028    import com.liferay.portlet.wiki.util.WikiConstants;
029    
030    import java.util.Map;
031    
032    /**
033     * @author Iv??n Zaera
034     */
035    public class WikiSettings {
036    
037            public static String[] ALL_KEYS = {
038                    "emailFromAddress", "emailFromName", "emailPageAddedBody",
039                    "emailPageAddedSubject", "emailPageUpdatedBody",
040                    "emailPageUpdatedSubject", "emailPageAddedEnabled",
041                    "emailPageUpdatedEnabled"
042            };
043    
044            public static WikiSettings getInstance(long groupId)
045                    throws PortalException {
046    
047                    Settings settings = SettingsFactoryUtil.getGroupServiceSettings(
048                            groupId, WikiConstants.SERVICE_NAME);
049    
050                    return new WikiSettings(settings);
051            }
052    
053            public static WikiSettings getInstance(
054                            long groupId, Map<String, String[]> parameterMap)
055                    throws PortalException {
056    
057                    Settings settings = SettingsFactoryUtil.getGroupServiceSettings(
058                            groupId, WikiConstants.SERVICE_NAME);
059    
060                    return new WikiSettings(
061                            new ParameterMapSettings(parameterMap, settings));
062            }
063    
064            public WikiSettings(Settings settings) {
065                    _typedSettings = new TypedSettings(settings);
066            }
067    
068            public String getEmailFromAddress() {
069                    return _typedSettings.getValue("emailFromAddress");
070            }
071    
072            public String getEmailFromName() {
073                    return _typedSettings.getValue("emailFromName");
074            }
075    
076            public LocalizedValuesMap getEmailPageAddedBody() {
077                    return _typedSettings.getLocalizedValuesMap("emailPageAddedBody");
078            }
079    
080            public String getEmailPageAddedBodyXml() {
081                    LocalizedValuesMap emailPageAddedBodyMap = getEmailPageAddedBody();
082    
083                    return emailPageAddedBodyMap.getLocalizationXml();
084            }
085    
086            public LocalizedValuesMap getEmailPageAddedSubject() {
087                    return _typedSettings.getLocalizedValuesMap("emailPageAddedSubject");
088            }
089    
090            public String getEmailPageAddedSubjectXml() {
091                    LocalizedValuesMap emailPageAddedSubjectMap =
092                            getEmailPageAddedSubject();
093    
094                    return emailPageAddedSubjectMap.getLocalizationXml();
095            }
096    
097            public LocalizedValuesMap getEmailPageUpdatedBody() {
098                    return _typedSettings.getLocalizedValuesMap("emailPageUpdatedBody");
099            }
100    
101            public String getEmailPageUpdatedBodyXml() {
102                    LocalizedValuesMap emailPageUpdatedBodyMap = getEmailPageUpdatedBody();
103    
104                    return emailPageUpdatedBodyMap.getLocalizationXml();
105            }
106    
107            public LocalizedValuesMap getEmailPageUpdatedSubject() {
108                    return _typedSettings.getLocalizedValuesMap("emailPageUpdatedSubject");
109            }
110    
111            public String getEmailPageUpdatedSubjectXml() {
112                    LocalizedValuesMap emailPageUpdatedSubjectMap =
113                            getEmailPageUpdatedSubject();
114    
115                    return emailPageUpdatedSubjectMap.getLocalizationXml();
116            }
117    
118            public boolean isEmailPageAddedEnabled() {
119                    return _typedSettings.getBooleanValue("emailPageAddedEnabled");
120            }
121    
122            public boolean isEmailPageUpdatedEnabled() {
123                    return _typedSettings.getBooleanValue("emailPageUpdatedEnabled");
124            }
125    
126            private static FallbackKeys _getFallbackKeys() {
127                    FallbackKeys fallbackKeys = new FallbackKeys();
128    
129                    fallbackKeys.add(
130                            "emailFromAddress", PropsKeys.WIKI_EMAIL_FROM_ADDRESS,
131                            PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
132                    fallbackKeys.add(
133                            "emailFromName", PropsKeys.WIKI_EMAIL_FROM_NAME,
134                            PropsKeys.ADMIN_EMAIL_FROM_NAME);
135                    fallbackKeys.add(
136                            "emailPageAddedBody", PropsKeys.WIKI_EMAIL_PAGE_ADDED_BODY);
137                    fallbackKeys.add(
138                            "emailPageAddedEnabled", PropsKeys.WIKI_EMAIL_PAGE_ADDED_ENABLED);
139                    fallbackKeys.add(
140                            "emailPageAddedSubject", PropsKeys.WIKI_EMAIL_PAGE_ADDED_SUBJECT);
141                    fallbackKeys.add(
142                            "emailPageUpdatedBody", PropsKeys.WIKI_EMAIL_PAGE_UPDATED_BODY);
143                    fallbackKeys.add(
144                            "emailPageUpdatedEnabled",
145                            PropsKeys.WIKI_EMAIL_PAGE_UPDATED_ENABLED);
146                    fallbackKeys.add(
147                            "emailPageUpdatedSubject",
148                            PropsKeys.WIKI_EMAIL_PAGE_UPDATED_SUBJECT);
149    
150                    return fallbackKeys;
151            }
152    
153            private static final String[] _MULTI_VALUED_KEYS = {};
154    
155            private static final ResourceManager _resourceManager =
156                    new ClassLoaderResourceManager(WikiSettings.class.getClassLoader());
157    
158            static {
159                    SettingsFactory settingsFactory =
160                            SettingsFactoryUtil.getSettingsFactory();
161    
162                    settingsFactory.registerSettingsMetadata(
163                            WikiConstants.SERVICE_NAME, _getFallbackKeys(), _MULTI_VALUED_KEYS,
164                            _resourceManager);
165            }
166    
167            private final TypedSettings _typedSettings;
168    
169    }