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