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.portal.verify;
016    
017    import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
018    import com.liferay.portal.kernel.dao.orm.DynamicQuery;
019    import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
020    import com.liferay.portal.kernel.dao.orm.Property;
021    import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
022    import com.liferay.portal.kernel.exception.PortalException;
023    import com.liferay.portal.kernel.log.Log;
024    import com.liferay.portal.kernel.log.LogFactoryUtil;
025    import com.liferay.portal.kernel.util.ListUtil;
026    import com.liferay.portal.model.Layout;
027    import com.liferay.portal.model.LayoutRevision;
028    import com.liferay.portal.model.LayoutTypePortlet;
029    import com.liferay.portal.model.Portlet;
030    import com.liferay.portal.model.PortletPreferences;
031    import com.liferay.portal.service.LayoutLocalServiceUtil;
032    import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
033    import com.liferay.portal.service.PortletPreferencesLocalServiceUtil;
034    
035    import java.util.List;
036    
037    /**
038     * @author Andrew Betts
039     */
040    public class VerifyPortletPreferences extends VerifyProcess {
041    
042            public static void cleanUpLayoutRevisionPortletPreferences()
043                    throws Exception {
044    
045                    ActionableDynamicQuery actionableDynamicQuery =
046                            getPortletPreferencesActionableDynamicQuery();
047    
048                    actionableDynamicQuery.performActions();
049            }
050    
051            protected static ActionableDynamicQuery
052                    getPortletPreferencesActionableDynamicQuery() {
053    
054                    ActionableDynamicQuery portletPreferencesActionableDynamicQuery =
055                            PortletPreferencesLocalServiceUtil.getActionableDynamicQuery();
056    
057                    portletPreferencesActionableDynamicQuery.setAddCriteriaMethod(
058                            new ActionableDynamicQuery.AddCriteriaMethod() {
059    
060                                    @Override
061                                    public void addCriteria(DynamicQuery dynamicQuery) {
062                                            Property plidProperty = PropertyFactoryUtil.forName("plid");
063    
064                                            DynamicQuery layoutRevisionDynamicQuery =
065                                                    LayoutRevisionLocalServiceUtil.dynamicQuery();
066    
067                                            layoutRevisionDynamicQuery.setProjection(
068                                                    ProjectionFactoryUtil.property("layoutRevisionId"));
069    
070                                            dynamicQuery.add(
071                                                    plidProperty.in(layoutRevisionDynamicQuery));
072                                    }
073    
074                            });
075                    portletPreferencesActionableDynamicQuery.setPerformActionMethod(
076                            new ActionableDynamicQuery.PerformActionMethod() {
077    
078                                    @Override
079                                    public void performAction(Object object)
080                                            throws PortalException {
081    
082                                            PortletPreferences portletPreferences =
083                                                    (PortletPreferences)object;
084    
085                                            long layoutRevisionId = portletPreferences.getPlid();
086    
087                                            LayoutRevision layoutRevision =
088                                                    LayoutRevisionLocalServiceUtil.getLayoutRevision(
089                                                            layoutRevisionId);
090    
091                                            Layout layout = LayoutLocalServiceUtil.getLayout(
092                                                    layoutRevision.getPlid());
093    
094                                            if (!layout.isTypePortlet()) {
095                                                    return;
096                                            }
097    
098                                            LayoutTypePortlet layoutTypePortlet =
099                                                    (LayoutTypePortlet)layout.getLayoutType();
100    
101                                            List<Portlet> portlets = layoutTypePortlet.getAllPortlets();
102    
103                                            List<String> portletIds = ListUtil.toList(
104                                                    portlets, Portlet.PORTLET_ID_ACCESSOR);
105    
106                                            if (portletIds.contains(
107                                                            portletPreferences.getPortletId())) {
108    
109                                                    return;
110                                            }
111    
112                                            if (_log.isWarnEnabled()) {
113                                                    _log.warn(
114                                                            "Removing portlet preferences " +
115                                                                    portletPreferences.getPortletPreferencesId());
116                                            }
117    
118                                            PortletPreferencesLocalServiceUtil.deletePortletPreferences(
119                                                    portletPreferences);
120                                    }
121    
122                            });
123    
124                    return portletPreferencesActionableDynamicQuery;
125            }
126    
127            @Override
128            protected void doVerify() throws Exception {
129                    cleanUpLayoutRevisionPortletPreferences();
130            }
131    
132            private static final Log _log = LogFactoryUtil.getLog(
133                    VerifyPortletPreferences.class);
134    
135    }