001/*
002 *  Copyright 2011 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.blog;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.logger.AbstractLogEnabled;
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.avalon.framework.service.Serviceable;
024import org.apache.commons.lang3.ArrayUtils;
025
026import org.ametys.cms.ObservationConstants;
027import org.ametys.cms.repository.Content;
028import org.ametys.core.observation.Event;
029import org.ametys.core.observation.Observer;
030import org.ametys.plugins.blog.posts.PostContentType;
031import org.ametys.plugins.repository.provider.WorkspaceSelector;
032import org.ametys.web.WebConstants;
033import org.ametys.web.repository.content.WebContent;
034
035/**
036 * Observes post creation, modification and deletion to update the cache accordingly.
037 */
038public class PostObserver extends AbstractLogEnabled implements Serviceable, Observer
039{
040    /** The blog cache manager. */
041    protected BlogCacheManager _blogCache;
042    
043    /** The workspace selector */
044    protected WorkspaceSelector _workspaceSelector;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        _blogCache = (BlogCacheManager) manager.lookup(BlogCacheManager.ROLE);
050        _workspaceSelector = (WorkspaceSelector) manager.lookup(WorkspaceSelector.ROLE);
051    }
052    
053    @Override
054    public boolean supports(Event event)
055    {
056        return event.getId().equals(ObservationConstants.EVENT_CONTENT_ADDED)
057            || event.getId().equals(ObservationConstants.EVENT_CONTENT_MODIFIED)
058            || event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETED)
059            || event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED)
060            || event.getId().equals(BlogObservationConstants.EVENT_FUTURE_CONTENT_VISIBLE);
061    }
062    
063    @Override
064    public int getPriority(Event event)
065    {
066        return MAX_PRIORITY + 1000;
067    }
068    
069    public void observe(Event event, Map<String, Object> transientVars) throws Exception
070    {
071        if (event.getId().equals(ObservationConstants.EVENT_CONTENT_ADDED)
072            || event.getId().equals(ObservationConstants.EVENT_CONTENT_MODIFIED)
073            || event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED)
074            || event.getId().equals(BlogObservationConstants.EVENT_FUTURE_CONTENT_VISIBLE))
075        {
076            Content content = (Content) event.getArguments().get(ObservationConstants.ARGS_CONTENT);
077            
078            if (content != null && ArrayUtils.contains(content.getTypes(), PostContentType.CONTENT_TYPE) && content instanceof WebContent)
079            {
080                WebContent webContent = (WebContent) content;
081                String siteName = webContent.getSiteName();
082                String language = webContent.getLanguage();
083                
084                switch (event.getId())
085                {
086                    case ObservationConstants.EVENT_CONTENT_ADDED:
087                        _blogCache.addPost(siteName, language, content);
088                        break;
089                    case ObservationConstants.EVENT_CONTENT_MODIFIED:
090                        _blogCache.modifyPost(siteName, language, content);
091                        break;
092                    case ObservationConstants.EVENT_CONTENT_VALIDATED:
093                        _blogCache.validatePost(siteName, language, content);
094                        break;
095                    case BlogObservationConstants.EVENT_FUTURE_CONTENT_VISIBLE:
096                        String workspaceName = _workspaceSelector.getWorkspace();
097                        if (WebConstants.LIVE_WORKSPACE.equals(workspaceName))
098                        {
099                            _blogCache.validatePost(siteName, language, content);
100                        }
101                        else
102                        {
103                            _blogCache.modifyPost(siteName, language, content);
104                        }
105                        break;
106                    default:
107                }
108            }
109        }
110        else if (event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETED))
111        {
112            String contentId = (String) event.getArguments().get(ObservationConstants.ARGS_CONTENT_ID);
113            String siteName = (String) event.getArguments().get(org.ametys.web.ObservationConstants.ARGS_SITE_NAME);
114            
115            if (contentId != null && siteName != null)
116            {
117                _blogCache.removePostById(siteName, contentId);
118            }
119        }
120    }
121}