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