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.Collections;
020import java.util.HashMap;
021import java.util.HashSet;
022import java.util.Iterator;
023import java.util.Map;
024import java.util.Set;
025
026import org.apache.avalon.framework.service.ServiceException;
027import org.apache.avalon.framework.service.ServiceManager;
028
029import org.ametys.cms.ObservationConstants;
030import org.ametys.cms.repository.Content;
031import org.ametys.core.observation.Event;
032import org.ametys.core.observation.ObservationManager;
033import org.ametys.core.observation.Observer;
034import org.ametys.core.user.CurrentUserProvider;
035import org.ametys.plugins.repository.AmetysObject;
036import org.ametys.plugins.repository.AmetysObjectIterable;
037import org.ametys.web.repository.content.ModifiableWebContent;
038import org.ametys.web.repository.site.Site;
039
040/**
041 * Tag added {@link Observer}:
042 * - Clear the cache.
043 * - Update contents tagged with this tag or a descendant tag and send the CONTENT_TAGGED event.
044 */
045public class TagDeletedObserver extends AbstractTagObserver
046{
047    /** Observer manager. */
048    protected ObservationManager _observationManager;
049    
050    /** The current user provider */
051    protected CurrentUserProvider _currentUserProvider;
052    
053    @Override
054    public void service(ServiceManager manager) throws ServiceException
055    {
056        super.service(manager);
057        _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
058        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
059    }
060    @Override
061    public boolean supports(Event event)
062    {
063        return event.getId().equals(ObservationConstants.EVENT_TAG_DELETED);
064    }
065    
066    @Override
067    protected void observe(Site site, AmetysObject parent, String tagName, Event rawEvent)
068    {
069        // Re-index content tagged with the deleted tag or a descendant tag.
070        Map<String, Object> arguments = rawEvent.getArguments();
071        @SuppressWarnings("unchecked")
072        Collection<String> descendantNames = (Collection<String>) arguments.get("descendantNames");
073        if (descendantNames == null)
074        {
075            descendantNames = Collections.emptySet();
076        }
077        
078        try
079        {
080            _updateTaggedContents(descendantNames);
081        }
082        catch (Exception e)
083        {
084            getLogger().error("Unable to create or update index for event: " + rawEvent, e);
085        }
086        
087        // Clear cache
088        _clearCache(site);
089    }
090    
091    /**
092     * Update content tagged with at least one of the tag names.
093     * Removes theses tags on the content and send the CONTENT_TAGGED event.
094     * @param tagNames the tags' name
095     * @throws Exception if an error occurs
096     */
097    protected void _updateTaggedContents(Collection<String> tagNames) throws Exception
098    {
099        AmetysObjectIterable<Content> contents = _getTaggedContents(tagNames);
100        Iterator<Content> it = contents.iterator();
101        
102        while (it.hasNext())
103        {
104            _updateTaggedContent(it.next(), tagNames);
105        }
106    }
107
108    /**
109     * Update the tagged content.
110     * Remove deleted tags on the content and send the CONTENT_TAGGED event.
111     * @param content the tagged content
112     * @param tagNames the tags' name
113     */
114    protected void _updateTaggedContent(Content content, Collection<String> tagNames)
115    {
116        if (content instanceof ModifiableWebContent)
117        {
118            ModifiableWebContent webContent = (ModifiableWebContent) content;
119            Set<String> oldTags = webContent.getTags();
120            Set<String> newTags = new HashSet<>(oldTags);
121            
122            // Update tags
123            for (String oldTagName : oldTags)
124            {
125                if (tagNames.contains(oldTagName))
126                {
127                    webContent.untag(oldTagName);
128                    newTags.remove(oldTagName);
129                }
130            }
131            
132            if (oldTags.size() != newTags.size())
133            {
134                webContent.saveChanges();
135            }
136            
137            // Notify observers that the content has been tagged
138            Map<String, Object> eventParams = new HashMap<>();
139            eventParams.put(org.ametys.cms.ObservationConstants.ARGS_CONTENT, webContent);
140            eventParams.put(org.ametys.cms.ObservationConstants.ARGS_CONTENT_ID, webContent.getId());
141            eventParams.put("content.tags", newTags);
142            eventParams.put("content.old.tags", oldTags);
143            
144            _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_TAGGED, _currentUserProvider.getUser(), eventParams));
145            
146        }
147        
148    }
149}