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.documentlibrary;
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.ParameterMapSettings;
022    import com.liferay.portal.kernel.settings.Settings;
023    import com.liferay.portal.kernel.settings.SettingsFactory;
024    import com.liferay.portal.kernel.settings.SettingsFactoryUtil;
025    import com.liferay.portal.kernel.settings.TypedSettings;
026    import com.liferay.portal.kernel.util.ArrayUtil;
027    import com.liferay.portal.kernel.util.PropsKeys;
028    import com.liferay.portal.model.Layout;
029    import com.liferay.portal.util.PortletKeys;
030    import com.liferay.portlet.documentlibrary.model.DLFolderConstants;
031    import com.liferay.portlet.documentlibrary.util.DLUtil;
032    
033    import java.util.Map;
034    
035    /**
036     * @author Sergio Gonz??lez
037     */
038    public class DLPortletInstanceSettings {
039    
040            public static final String[] ALL_KEYS = {
041                    "rootFolderId", "displayViews", "enableFileEntryDrafts",
042                    "entriesPerPage", "entryColumns", "fileEntriesPerPage",
043                    "fileEntryColumns", "folderColumns", "foldersPerPage", "mimeTypes",
044                    "enableCommentRatings", "enableRatings", "enableRelatedAssets",
045                    "showActions", "showFolderMenu", "showFoldersSearch", "showSubfolders",
046                    "showTabs",
047            };
048    
049            public static DLPortletInstanceSettings getInstance(
050                            Layout layout, String portletId)
051                    throws PortalException {
052    
053                    Settings settings = SettingsFactoryUtil.getPortletInstanceSettings(
054                            layout, portletId);
055    
056                    return new DLPortletInstanceSettings(settings);
057            }
058    
059            public static DLPortletInstanceSettings getInstance(
060                            Layout layout, String portletId, Map<String, String[]> parameterMap)
061                    throws PortalException {
062    
063                    Settings settings = SettingsFactoryUtil.getPortletInstanceSettings(
064                            layout, portletId);
065    
066                    Settings parameterMapSettings = new ParameterMapSettings(
067                            parameterMap, settings);
068    
069                    return new DLPortletInstanceSettings(parameterMapSettings);
070            }
071    
072            public DLPortletInstanceSettings(Settings settings) {
073                    _typedSettings = new TypedSettings(settings);
074            }
075    
076            public long getDefaultFolderId() {
077                    return _typedSettings.getLongValue(
078                            "rootFolderId", DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
079            }
080    
081            public String[] getDisplayViews() {
082                    return _typedSettings.getValues("displayViews");
083            }
084    
085            public int getEntriesPerPage() {
086                    return _typedSettings.getIntegerValue("entriesPerPage");
087            }
088    
089            public String[] getEntryColumns() {
090                    return _typedSettings.getValues("entryColumns");
091            }
092    
093            public int getFileEntriesPerPage() {
094                    return _typedSettings.getIntegerValue("fileEntriesPerPage");
095            }
096    
097            public String[] getFileEntryColumns() {
098                    return _typedSettings.getValues("fileEntryColumns");
099            }
100    
101            public String[] getFolderColumns() {
102                    return _typedSettings.getValues("folderColumns");
103            }
104    
105            public int getFoldersPerPage() {
106                    return _typedSettings.getIntegerValue("foldersPerPage");
107            }
108    
109            public String[] getMimeTypes() {
110                    return _typedSettings.getValues("mimeTypes", _MIME_TYPES_DEFAULT);
111            }
112    
113            public long getRootFolderId() {
114                    return _typedSettings.getLongValue(
115                            "rootFolderId", DLFolderConstants.DEFAULT_PARENT_FOLDER_ID);
116            }
117    
118            public boolean isEnableCommentRatings() {
119                    return _typedSettings.getBooleanValue("enableCommentRatings");
120            }
121    
122            public boolean isEnableFileEntryDrafts() {
123                    return _typedSettings.getBooleanValue("enableFileEntryDrafts");
124            }
125    
126            public boolean isEnableRatings() {
127                    return _typedSettings.getBooleanValue("enableRatings");
128            }
129    
130            public boolean isEnableRelatedAssets() {
131                    return _typedSettings.getBooleanValue("enableRelatedAssets");
132            }
133    
134            public boolean isShowActions() {
135                    return _typedSettings.getBooleanValue("showActions");
136            }
137    
138            public boolean isShowFolderMenu() {
139                    return _typedSettings.getBooleanValue("showFolderMenu");
140            }
141    
142            public boolean isShowFoldersSearch() {
143                    return _typedSettings.getBooleanValue("showFoldersSearch");
144            }
145    
146            public boolean isShowSubfolders() {
147                    return _typedSettings.getBooleanValue("showSubfolders");
148            }
149    
150            public boolean isShowTabs() {
151                    return _typedSettings.getBooleanValue("showTabs");
152            }
153    
154            private static FallbackKeys _getFallbackKeys() {
155                    FallbackKeys fallbackKeys = new FallbackKeys();
156    
157                    fallbackKeys.add("displayViews", PropsKeys.DL_DISPLAY_VIEWS);
158                    fallbackKeys.add(
159                            "enableCommentRatings", PropsKeys.DL_COMMENT_RATINGS_ENABLED);
160                    fallbackKeys.add(
161                            "enableFileEntryDrafts", PropsKeys.DL_FILE_ENTRY_DRAFTS_ENABLED);
162                    fallbackKeys.add("enableRatings", PropsKeys.DL_RATINGS_ENABLED);
163                    fallbackKeys.add(
164                            "enableRelatedAssets", PropsKeys.DL_RELATED_ASSETS_ENABLED);
165                    fallbackKeys.add(
166                            "entriesPerPage", PropsKeys.SEARCH_CONTAINER_PAGE_DEFAULT_DELTA);
167                    fallbackKeys.add("entryColumns", PropsKeys.DL_ENTRY_COLUMNS);
168                    fallbackKeys.add("fileEntryColumns", PropsKeys.DL_FILE_ENTRY_COLUMNS);
169                    fallbackKeys.add("folderColumns", PropsKeys.DL_FOLDER_COLUMNS);
170                    fallbackKeys.add(
171                            "foldersPerPage", PropsKeys.SEARCH_CONTAINER_PAGE_DEFAULT_DELTA);
172                    fallbackKeys.add(
173                            "fileEntriesPerPage",
174                            PropsKeys.SEARCH_CONTAINER_PAGE_DEFAULT_DELTA);
175                    fallbackKeys.add("showActions", PropsKeys.DL_ACTIONS_VISIBLE);
176                    fallbackKeys.add("showFolderMenu", PropsKeys.DL_FOLDER_MENU_VISIBLE);
177                    fallbackKeys.add(
178                            "showFoldersSearch", PropsKeys.DL_FOLDERS_SEARCH_VISIBLE);
179                    fallbackKeys.add("showSubfolders", PropsKeys.DL_SUBFOLDERS_VISIBLE);
180                    fallbackKeys.add("showTabs", PropsKeys.DL_TABS_VISIBLE);
181    
182                    return fallbackKeys;
183            }
184    
185            private static final String[] _MIME_TYPES_DEFAULT = ArrayUtil.toStringArray(
186                    DLUtil.getAllMediaGalleryMimeTypes());
187    
188            private static final String[] _MULTI_VALUED_KEYS = {
189                    "displayViews", "entryColumns", "fileEntryColumns", "folderColumns",
190                    "mimeTypes"
191            };
192    
193            private static final ResourceManager _resourceManager =
194                    new ClassLoaderResourceManager(
195                            DLPortletInstanceSettings.class.getClassLoader());
196    
197            static {
198                    SettingsFactory settingsFactory =
199                            SettingsFactoryUtil.getSettingsFactory();
200    
201                    settingsFactory.registerSettingsMetadata(
202                            PortletKeys.DOCUMENT_LIBRARY, _getFallbackKeys(),
203                            _MULTI_VALUED_KEYS, _resourceManager);
204                    settingsFactory.registerSettingsMetadata(
205                            PortletKeys.DOCUMENT_LIBRARY_ADMIN, _getFallbackKeys(),
206                            _MULTI_VALUED_KEYS, _resourceManager);
207                    settingsFactory.registerSettingsMetadata(
208                            PortletKeys.DOCUMENT_LIBRARY_DISPLAY, _getFallbackKeys(),
209                            _MULTI_VALUED_KEYS, _resourceManager);
210                    settingsFactory.registerSettingsMetadata(
211                            PortletKeys.MEDIA_GALLERY_DISPLAY, _getFallbackKeys(),
212                            _MULTI_VALUED_KEYS, _resourceManager);
213            }
214    
215            private final TypedSettings _typedSettings;
216    
217    }