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.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 final String _RESOURCE_TEMPLATE_NAME_SPACE = String.valueOf(
074                    ResourceManager.RESOURCE_TEMPLATE);
075    
076    }