001    /**
002     * Copyright (c) 2000-2011 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.model;
016    
017    import com.liferay.portal.service.persistence.LayoutRevisionUtil;
018    import com.liferay.portal.service.persistence.LayoutUtil;
019    import com.liferay.portal.servlet.filters.cache.CacheUtil;
020    
021    /**
022     * @author Alexander Chow
023     * @author Raymond Augé
024     */
025    public class PortletPreferencesListener
026            extends BaseModelListener<PortletPreferences> {
027    
028            @Override
029            public void onAfterRemove(PortletPreferences portletPreferences) {
030                    clearCache(portletPreferences);
031            }
032    
033            @Override
034            public void onAfterUpdate(PortletPreferences portletPreferences) {
035                    clearCache(portletPreferences);
036            }
037    
038            protected void clearCache(PortletPreferences portletPreferences) {
039                    try {
040                            long companyId = 0;
041    
042                            Layout layout = LayoutUtil.fetchByPrimaryKey(
043                                    portletPreferences.getPlid());
044    
045                            if ((layout != null) && !layout.isPrivateLayout()) {
046                                    companyId = layout.getCompanyId();
047                            }
048                            else {
049                                    LayoutRevision layoutRevision =
050                                            LayoutRevisionUtil.fetchByPrimaryKey(
051                                                    portletPreferences.getPlid());
052    
053                                    if ((layoutRevision != null) &&
054                                            !layoutRevision.isPrivateLayout()) {
055    
056                                            companyId = layoutRevision.getCompanyId();
057                                    }
058                            }
059    
060                            if (companyId > 0) {
061                                    CacheUtil.clearCache(companyId);
062                            }
063                    }
064                    catch (Exception e) {
065                            CacheUtil.clearCache();
066                    }
067            }
068    
069    }