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