001    /**
002     * Copyright (c) 2000-2013 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 onAfterUpdate(Layout layout) {
042                    clearCache(layout);
043            }
044    
045            @Override
046            public void onBeforeRemove(Layout layout) throws ModelListenerException {
047                    try {
048                            if (!LayoutStagingUtil.isBranchingLayout(layout)) {
049                                    return;
050                            }
051    
052                            LayoutRevisionLocalServiceUtil.deleteLayoutLayoutRevisions(
053                                    layout.getPlid());
054                    }
055                    catch (IllegalStateException ise) {
056    
057                            // This is only needed because of LayoutPersistenceTest but should
058                            // never happen in a deployed environment
059    
060                    }
061                    catch (PortalException pe) {
062                            throw new ModelListenerException(pe);
063                    }
064                    catch (SystemException se) {
065                            throw new ModelListenerException(se);
066                    }
067            }
068    
069            protected void clearCache(Layout layout) {
070                    if (layout == null) {
071                            return;
072                    }
073    
074                    if (!layout.isPrivateLayout()) {
075                            CacheUtil.clearCache(layout.getCompanyId());
076                    }
077            }
078    
079    }