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.blogs;
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.blogs.util.BlogsConstants;
029    
030    import java.util.Map;
031    
032    /**
033     * @author Iv??n Zaera
034     */
035    public class BlogsSettings {
036    
037            public static final String[] ALL_KEYS = {
038                    "emailEntryAddedBody", "emailEntryAddedSubject",
039                    "emailEntryUpdatedBody", "emailEntryUpdatedSubject", "emailFromAddress",
040                    "emailFromName", "emailEntryAddedEnabled", "emailEntryUpdatedEnabled"
041            };
042    
043            public static BlogsSettings getInstance(long groupId)
044                    throws PortalException {
045    
046                    Settings settings = SettingsFactoryUtil.getGroupServiceSettings(
047                            groupId, BlogsConstants.SERVICE_NAME);
048    
049                    return new BlogsSettings(settings);
050            }
051    
052            public static BlogsSettings getInstance(
053                            long groupId, Map<String, String[]> parameterMap)
054                    throws PortalException {
055    
056                    Settings settings = SettingsFactoryUtil.getGroupServiceSettings(
057                            groupId, BlogsConstants.SERVICE_NAME);
058    
059                    return new BlogsSettings(
060                            new ParameterMapSettings(parameterMap, settings));
061            }
062    
063            public BlogsSettings(Settings settings) {
064                    _typedSettings = new TypedSettings(settings);
065            }
066    
067            public LocalizedValuesMap getEmailEntryAddedBody() {
068                    return _typedSettings.getLocalizedValuesMap("emailEntryAddedBody");
069            }
070    
071            public String getEmailEntryAddedBodyXml() {
072                    LocalizedValuesMap emailEntryAddedBodyMap = getEmailEntryAddedBody();
073    
074                    return emailEntryAddedBodyMap.getLocalizationXml();
075            }
076    
077            public LocalizedValuesMap getEmailEntryAddedSubject() {
078                    return _typedSettings.getLocalizedValuesMap("emailEntryAddedSubject");
079            }
080    
081            public String getEmailEntryAddedSubjectXml() {
082                    LocalizedValuesMap emailEntryAddedSubjectMap =
083                            getEmailEntryAddedSubject();
084    
085                    return emailEntryAddedSubjectMap.getLocalizationXml();
086            }
087    
088            public LocalizedValuesMap getEmailEntryUpdatedBody() {
089                    return _typedSettings.getLocalizedValuesMap("emailEntryUpdatedBody");
090            }
091    
092            public String getEmailEntryUpdatedBodyXml() {
093                    LocalizedValuesMap emailEntryUpdatedBodyMap =
094                            getEmailEntryUpdatedBody();
095    
096                    return emailEntryUpdatedBodyMap.getLocalizationXml();
097            }
098    
099            public LocalizedValuesMap getEmailEntryUpdatedSubject() {
100                    return _typedSettings.getLocalizedValuesMap("emailEntryUpdatedSubject");
101            }
102    
103            public String getEmailEntryUpdatedSubjectXml() {
104                    LocalizedValuesMap emailEntryUpdatedSubjectMap =
105                            getEmailEntryUpdatedSubject();
106    
107                    return emailEntryUpdatedSubjectMap.getLocalizationXml();
108            }
109    
110            public String getEmailFromAddress() {
111                    return _typedSettings.getValue("emailFromAddress");
112            }
113    
114            public String getEmailFromName() {
115                    return _typedSettings.getValue("emailFromName");
116            }
117    
118            public boolean isEmailEntryAddedEnabled() {
119                    return _typedSettings.getBooleanValue("emailEntryAddedEnabled");
120            }
121    
122            public boolean isEmailEntryUpdatedEnabled() {
123                    return _typedSettings.getBooleanValue("emailEntryUpdatedEnabled");
124            }
125    
126            private static FallbackKeys _getFallbackKeys() {
127                    FallbackKeys fallbackKeys = new FallbackKeys();
128    
129                    fallbackKeys.add(
130                            "emailEntryAddedBody", PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_BODY);
131                    fallbackKeys.add(
132                            "emailEntryAddedEnabled",
133                            PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_ENABLED);
134                    fallbackKeys.add(
135                            "emailEntryAddedSubject",
136                            PropsKeys.BLOGS_EMAIL_ENTRY_ADDED_SUBJECT);
137                    fallbackKeys.add(
138                            "emailEntryUpdatedBody", PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_BODY);
139                    fallbackKeys.add(
140                            "emailEntryUpdatedEnabled",
141                            PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_ENABLED);
142                    fallbackKeys.add(
143                            "emailEntryUpdatedSubject",
144                            PropsKeys.BLOGS_EMAIL_ENTRY_UPDATED_SUBJECT);
145                    fallbackKeys.add(
146                            "emailFromAddress", PropsKeys.BLOGS_EMAIL_FROM_ADDRESS,
147                            PropsKeys.ADMIN_EMAIL_FROM_ADDRESS);
148                    fallbackKeys.add(
149                            "emailFromName", PropsKeys.BLOGS_EMAIL_FROM_NAME,
150                            PropsKeys.ADMIN_EMAIL_FROM_NAME);
151    
152                    return fallbackKeys;
153            }
154    
155            private static final String[] _MULTI_VALUED_KEYS = {};
156    
157            private static final ResourceManager _resourceManager =
158                    new ClassLoaderResourceManager(BlogsSettings.class.getClassLoader());
159    
160            static {
161                    SettingsFactory settingsFactory =
162                            SettingsFactoryUtil.getSettingsFactory();
163    
164                    settingsFactory.registerSettingsMetadata(
165                            BlogsConstants.SERVICE_NAME, _getFallbackKeys(), _MULTI_VALUED_KEYS,
166                            _resourceManager);
167            }
168    
169            private final TypedSettings _typedSettings;
170    
171    }