001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }