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.portal.kernel.settings;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.model.Group;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.service.GroupLocalServiceUtil;
021    
022    /**
023     * @author Ivan Zaera
024     * @author Jorge Ferrer
025     */
026    public class PortletInstanceSettingsLocator implements SettingsLocator {
027    
028            public PortletInstanceSettingsLocator(Layout layout, String portletId) {
029                    _layout = layout;
030                    _portletId = portletId;
031            }
032    
033            @Override
034            public Settings getSettings() throws SettingsException {
035                    long companyId = getCompanyId(_layout.getGroupId());
036    
037                    Settings portalPropertiesSettings =
038                            _settingsLocatorHelper.getPortalPropertiesSettings();
039    
040                    Settings configurationBeanSettings =
041                            _settingsLocatorHelper.getConfigurationBeanSettings(
042                                    _portletId, portalPropertiesSettings);
043    
044                    Settings portalPreferencesSettings =
045                            _settingsLocatorHelper.getPortalPreferencesSettings(
046                                    companyId, configurationBeanSettings);
047    
048                    Settings companyPortletPreferencesSettings =
049                            _settingsLocatorHelper.getCompanyPortletPreferencesSettings(
050                                    companyId, _portletId, portalPreferencesSettings);
051    
052                    Settings groupPortletPreferencesSettings =
053                            _settingsLocatorHelper.getGroupPortletPreferencesSettings(
054                                    _layout.getGroupId(), _portletId,
055                                    companyPortletPreferencesSettings);
056    
057                    return
058                            _settingsLocatorHelper.getPortletInstancePortletPreferencesSettings(
059                                    _layout, _portletId, groupPortletPreferencesSettings);
060            }
061    
062            @Override
063            public String getSettingsId() {
064                    return _portletId;
065            }
066    
067            protected long getCompanyId(long groupId) throws SettingsException {
068                    try {
069                            Group group = GroupLocalServiceUtil.getGroup(groupId);
070    
071                            return group.getCompanyId();
072                    }
073                    catch (PortalException pe) {
074                            throw new SettingsException(pe);
075                    }
076            }
077    
078            private final Layout _layout;
079            private final String _portletId;
080            private final SettingsLocatorHelper _settingsLocatorHelper =
081                    SettingsLocatorHelperUtil.getSettingsLocatorHelper();
082    
083    }