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.ModelListenerException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.staging.LayoutStagingUtil;
021    import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
022    import com.liferay.portal.servlet.filters.cache.CacheUtil;
023    
024    /**
025     * @author Alexander Chow
026     * @author Raymond Augé
027     */
028    public class LayoutListener extends BaseModelListener<Layout> {
029    
030            @Override
031            public void onAfterCreate(Layout layout) {
032                    clearCache(layout);
033            }
034    
035            @Override
036            public void onAfterRemove(Layout layout) {
037                    clearCache(layout);
038            }
039    
040            @Override
041            public void onBeforeRemove(Layout layout) throws ModelListenerException {
042                    try {
043                            if (!LayoutStagingUtil.isBranchingLayout(layout)) {
044                                    return;
045                            }
046    
047                            LayoutRevisionLocalServiceUtil.deleteLayoutLayoutRevisions(
048                                    layout.getPlid());
049                    }
050                    catch (IllegalStateException ise) {
051    
052                            // This is only needed because of LayoutPersistenceTest but should
053                            // never happen in a deployed environment
054    
055                    }
056                    catch (PortalException pe) {
057                            throw new ModelListenerException(pe);
058                    }
059                    catch (SystemException se) {
060                            throw new ModelListenerException(se);
061                    }
062            }
063    
064            @Override
065            public void onAfterUpdate(Layout layout) {
066                    clearCache(layout);
067            }
068    
069            protected void clearCache(Layout layout) {
070                    if (!layout.isPrivateLayout()) {
071                            CacheUtil.clearCache(layout.getCompanyId());
072                    }
073            }
074    
075    }