001/*
002 *  Copyright 2010 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.web.indexing.observation;
017
018import java.util.Map;
019
020import javax.jcr.Repository;
021import javax.jcr.Session;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.Observer;
028import org.ametys.plugins.repository.RepositoryConstants;
029import org.ametys.web.ObservationConstants;
030import org.ametys.web.WebConstants;
031import org.ametys.web.repository.page.Page;
032import org.ametys.web.skin.Skin;
033import org.ametys.web.skin.SkinsManager;
034import org.ametys.web.synchronization.SynchronizeComponent;
035
036/**
037 * {@link Observer} for observing page move in order to synchronize
038 * Solr index associated.
039 */
040public class SolrPageMovePart2Observer extends AbstractLiveSolrObserver
041{
042    private SkinsManager _skinsManager;
043    private Repository _repository;
044    private SynchronizeComponent _synchronizeHelper;
045    
046    @Override
047    public void service(ServiceManager manager) throws ServiceException
048    {
049        super.service(manager);
050        _skinsManager = (SkinsManager) manager.lookup(SkinsManager.ROLE);
051        _repository = (Repository) manager.lookup("javax.jcr.Repository");
052        _synchronizeHelper = (SynchronizeComponent) manager.lookup(SynchronizeComponent.ROLE);
053    }
054    
055    @Override
056    public boolean supports(Event event)
057    {
058        return event.getId().equals(ObservationConstants.EVENT_PAGE_MOVED);
059    }
060    
061    @Override
062    protected void _updateIndex(Event event, Map<String, Object> transientVars) throws Exception
063    {
064        Page page = (Page) event.getArguments().get(ObservationConstants.ARGS_PAGE);
065        String oldPathInSitemap = (String) event.getArguments().get("page.old.path");
066        String newPathInSitemap = (String) event.getArguments().get(ObservationConstants.ARGS_PAGE_PATH);
067        
068        // If parent has not changed, no need to reindex anything
069        if (!oldPathInSitemap.equals(newPathInSitemap))
070        {
071            _solrPageIndexer.indexPage(page.getId(), RepositoryConstants.DEFAULT_WORKSPACE, true, false);
072            
073            Skin skin = _skinsManager.getSkin(page.getSite().getSkinId());
074            Session liveSession = _repository.login(WebConstants.LIVE_WORKSPACE);
075            
076            if (_synchronizeHelper.isHierarchyValid(page, liveSession) && _synchronizeHelper.isPageValid(page, skin))
077            {
078                // if page is valid, it should be in live
079                _solrPageIndexer.indexPage(page.getId(), WebConstants.LIVE_WORKSPACE, true, false);
080            }
081            else
082            {
083                // The page was already unindexed, but not its attachments
084                _solrPageIndexer.unindexPage(page.getId(), WebConstants.LIVE_WORKSPACE, true, true);
085            }
086        }
087    }
088}