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