001/*
002 *  Copyright 2015 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.observer;
017
018import org.ametys.core.observation.Event;
019import org.ametys.web.ObservationConstants;
020import org.ametys.web.WebConstants;
021import org.ametys.web.repository.content.WebContent;
022import org.ametys.web.repository.page.Page;
023
024/**
025 * Observer triggered by the deletion/unpublication of a blog content
026 */
027public class LuceneBlogContentDeletedOrUnpublishedObserver extends AbstractBlogObserver
028{
029    @Override
030    public boolean supports(Event event)
031    {
032        return event.getId().equals(org.ametys.cms.ObservationConstants.EVENT_CONTENT_DELETING)
033            || event.getId().equals(ObservationConstants.EVENT_CONTENT_UNPUBLISHED);
034    }
035    
036    @Override
037    protected void _internalObserve(Event event, Page postPage, WebContent postContent)
038    {
039        try
040        {
041            if (getLogger().isInfoEnabled())
042            {
043                getLogger().info("Deleting solr document for page: " + postPage.getId());
044            }
045            
046            // Update the document by first deleting it
047            _solrPageIndexer.unindexPage(postPage.getId(), true, true);
048        }
049        catch (Exception e)
050        {
051            getLogger().error("Unable to index post page " + postContent, e);
052            
053            try
054            {
055                _solrIndexer.rollback(null);
056                _solrIndexer.rollback(WebConstants.LIVE_WORKSPACE);
057            }
058            catch (Exception ex)
059            {
060                getLogger().error("Error rolling back the indexing transation for post page " + postContent, e);
061            }
062        }
063    }
064}