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.portlet.journal.model;
016    
017    import com.liferay.portal.kernel.freemarker.FreeMarkerEngineUtil;
018    import com.liferay.portal.kernel.velocity.VelocityEngineUtil;
019    import com.liferay.portal.model.BaseModelListener;
020    import com.liferay.portal.servlet.filters.cache.CacheUtil;
021    import com.liferay.portal.velocity.LiferayResourceCacheUtil;
022    import com.liferay.portlet.journalcontent.util.JournalContentUtil;
023    
024    import org.apache.velocity.runtime.resource.ResourceManager;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     * @author Jon Steer
029     * @author Raymond Augé
030     * @author Shuyang Zhou
031     */
032    public class JournalTemplateListener
033            extends BaseModelListener<JournalTemplate> {
034    
035            @Override
036            public void onAfterRemove(JournalTemplate template) {
037                    clearCache(template);
038            }
039    
040            @Override
041            public void onAfterUpdate(JournalTemplate template) {
042                    clearCache(template);
043            }
044    
045            protected void clearCache(JournalTemplate template) {
046    
047                    // Freemarker cache
048    
049                    String freeMarkerTemplateId =
050                            template.getCompanyId() + template.getGroupId() +
051                                    template.getTemplateId();
052    
053                    FreeMarkerEngineUtil.flushTemplate(freeMarkerTemplateId);
054    
055                    // Journal content
056    
057                    JournalContentUtil.clearCache();
058    
059                    // Layout cache
060    
061                    CacheUtil.clearCache(template.getCompanyId());
062    
063                    // Liferay resource cache
064    
065                    LiferayResourceCacheUtil.remove(
066                            _RESOURCE_TEMPLATE_NAME_SPACE.concat(freeMarkerTemplateId));
067    
068                    // Velocity cache
069    
070                    VelocityEngineUtil.flushTemplate(freeMarkerTemplateId);
071            }
072    
073            private static String _RESOURCE_TEMPLATE_NAME_SPACE = String.valueOf(
074                    ResourceManager.RESOURCE_TEMPLATE);
075    
076    }