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.bookmarks;
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.bookmarks.model.BookmarksFolderConstants;
029    import com.liferay.portlet.bookmarks.util.BookmarksConstants;
030    
031    import java.util.Map;
032    
033    /**
034     * @author Iv??n Zaera
035     */
036    public class BookmarksSettings {
037    
038            public static final String[] ALL_KEYS = {
039                    "emailEntryAddedBody", "emailEntryAddedSubject",
040                    "emailEntryUpdatedBody", "emailEntryUpdatedSubject", "emailFromAddress",
041                    "emailFromName", "entriesPerPage", "entryColumns", "folderColumns",
042                    "foldersPerPage", "rootFolderId", "emailEntryAddedEnabled",
043                    "emailEntryUpdatedEnabled", "enableRelatedAssets", "showFoldersSearch",
044                    "showSubfolders"
045            };
046    
047            public static BookmarksSettings getInstance(long groupId)
048                    throws PortalException {
049    
050                    Settings settings = SettingsFactoryUtil.getGroupServiceSettings(
051                            groupId, BookmarksConstants.SERVICE_NAME);
052    
053                    return new BookmarksSettings(settings);
054            }
055    
056            public static BookmarksSettings getInstance(
057                            long groupId, Map<String, String[]> parameterMap)
058                    throws PortalException {
059    
060                    Settings settings = SettingsFactoryUtil.getGroupServiceSettings(
061                            groupId, BookmarksConstants.SERVICE_NAME);
062    
063                    ParameterMapSettings parameterMapSettings = new ParameterMapSettings(
064                            parameterMap, settings);
065    
066                    return new BookmarksSettings(parameterMapSettings);
067            }
068    
069            public BookmarksSettings(Settings settings) {
070                    _typedSettings = new TypedSettings(settings);
071            }
072    
073            public LocalizedValuesMap getEmailEntryAddedBody() {
074                    return _typedSettings.getLocalizedValuesMap("emailEntryAddedBody");
075            }
076    
077            public String getEmailEntryAddedBodyXml() {
078                    LocalizedValuesMap emailEntryAddedBody = getEmailEntryAddedBody();
079    
080                    return emailEntryAddedBody.getLocalizationXml();
081            }
082    
083            public LocalizedValuesMap getEmailEntryAddedSubject() {
084                    return _typedSettings.getLocalizedValuesMap("emailEntryAddedSubject");
085            }
086    
087            public String getEmailEntryAddedSubjectXml() {
088                    LocalizedValuesMap emailEntryAddedSubject = getEmailEntryAddedSubject();
089    
090                    return emailEntryAddedSubject.getLocalizationXml();
091            }
092    
093            public LocalizedValuesMap getEmailEntryUpdatedBody() {
094                    return _typedSettings.getLocalizedValuesMap("emailEntryUpdatedBody");
095            }
096    
097            public String getEmailEntryUpdatedBodyXml() {
098                    LocalizedValuesMap emailEntryUpdatedBody = getEmailEntryUpdatedBody();
099    
100                    return emailEntryUpdatedBody.getLocalizationXml();
101            }
102    
103            public LocalizedValuesMap getEmailEntryUpdatedSubject() {
104                    return _typedSettings.getLocalizedValuesMap("emailEntryUpdatedSubject");
105            }
106    
107            public String getEmailEntryUpdatedSubjectXml() {
108                    LocalizedValuesMap emailEntryUpdatedSubject =
109                            getEmailEntryUpdatedSubject();
110    
111                    return emailEntryUpdatedSubject.getLocalizationXml();
112            }
113    
114            public String getEmailFromAddress() {
115                    return _typedSettings.getValue("emailFromAddress");
116            }
117    
118            public String getEmailFromName() {
119                    return _typedSettings.getValue("emailFromName");
120            }
121    
122            public int getEntriesPerPage() {
123                    return _typedSettings.getIntegerValue("entriesPerPage");
124            }
125    
126            public String[] getEntryColumns() {
127                    return _typedSettings.getValues("entryColumns");
128            }
129    
130            public String[] getFolderColumns() {
131                    return _typedSettings.getValues("folderColumns");
132            }
133    
134            public int getFoldersPerPage() {
135                    return _typedSettings.getIntegerValue("foldersPerPage");
136            }
137    
138            public long getRootFolderId() {
139                    return _typedSettings.getLongValue(
140                            "rootFolderId", BookmarksFolderConstants.DEFAULT_PARENT_FOLDER_ID);
141            }
142    
143            public boolean isEmailEntryAddedEnabled() {
144                    return _typedSettings.getBooleanValue("emailEntryAddedEnabled");
145            }
146    
147            public boolean isEmailEntryUpdatedEnabled() {
148                    return _typedSettings.getBooleanValue("emailEntryUpdatedEnabled");
149            }
150    
151            public boolean isEnableRelatedAssets() {
152                    return _typedSettings.getBooleanValue("enableRelatedAssets");
153            }
154    
155            public boolean isShowFoldersSearch() {
156                    return _typedSettings.getBooleanValue("showFoldersSearch");
157            }
158    
159            public boolean isShowSubfolders() {
160                    return _typedSettings.getBooleanValue("showSubfolders");
161            }
162    
163            private static FallbackKeys _getFallbackKeys() {
164                    FallbackKeys fallbackKeys = new FallbackKeys();
165    
166                    fallbackKeys.add(
167                            "emailEntryAddedBody", PropsKeys.BOOKMARKS_EMAIL_ENTRY_ADDED_BODY);
168                    fallbackKeys.add(
169                            "emailEntryAddedEnabled",
170                            PropsKeys.BOOKMARKS_EMAIL_ENTRY_ADDED_ENABLED);
171                    fallbackKeys.add(
172                            "emailEntryAddedSubject",
173                            PropsKeys.BOOKMARKS_EMAIL_ENTRY_ADDED_SUBJECT);
174                    fallbackKeys.add(
175                            "emailEntryUpdatedBody",
176                            PropsKeys.BOOKMARKS_EMAIL_ENTRY_UPDATED_BODY);
177                    fallbackKeys.add(
178                            "emailEntryUpdatedEnabled",
179                            PropsKeys.BOOKMARKS_EMAIL_ENTRY_UPDATED_ENABLED);
180                    fallbackKeys.add(
181                            "emailEntryUpdatedSubject",
182                            PropsKeys.BOOKMARKS_EMAIL_ENTRY_UPDATED_SUBJECT);
183                    fallbackKeys.add(
184                            "emailFromAddress", PropsKeys.BOOKMARKS_EMAIL_FROM_ADDRESS,
185                            PropsKeys.ADMIN_EMAIL_FROM_ADDRESS );
186                    fallbackKeys.add(
187                            "emailFromName", PropsKeys.BOOKMARKS_EMAIL_FROM_NAME,
188                            PropsKeys.ADMIN_EMAIL_FROM_NAME);
189                    fallbackKeys.add(
190                            "enableRelatedAssets", PropsKeys.BOOKMARKS_RELATED_ASSETS_ENABLED);
191                    fallbackKeys.add(
192                            "entriesPerPage", PropsKeys.SEARCH_CONTAINER_PAGE_DEFAULT_DELTA);
193                    fallbackKeys.add("entryColumns", PropsKeys.BOOKMARKS_ENTRY_COLUMNS);
194                    fallbackKeys.add("folderColumns", PropsKeys.BOOKMARKS_FOLDER_COLUMNS);
195                    fallbackKeys.add(
196                            "foldersPerPage", PropsKeys.SEARCH_CONTAINER_PAGE_DEFAULT_DELTA);
197                    fallbackKeys.add(
198                            "showFoldersSearch", PropsKeys.BOOKMARKS_FOLDERS_SEARCH_VISIBLE);
199                    fallbackKeys.add(
200                            "showSubfolders", PropsKeys.BOOKMARKS_SUBFOLDERS_VISIBLE);
201    
202                    return fallbackKeys;
203            }
204    
205            private static final String[] _MULTI_VALUED_KEYS = {
206                    "entryColumns", "folderColumns"
207            };
208    
209            private static final ResourceManager _resourceManager =
210                    new ClassLoaderResourceManager(
211                            BookmarksSettings.class.getClassLoader());
212    
213            static {
214                    SettingsFactory settingsFactory =
215                            SettingsFactoryUtil.getSettingsFactory();
216    
217                    settingsFactory.registerSettingsMetadata(
218                            BookmarksConstants.SERVICE_NAME, _getFallbackKeys(),
219                            _MULTI_VALUED_KEYS, _resourceManager);
220            }
221    
222            private final TypedSettings _typedSettings;
223    
224    }