001    /**
002     * Copyright (c) 2000-2012 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.portlet.journal.model;
016    
017    import com.liferay.portal.ModelListenerException;
018    import com.liferay.portal.kernel.template.TemplateConstants;
019    import com.liferay.portal.kernel.template.TemplateResourceLoaderUtil;
020    import com.liferay.portal.model.BaseModelListener;
021    import com.liferay.portal.servlet.filters.cache.CacheUtil;
022    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     * @author Jon Steer
027     * @author Raymond Augé
028     * @author Shuyang Zhou
029     */
030    public class JournalTemplateListener
031            extends BaseModelListener<JournalTemplate> {
032    
033            @Override
034            public void onAfterRemove(JournalTemplate template)
035                    throws ModelListenerException {
036    
037                    try {
038                            clearCache(template);
039                    }
040                    catch (Exception e) {
041                            throw new ModelListenerException(e);
042                    }
043            }
044    
045            @Override
046            public void onAfterUpdate(JournalTemplate template)
047                    throws ModelListenerException {
048    
049                    try {
050                            clearCache(template);
051                    }
052                    catch (Exception e) {
053                            throw new ModelListenerException(e);
054                    }
055            }
056    
057            protected void clearCache(JournalTemplate template) throws Exception {
058    
059                    // Freemarker cache
060    
061                    String freeMarkerTemplateId =
062                            template.getCompanyId() + template.getGroupId() +
063                                    template.getTemplateId();
064    
065                    TemplateResourceLoaderUtil.clearCache(
066                            TemplateConstants.LANG_TYPE_FTL, freeMarkerTemplateId);
067    
068                    // Journal content
069    
070                    JournalContentUtil.clearCache();
071    
072                    // Layout cache
073    
074                    CacheUtil.clearCache(template.getCompanyId());
075    
076                    // Velocity cache
077    
078                    TemplateResourceLoaderUtil.clearCache(
079                            TemplateConstants.LANG_TYPE_VM, freeMarkerTemplateId);
080            }
081    
082    }