001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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, SystemException {
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, SystemException {
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, SystemException {
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, SystemException {
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, SystemException {
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                    throws SystemException {
146    
147                    try {
148                            Map<String, String[]> targetPreferencesMap =
149                                    targetPreferences.getMap();
150    
151                            for (String key : targetPreferencesMap.keySet()) {
152                                    try {
153                                            targetPreferences.reset(key);
154                                    }
155                                    catch (ReadOnlyException roe) {
156                                    }
157                            }
158    
159                            Map<String, String[]> sourcePreferencesMap =
160                                    sourcePreferences.getMap();
161    
162                            for (String key : sourcePreferencesMap.keySet()) {
163                                    try {
164                                            targetPreferences.setValues(
165                                                    key, sourcePreferences.getValues(key, new String[0]));
166                                    }
167                                    catch (ReadOnlyException roe) {
168                                    }
169                            }
170    
171                            targetPreferences.store();
172                    }
173                    catch (IOException ioe) {
174                            _log.error(ioe);
175                    }
176                    catch (ValidatorException ve) {
177                            throw new SystemException(ve);
178                    }
179            }
180    
181            private static Log _log = LogFactoryUtil.getLog(
182                    PortletPreferencesServiceImpl.class);
183    
184    }