001/*
002 *  Copyright 2011 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.cms.workflow;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023import org.apache.commons.lang.ArrayUtils;
024
025import org.ametys.cms.CmsConstants;
026import org.ametys.cms.ObservationConstants;
027import org.ametys.cms.repository.WorkflowAwareContent;
028import org.ametys.core.observation.Event;
029import org.ametys.core.observation.ObservationManager;
030import org.ametys.core.user.CurrentUserProvider;
031import org.ametys.plugins.repository.AmetysRepositoryException;
032import org.ametys.plugins.repository.version.VersionableAmetysObject;
033
034import com.opensymphony.module.propertyset.PropertySet;
035import com.opensymphony.workflow.WorkflowException;
036
037/**
038 * OSWorkflow function for validating a content.
039 */
040public class RemoveLiveLabelFunction extends AbstractContentFunction
041{
042    /** Observer manager. */
043    protected ObservationManager _observerManager;
044    /** Current user provider */
045    protected CurrentUserProvider _currentUserProvider;
046    
047    @Override
048    public void service(ServiceManager manager) throws ServiceException
049    {
050        super.service(manager);
051        _observerManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
052        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
053    }
054    
055    @Override
056    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
057    {
058        _logger.info("Performing content unpublish.");
059        WorkflowAwareContent content = getContent(transientVars);
060        
061        try
062        {
063            unpublishContent(content);
064        }
065        catch (AmetysRepositoryException e)
066        {
067            throw new WorkflowException("Error unpublishing the content", e);
068        }
069        
070        notifyObservers(content);
071    }
072
073    /**
074     * Notify observers of content validation
075     * @param content The content to unpublish
076     */
077    protected void notifyObservers(WorkflowAwareContent content)
078    {
079        _observerManager.notify(new Event(ObservationConstants.EVENT_CONTENT_UNTAG_LIVE, _currentUserProvider.getUser(), _getEventParams(content)));
080    }
081    
082    /**
083     * Get the event params for notifications.
084     * @param content The content to unpublish
085     * @return a {@link Map} of params
086     */
087    protected Map<String, Object> _getEventParams(WorkflowAwareContent content)
088    {
089        Map<String, Object> eventParams = new HashMap<>();
090        eventParams.put(ObservationConstants.ARGS_CONTENT, content);
091        return eventParams;
092    }
093
094    /**
095     * Unpublish the content.
096     * @param content the content to unpublish.
097     * @throws WorkflowException if an error occurs.
098     */
099    protected void unpublishContent(WorkflowAwareContent content) throws WorkflowException
100    {
101        if (!(content instanceof VersionableAmetysObject))
102        {
103            throw new WorkflowException("Invalid content implementation: " + content);
104        }
105        
106        VersionableAmetysObject versionableContent = (VersionableAmetysObject) content;
107        
108        if (ArrayUtils.contains(versionableContent.getAllLabels(), CmsConstants.LIVE_LABEL))
109        {
110            versionableContent.removeLabel(CmsConstants.LIVE_LABEL);
111        }
112        
113        content.saveChanges();
114    }
115}