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.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.model.PortletConstants;
023    import com.liferay.portal.model.PortletItem;
024    import com.liferay.portal.model.PortletPreferences;
025    import com.liferay.portal.security.permission.ActionKeys;
026    import com.liferay.portal.service.base.PortletPreferencesServiceBaseImpl;
027    import com.liferay.portal.service.permission.GroupPermissionUtil;
028    import com.liferay.portal.service.permission.PortletPermissionUtil;
029    import com.liferay.portal.util.PortletKeys;
030    
031    import java.io.IOException;
032    
033    import java.util.Map;
034    
035    import javax.portlet.ReadOnlyException;
036    import javax.portlet.ValidatorException;
037    
038    /**
039     * @author Jorge Ferrer
040     * @author Raymond Aug??
041     */
042    public class PortletPreferencesServiceImpl
043            extends PortletPreferencesServiceBaseImpl {
044    
045            @Override
046            public void deleteArchivedPreferences(long portletItemId)
047                    throws PortalException {
048    
049                    PortletItem portletItem = portletItemLocalService.getPortletItem(
050                            portletItemId);
051    
052                    GroupPermissionUtil.check(
053                            getPermissionChecker(), portletItem.getGroupId(),
054                            ActionKeys.MANAGE_ARCHIVED_SETUPS);
055    
056                    long ownerId = portletItemId;
057                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
058                    long plid = 0;
059                    String portletId = portletItem.getPortletId();
060    
061                    portletPreferencesLocalService.deletePortletPreferences(
062                            ownerId, ownerType, plid, portletId);
063    
064                    portletItemLocalService.deletePortletItem(portletItemId);
065            }
066    
067            @Override
068            public void restoreArchivedPreferences(
069                            long groupId, Layout layout, String portletId, long portletItemId,
070                            javax.portlet.PortletPreferences preferences)
071                    throws PortalException {
072    
073                    PortletItem portletItem = portletItemLocalService.getPortletItem(
074                            portletItemId);
075    
076                    restoreArchivedPreferences(
077                            groupId, layout, portletId, portletItem, preferences);
078            }
079    
080            @Override
081            public void restoreArchivedPreferences(
082                            long groupId, Layout layout, String portletId,
083                            PortletItem portletItem,
084                            javax.portlet.PortletPreferences preferences)
085                    throws PortalException {
086    
087                    PortletPermissionUtil.check(
088                            getPermissionChecker(), groupId, layout, portletId,
089                            ActionKeys.CONFIGURATION);
090    
091                    long ownerId = portletItem.getPortletItemId();
092                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
093                    long plid = 0;
094    
095                    javax.portlet.PortletPreferences archivedPreferences =
096                            portletPreferencesLocalService.getPreferences(
097                                    portletItem.getCompanyId(), ownerId, ownerType, plid,
098                                    PortletConstants.getRootPortletId(portletId));
099    
100                    copyPreferences(archivedPreferences, preferences);
101            }
102    
103            @Override
104            public void restoreArchivedPreferences(
105                            long groupId, String name, Layout layout, String portletId,
106                            javax.portlet.PortletPreferences preferences)
107                    throws PortalException {
108    
109                    PortletItem portletItem = portletItemLocalService.getPortletItem(
110                            groupId, name, portletId, PortletPreferences.class.getName());
111    
112                    restoreArchivedPreferences(
113                            groupId, layout, portletId, portletItem, preferences);
114            }
115    
116            @Override
117            public void updateArchivePreferences(
118                            long userId, long groupId, String name, String portletId,
119                            javax.portlet.PortletPreferences preferences)
120                    throws PortalException {
121    
122                    PortletPermissionUtil.check(
123                            getPermissionChecker(), groupId, 0, portletId,
124                            ActionKeys.CONFIGURATION);
125    
126                    PortletItem portletItem = portletItemLocalService.updatePortletItem(
127                            userId, groupId, name, portletId,
128                            PortletPreferences.class.getName());
129    
130                    long ownerId = portletItem.getPortletItemId();
131                    int ownerType = PortletKeys.PREFS_OWNER_TYPE_ARCHIVED;
132                    long plid = 0;
133    
134                    javax.portlet.PortletPreferences archivedPreferences =
135                            portletPreferencesLocalService.getPreferences(
136                                    portletItem.getCompanyId(), ownerId, ownerType, plid,
137                                    portletId);
138    
139                    copyPreferences(preferences, archivedPreferences);
140            }
141    
142            protected void copyPreferences(
143                    javax.portlet.PortletPreferences sourcePreferences,
144                    javax.portlet.PortletPreferences targetPreferences) {
145    
146                    try {
147                            Map<String, String[]> targetPreferencesMap =
148                                    targetPreferences.getMap();
149    
150                            for (String key : targetPreferencesMap.keySet()) {
151                                    try {
152                                            targetPreferences.reset(key);
153                                    }
154                                    catch (ReadOnlyException roe) {
155                                    }
156                            }
157    
158                            Map<String, String[]> sourcePreferencesMap =
159                                    sourcePreferences.getMap();
160    
161                            for (String key : sourcePreferencesMap.keySet()) {
162                                    try {
163                                            targetPreferences.setValues(
164                                                    key, sourcePreferences.getValues(key, new String[0]));
165                                    }
166                                    catch (ReadOnlyException roe) {
167                                    }
168                            }
169    
170                            targetPreferences.store();
171                    }
172                    catch (IOException ioe) {
173                            _log.error(ioe);
174                    }
175                    catch (ValidatorException ve) {
176                            throw new SystemException(ve);
177                    }
178            }
179    
180            private static final Log _log = LogFactoryUtil.getLog(
181                    PortletPreferencesServiceImpl.class);
182    
183    }