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.web.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.ObservationConstants;
026import org.ametys.cms.repository.WorkflowAwareContent;
027import org.ametys.cms.workflow.AbstractContentWorkflowComponent;
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;
033import org.ametys.web.WebConstants;
034import org.ametys.web.repository.content.WebContent;
035
036import com.opensymphony.module.propertyset.PropertySet;
037import com.opensymphony.workflow.FunctionProvider;
038import com.opensymphony.workflow.WorkflowException;
039
040/**
041 * OSWorkflow function for validating a content.
042 */
043public class UnpublishContentFunction extends AbstractContentWorkflowComponent implements FunctionProvider
044{
045    /** Observer manager. */
046    protected ObservationManager _observerManager;
047    /** Current user provider */
048    protected CurrentUserProvider _currentUserProvider;
049    
050    @Override
051    public void service(ServiceManager manager) throws ServiceException
052    {
053        super.service(manager);
054        _observerManager = (ObservationManager) manager.lookup(ObservationManager.ROLE);
055        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
056    }
057    
058    @Override
059    public void execute(Map transientVars, Map args, PropertySet ps) throws WorkflowException
060    {
061        _logger.info("Performing content unpublish.");
062        WorkflowAwareContent content = getContent(transientVars);
063        
064        try
065        {
066            unpublishContent(content);
067        }
068        catch (AmetysRepositoryException e)
069        {
070            throw new WorkflowException("Error unpublishing the content", e);
071        }
072        
073        String siteName = null;
074        if (content instanceof WebContent)
075        {
076            siteName = ((WebContent) content).getSiteName();
077        }
078        
079        // Notify observers that the content has been unpublished.
080        Map<String, Object> eventParams = new HashMap<>();
081        eventParams.put(ObservationConstants.ARGS_CONTENT, content);
082        eventParams.put(org.ametys.web.ObservationConstants.ARGS_SITE_NAME, siteName);
083        
084        _observerManager.notify(new Event(org.ametys.web.ObservationConstants.EVENT_CONTENT_UNPUBLISHED, _currentUserProvider.getUser(), eventParams));
085    }
086
087    /**
088     * Unpublish the content.
089     * @param content the content to unpublish.
090     * @throws WorkflowException if an error occurs.
091     */
092    protected void unpublishContent(WorkflowAwareContent content) throws WorkflowException
093    {
094        if (!(content instanceof VersionableAmetysObject))
095        {
096            throw new WorkflowException("Invalid content implementation: " + content);
097        }
098        
099        VersionableAmetysObject versionableContent = (VersionableAmetysObject) content;
100        
101        if (ArrayUtils.contains(versionableContent.getAllLabels(), WebConstants.LIVE_LABEL))
102        {
103            versionableContent.removeLabel(WebConstants.LIVE_LABEL);
104        }
105        
106        content.saveChanges();
107    }
108}