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.portlet.admin.util;
016    
017    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
018    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
020    import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
021    import com.liferay.portal.kernel.dao.orm.Property;
022    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
023    import com.liferay.portal.kernel.exception.PortalException;
024    import com.liferay.portal.kernel.exception.SystemException;
025    import com.liferay.portal.kernel.log.Log;
026    import com.liferay.portal.kernel.log.LogFactoryUtil;
027    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
028    import com.liferay.portal.kernel.util.ProxyUtil;
029    import com.liferay.portal.model.Layout;
030    import com.liferay.portal.model.LayoutRevision;
031    import com.liferay.portal.model.LayoutStagingHandler;
032    import com.liferay.portal.model.LayoutTypePortlet;
033    import com.liferay.portal.model.Portlet;
034    import com.liferay.portal.model.PortletPreferences;
035    import com.liferay.portal.service.LayoutLocalServiceUtil;
036    import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
037    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
038    import com.liferay.portal.service.persistence.PortletPreferencesActionableDynamicQuery;
039    
040    import java.util.ArrayList;
041    import java.util.List;
042    
043    /**
044     * @author Andrew Betts
045     * @author Christopher Kian
046     */
047    public class CleanUpPortletPreferencesUtil {
048    
049            public static void cleanUpLayoutRevisionPortletPreferences()
050                    throws Exception {
051    
052                    CacheRegistryUtil.setActive(true);
053    
054                    ActionableDynamicQuery actionableDynamicQuery =
055                            new PortletPreferencesActionableDynamicQuery() {
056    
057                            @Override
058                            protected void addCriteria(DynamicQuery dynamicQuery) {
059                                    Property plidProperty = PropertyFactoryUtil.forName("plid");
060    
061                                    DynamicQuery layoutRevisionDynamicQuery =
062                                            LayoutRevisionLocalServiceUtil.dynamicQuery();
063    
064                                    layoutRevisionDynamicQuery.setProjection(
065                                            ProjectionFactoryUtil.property("layoutRevisionId"));
066    
067                                    dynamicQuery.add(plidProperty.in(layoutRevisionDynamicQuery));
068                            }
069    
070                            @Override
071                            protected void performAction(Object object)
072                                    throws PortalException, SystemException {
073    
074                                    PortletPreferences portletPreferences =
075                                            (PortletPreferences)object;
076    
077                                    long layoutRevisionId = portletPreferences.getPlid();
078    
079                                    LayoutRevision layoutRevision =
080                                            LayoutRevisionLocalServiceUtil.getLayoutRevision(
081                                                    layoutRevisionId);
082    
083                                    Layout layout = LayoutLocalServiceUtil.getLayout(
084                                            layoutRevision.getPlid());
085    
086                                    if (!layout.isTypePortlet()) {
087                                            return;
088                                    }
089    
090                                    if (containsPortlet(
091                                                    layout, portletPreferences.getPortletId())) {
092    
093                                            return;
094                                    }
095    
096                                    LayoutStagingHandler layoutStagingHandler =
097                                            new LayoutStagingHandler(layout);
098    
099                                    layoutStagingHandler.setLayoutRevision(layoutRevision);
100    
101                                    Layout proxiedLayout = (Layout)ProxyUtil.newProxyInstance(
102                                            PortalClassLoaderUtil.getClassLoader(),
103                                            new Class[] {Layout.class}, layoutStagingHandler);
104    
105                                    if (containsPortlet(
106                                                    proxiedLayout, portletPreferences.getPortletId())) {
107    
108                                            return;
109                                    }
110    
111                                    if (_log.isWarnEnabled()) {
112                                            _log.warn(
113                                                    "Removing portlet preferences " +
114                                                            portletPreferences.getPortletPreferencesId());
115                                    }
116    
117                                    PortletPreferencesLocalServiceUtil.deletePortletPreferences(
118                                            portletPreferences);
119                            }
120    
121                    };
122    
123                    actionableDynamicQuery.performActions();
124    
125                    CacheRegistryUtil.setActive(false);
126            }
127    
128            protected static boolean containsPortlet(Layout layout, String portletId)
129                    throws PortalException, SystemException {
130    
131                    LayoutTypePortlet layoutTypePortlet =
132                            (LayoutTypePortlet)layout.getLayoutType();
133    
134                    List<Portlet> portlets = layoutTypePortlet.getAllPortlets();
135    
136                    List<String> portletIds = new ArrayList<String>(portlets.size());
137    
138                    for (Portlet portlet : portlets) {
139                            portletIds.add(Portlet.PORTLET_ID_ACCESSOR.get(portlet));
140                    }
141    
142                    return portletIds.contains(portletId);
143            }
144    
145            private static Log _log = LogFactoryUtil.getLog(
146                    CleanUpPortletPreferencesUtil.class);
147    
148    }