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.model.LayoutTypePortlet;
021    import com.liferay.portal.service.GroupLocalServiceUtil;
022    import com.liferay.portal.util.PortletKeys;
023    
024    /**
025     * @author Ivan Zaera
026     * @author Jorge Ferrer
027     */
028    public class PortletInstanceSettingsLocator implements SettingsLocator {
029    
030            public PortletInstanceSettingsLocator(Layout layout, String settingsId) {
031                    _layout = layout;
032                    _settingsId = settingsId;
033            }
034    
035            @Override
036            public Settings getSettings() throws SettingsException {
037                    long companyId = getCompanyId(_layout.getGroupId());
038    
039                    Settings portalPropertiesSettings =
040                            _settingsLocatorHelper.getPortalPropertiesSettings();
041    
042                    Settings configurationBeanSettings =
043                            _settingsLocatorHelper.getConfigurationBeanSettings(
044                                    _settingsId, portalPropertiesSettings);
045    
046                    Settings portalPreferencesSettings =
047                            _settingsLocatorHelper.getPortalPreferencesSettings(
048                                    companyId, configurationBeanSettings);
049    
050                    Settings companyPortletPreferencesSettings =
051                            _settingsLocatorHelper.getCompanyPortletPreferencesSettings(
052                                    companyId, _settingsId, portalPreferencesSettings);
053    
054                    Settings groupPortletPreferencesSettings =
055                            _settingsLocatorHelper.getGroupPortletPreferencesSettings(
056                                    _layout.getGroupId(), _settingsId,
057                                    companyPortletPreferencesSettings);
058    
059                    return
060                            _settingsLocatorHelper.getPortletInstancePortletPreferencesSettings(
061                                    _layout.getCompanyId(), getPlid(), _settingsId,
062                                    groupPortletPreferencesSettings);
063            }
064    
065            @Override
066            public String getSettingsId() {
067                    return _settingsId;
068            }
069    
070            protected long getCompanyId(long groupId) throws SettingsException {
071                    try {
072                            Group group = GroupLocalServiceUtil.getGroup(groupId);
073    
074                            return group.getCompanyId();
075                    }
076                    catch (PortalException pe) {
077                            throw new SettingsException(pe);
078                    }
079            }
080    
081            protected long getPlid() {
082                    if (isEmbeddedPortlet()) {
083                            return PortletKeys.PREFS_PLID_SHARED;
084                    }
085    
086                    return _layout.getPlid();
087            }
088    
089            protected boolean isEmbeddedPortlet() {
090                    if (_embeddedPortlet != null) {
091                            return _embeddedPortlet;
092                    }
093    
094                    _embeddedPortlet = false;
095    
096                    if (_layout.isSupportsEmbeddedPortlets()) {
097                            LayoutTypePortlet layoutTypePortlet =
098                                    (LayoutTypePortlet)_layout.getLayoutType();
099    
100                            _embeddedPortlet = layoutTypePortlet.isPortletEmbedded(_settingsId);
101                    }
102    
103                    return _embeddedPortlet;
104            }
105    
106            private Boolean _embeddedPortlet = null;
107            private final Layout _layout;
108            private final String _settingsId;
109            private final SettingsLocatorHelper _settingsLocatorHelper =
110                    SettingsLocatorHelperUtil.getSettingsLocatorHelper();
111    
112    }