001    /**
002     * Copyright (c) 2000-2013 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.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.isPrivateLayout()) {
071                            CacheUtil.clearCache(layout.getCompanyId());
072                    }
073            }
074    
075    }