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.
077                                    PerformActionMethod<PortletPreferences>() {
078    
079                                    @Override
080                                    public void performAction(PortletPreferences portletPreferences)
081                                            throws PortalException {
082    
083                                            long layoutRevisionId = portletPreferences.getPlid();
084    
085                                            LayoutRevision layoutRevision =
086                                                    LayoutRevisionLocalServiceUtil.getLayoutRevision(
087                                                            layoutRevisionId);
088    
089                                            Layout layout = LayoutLocalServiceUtil.getLayout(
090                                                    layoutRevision.getPlid());
091    
092                                            if (!layout.isTypePortlet()) {
093                                                    return;
094                                            }
095    
096                                            LayoutTypePortlet layoutTypePortlet =
097                                                    (LayoutTypePortlet)layout.getLayoutType();
098    
099                                            List<Portlet> portlets = layoutTypePortlet.getAllPortlets();
100    
101                                            List<String> portletIds = ListUtil.toList(
102                                                    portlets, Portlet.PORTLET_ID_ACCESSOR);
103    
104                                            if (portletIds.contains(
105                                                            portletPreferences.getPortletId())) {
106    
107                                                    return;
108                                            }
109    
110                                            if (_log.isWarnEnabled()) {
111                                                    _log.warn(
112                                                            "Removing portlet preferences " +
113                                                                    portletPreferences.getPortletPreferencesId());
114                                            }
115    
116                                            PortletPreferencesLocalServiceUtil.deletePortletPreferences(
117                                                    portletPreferences);
118                                    }
119    
120                            });
121    
122                    return portletPreferencesActionableDynamicQuery;
123            }
124    
125            @Override
126            protected void doVerify() throws Exception {
127                    cleanUpLayoutRevisionPortletPreferences();
128            }
129    
130            private static final Log _log = LogFactoryUtil.getLog(
131                    VerifyPortletPreferences.class);
132    
133    }