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