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.contentio.synchronize.SynchronizableContentsCollectionDAO;
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 DAO for synchronizable contents collections */
038    protected SynchronizableContentsCollectionDAO _synchronizableContentsCollectionDAO;
039
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        _userPageHandler = (UserDirectoryPageHandler) manager.lookup(UserDirectoryPageHandler.ROLE);
045        _synchronizableContentsCollectionDAO = (SynchronizableContentsCollectionDAO) manager.lookup(SynchronizableContentsCollectionDAO.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(org.ametys.web.ObservationConstants.EVENT_CONTENT_UNPUBLISHED) 
053                || event.getId().equals(ObservationConstants.EVENT_CONTENT_MODIFIED) 
054                || event.getId().equals(ObservationConstants.EVENT_CONTENT_VALIDATED);
055    }
056
057    @Override
058    public int getPriority(Event event)
059    {
060        // Will be processed AFTER live synchronization observers and AFTER page unindexing observer
061        return MAX_PRIORITY + 5000;
062    }
063
064    @Override
065    protected void _internalObserve(Event event, Page rootUsersPage, Content userContent)
066    {
067        if (_isUserContent(userContent))
068        {
069            _userPageHandler.clearCache(rootUsersPage);
070        }
071    }
072}