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.settings;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.settings.ArchivedSettings;
020    import com.liferay.portal.kernel.settings.BaseModifiableSettings;
021    import com.liferay.portal.kernel.settings.ModifiableSettings;
022    import com.liferay.portal.kernel.settings.PortletPreferencesSettings;
023    import com.liferay.portal.model.PortletConstants;
024    import com.liferay.portal.model.PortletItem;
025    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
026    import com.liferay.portal.service.PortletPreferencesServiceUtil;
027    import com.liferay.portal.util.PortletKeys;
028    
029    import java.io.IOException;
030    
031    import java.util.Collection;
032    import java.util.Date;
033    
034    import javax.portlet.PortletPreferences;
035    import javax.portlet.ValidatorException;
036    
037    /**
038     * @author Iv??n Zaera
039     */
040    public class ArchivedSettingsImpl
041            extends BaseModifiableSettings implements ArchivedSettings {
042    
043            public ArchivedSettingsImpl(PortletItem portletItem) {
044                    _portletItem = portletItem;
045            }
046    
047            @Override
048            public void delete() throws IOException {
049                    try {
050                            PortletPreferencesServiceUtil.deleteArchivedPreferences(
051                                    _portletItem.getPortletItemId());
052                    }
053                    catch (PortalException pe) {
054                            throw new IOException("Unable to delete archived settings", pe);
055                    }
056                    catch (SystemException se) {
057                            throw new IOException("Unable to delete archived settings", se);
058                    }
059            }
060    
061            @Override
062            public Date getModifiedDate() {
063                    return _portletItem.getModifiedDate();
064            }
065    
066            @Override
067            public Collection<String> getModifiedKeys() {
068                    ModifiableSettings modifiableSettings = _getModifiableSettings();
069    
070                    return modifiableSettings.getModifiedKeys();
071            }
072    
073            @Override
074            public String getName() {
075                    return _portletItem.getName();
076            }
077    
078            @Override
079            public String getUserName() {
080                    return _portletItem.getUserName();
081            }
082    
083            @Override
084            public void reset(String key) {
085                    ModifiableSettings modifiableSettings = _getModifiableSettings();
086    
087                    modifiableSettings.reset(key);
088            }
089    
090            @Override
091            public ModifiableSettings setValue(String key, String value) {
092                    ModifiableSettings modifiableSettings = _getModifiableSettings();
093    
094                    modifiableSettings.setValue(key, value);
095    
096                    return this;
097            }
098    
099            @Override
100            public ModifiableSettings setValues(String key, String[] values) {
101                    ModifiableSettings modifiableSettings = _getModifiableSettings();
102    
103                    modifiableSettings.setValues(key, values);
104    
105                    return this;
106            }
107    
108            @Override
109            public void store() throws IOException, ValidatorException {
110                    ModifiableSettings modifiableSettings = _getModifiableSettings();
111    
112                    modifiableSettings.store();
113            }
114    
115            @Override
116            protected String doGetValue(String key) {
117                    ModifiableSettings modifiableSettings = _getModifiableSettings();
118    
119                    return modifiableSettings.getValue(key, null);
120            }
121    
122            @Override
123            protected String[] doGetValues(String key) {
124                    ModifiableSettings modifiableSettings = _getModifiableSettings();
125    
126                    return modifiableSettings.getValues(key, null);
127            }
128    
129            private ModifiableSettings _getModifiableSettings() {
130                    if (_portletPreferencesSettings != null) {
131                            return _portletPreferencesSettings;
132                    }
133    
134                    PortletPreferences portletPreferences = null;
135    
136                    try {
137                            long ownerId = _portletItem.getPortletItemId();
138                            int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
139                            long plid = 0;
140                            String portletId = _portletItem.getPortletId();
141    
142                            portletPreferences =
143                                    PortletPreferencesLocalServiceUtil.getPreferences(
144                                            _portletItem.getCompanyId(), ownerId, ownerType, plid,
145                                            PortletConstants.getRootPortletId(portletId));
146                    }
147                    catch (SystemException se) {
148                            throw new RuntimeException("Unable to load settings", se);
149                    }
150    
151                    _portletPreferencesSettings = new PortletPreferencesSettings(
152                            portletPreferences);
153    
154                    return _portletPreferencesSettings;
155            }
156    
157            private final PortletItem _portletItem;
158            private PortletPreferencesSettings _portletPreferencesSettings;
159    
160    }