001/*
002 *  Copyright 2016 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.userdirectory.observation;
017
018import org.apache.avalon.framework.service.ServiceException;
019import org.apache.avalon.framework.service.ServiceManager;
020
021import org.ametys.cms.ObservationConstants;
022import org.ametys.cms.repository.Content;
023import org.ametys.core.observation.Event;
024import org.ametys.core.observation.Observer;
025import org.ametys.plugins.userdirectory.UserDirectoryHelper;
026import org.ametys.plugins.userdirectory.UserDirectoryPageHandler;
027import org.ametys.web.repository.page.Page;
028
029/**
030 * {@link Observer} when a user content is modified, deleted or validated.
031 * Clear the cache of virtual user pages and reindex all virtual pages on modification.
032 */
033public class UserContentCacheObserver extends AbstractContentObserver
034{
035    /** The user directory page handler. */
036    protected UserDirectoryPageHandler _userPageHandler;
037    /** The user directory helper */
038    protected UserDirectoryHelper _userDirectoryHelper;
039    
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        _userPageHandler = (UserDirectoryPageHandler) manager.lookup(UserDirectoryPageHandler.ROLE);
045        _userDirectoryHelper = (UserDirectoryHelper) manager.lookup(UserDirectoryHelper.ROLE);
046    }
047
048    @Override
049    public boolean supports(Event event)
050    {
051        return event.getId().equals(ObservationConstants.EVENT_CONTENT_DELETING)  
052                || event.getId().equals(ObservationConstants.EVENT_CONTENT_UNTAG_LIVE) 
053                || event.getId().equals(ObservationConstants.EVENT_CONTENT_MODIFIED) 
054                || event.getId().equals(ObservationConstants.EVENT_CONTENT_ADDED) 
055                || event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED);
056    }
057
058    @Override
059    public int getPriority()
060    {
061        // Will be processed AFTER live synchronization observers and AFTER page unindexing observer
062        return MAX_PRIORITY + 5000;
063    }
064
065    @Override
066    protected void _internalObserve(Event event, Page rootUsersPage, Content userContent)
067    {
068        if (_isUserContent(userContent))
069        {
070            if (!event.getId().equals(ObservationConstants.EVENT_CONTENT_ADDED))
071            {
072                _userPageHandler.clearCache(rootUsersPage);
073                // Invalidate user page cache
074                _userDirectoryPageResolver.invalidateUserPageCache(userContent.getLanguage());
075            }
076            
077            if (!event.getId().equals(ObservationConstants.EVENT_CONTENT_UNTAG_LIVE) && !event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED))
078            {
079                // Invalidate user content cache
080                _userDirectoryHelper.invalidateUserContentCache(userContent.getLanguage());
081            }
082        }
083    }
084}