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.repository.Content;
022import org.ametys.core.observation.Event;
023import org.ametys.plugins.userdirectory.page.OrganisationChartPageResolver;
024import org.ametys.plugins.userdirectory.page.UserDirectoryPageResolver;
025import org.ametys.web.repository.page.Page;
026
027/**
028 * Observes user directory content deletion in order to synchronize the solr index.
029 */
030public class SolrContentDeletedObserver extends AbstractSolrContentObserver
031{
032    /** The user directory page resolver */
033    protected UserDirectoryPageResolver _uDPageResolver;
034    
035    /** The organisation chart page resolver */
036    protected OrganisationChartPageResolver _oCPageResolver;
037    
038    @Override
039    public void service(ServiceManager manager) throws ServiceException
040    {
041        super.service(manager);
042        _uDPageResolver = (UserDirectoryPageResolver) manager.lookup(UserDirectoryPageResolver.ROLE);
043        _oCPageResolver = (OrganisationChartPageResolver) manager.lookup(OrganisationChartPageResolver.ROLE);
044    }
045    
046    @Override
047    public boolean supports(Event event)
048    {
049        return event.getId().equals(org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETING);
050    }
051    
052    @Override
053    protected void _internalObserve(Event event, Page rootContentPage, Content content)
054    {
055        Page contentPage = _getContentPage(rootContentPage, content);
056        if (contentPage != null)
057        {
058            try
059            {
060                // Unindex user directory content page in all workspaces
061                _solrPageIndexer.unindexPage(contentPage.getId(), true, true);
062            }
063            catch (Exception e)
064            {
065                getLogger().error("Failed to unindex page " + contentPage.getId(), e);
066            }
067        }
068    }
069    
070    /**
071     * Get content page
072     * @param rootContentPage the root content page
073     * @param content the content
074     * @return the content page
075     */
076    protected Page _getContentPage(Page rootContentPage, Content content)
077    {
078        if (_isUserContent(content))
079        {
080            return _uDPageResolver.getUserPage(rootContentPage, content);
081        }
082        else if (_isOrgUnitContent(content))
083        {
084            return _oCPageResolver.getOrgUnitPage(rootContentPage, content);
085        }
086        
087        return null;
088    }
089}