001/*
002 *  Copyright 2014 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.tags.observers;
017
018import java.util.Collection;
019import java.util.HashMap;
020import java.util.Iterator;
021import java.util.Map;
022import java.util.Set;
023
024import org.ametys.cms.ObservationConstants;
025import org.ametys.cms.repository.Content;
026import org.ametys.cms.tag.Tag;
027import org.ametys.cms.tag.TagHelper;
028import org.ametys.core.observation.Event;
029import org.ametys.core.observation.Observer;
030import org.ametys.plugins.repository.AmetysObject;
031import org.ametys.plugins.repository.AmetysObjectIterable;
032import org.ametys.web.repository.site.Site;
033
034/**
035 * Tag added {@link Observer}:
036 * - Clear the cache.
037 * - Re-index contents tagged with this tag or a descendant tag.
038 */
039public class TagUpdatedObserver extends AbstractTagObserver
040{
041    @Override
042    public boolean supports(Event event)
043    {
044        return event.getId().equals(ObservationConstants.EVENT_TAG_UPDATED);
045    }
046    
047    @Override
048    protected void observe(Site site, AmetysObject aoTag, String tagName, Event rawEvent)
049    {
050        // Re-index content tagged with this tag or a descendant tag
051        Map<String, Object> contextParameters = new HashMap<>();
052        if (site != null)
053        {
054            contextParameters.put("siteName", site.getName());
055        }
056        Tag tag = _tagProviderEP.getTag(tagName, contextParameters);
057        Set<String> descendantNames = TagHelper.getDescendantNames(tag, true);
058        
059        try
060        {
061            _reindexTaggedContents(descendantNames);
062        }
063        catch (Exception e)
064        {
065            getLogger().error("Unable to create or update index for event: " + rawEvent, e);
066        }
067        
068        // Clear cache
069        _clearCache(site);
070    }
071    
072    /**
073     * Re-index content tagged with at least of this tags
074     * @param descendantNames the descendant's name
075     * @throws Exception if an error occurs
076     */
077    protected void _reindexTaggedContents(Collection<String> descendantNames) throws Exception
078    {
079        AmetysObjectIterable<Content> contents = _getTaggedContents(descendantNames);
080        Iterator<Content> it = contents.iterator();
081        
082        while (it.hasNext())
083        {
084            Content content = it.next();
085            _reindexContent(content);
086        }
087    }
088    
089    /**
090     * Re-index the given content
091     * @param content the content to re-index
092     * @throws Exception if unable to create or update the index
093     */
094    protected void _reindexContent(Content content) throws Exception
095    {
096        _updatePageDocumentsForContent(content.getId());
097    }
098}