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(), getOwnerId(),
062                                    PortletKeys.PREFS_OWNER_TYPE_LAYOUT, getPlid(), _settingsId,
063                                    groupPortletPreferencesSettings);
064            }
065    
066            @Override
067            public String getSettingsId() {
068                    return _settingsId;
069            }
070    
071            protected long getCompanyId(long groupId) throws SettingsException {
072                    try {
073                            Group group = GroupLocalServiceUtil.getGroup(groupId);
074    
075                            return group.getCompanyId();
076                    }
077                    catch (PortalException pe) {
078                            throw new SettingsException(pe);
079                    }
080            }
081    
082            protected long getOwnerId() {
083                    if (isEmbeddedPortlet()) {
084                            return _layout.getGroupId();
085                    }
086    
087                    return PortletKeys.PREFS_OWNER_ID_DEFAULT;
088            }
089    
090            protected long getPlid() {
091                    if (isEmbeddedPortlet()) {
092                            return PortletKeys.PREFS_PLID_SHARED;
093                    }
094    
095                    return _layout.getPlid();
096            }
097    
098            protected boolean isEmbeddedPortlet() {
099                    if (_embeddedPortlet != null) {
100                            return _embeddedPortlet;
101                    }
102    
103                    _embeddedPortlet = false;
104    
105                    if (_layout.isSupportsEmbeddedPortlets()) {
106                            LayoutTypePortlet layoutTypePortlet =
107                                    (LayoutTypePortlet)_layout.getLayoutType();
108    
109                            _embeddedPortlet = layoutTypePortlet.isPortletEmbedded(_settingsId);
110                    }
111    
112                    return _embeddedPortlet;
113            }
114    
115            private Boolean _embeddedPortlet = null;
116            private final Layout _layout;
117            private final String _settingsId;
118            private final SettingsLocatorHelper _settingsLocatorHelper =
119                    SettingsLocatorHelperUtil.getSettingsLocatorHelper();
120    
121    }