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